Euler Problem 63


Euler Problem 63

How many n-digit positive integers exist which are also an nth power?

Solution:

Vrlo naivan zadatak. Postaviti dvije petlje prva da ide u beskonačnost, a druga proizvoljno npr. 1000 i čekati da program izlista prvih nekoliko brojeva. Poslije toga se nista ne desava, pa se moze smatrati da je program zavrsio brojanje:

C# Implementacija:

static void Main(string[] args)
{
long counter = 0;
for (long i = 1; ; i++)
{
for (int j = 1; j < 1000; j++)
{
BigInteger number = BigInteger.Pow(i, j);
int digits= NG(number);
if (digits == j)
{
counter++;

Console.WriteLine("Broj={0},  BrojCifara={1}; Mantisa={2}",number,digits,i);
Console.WriteLine(counter);
}
}
}
Console.WriteLine(counter);
Console.Read();
}

static int NG(BigInteger n)
{
return n.ToString().Length;
}

About Bahrudin Hrnjica

PhD in Mechanical Engineering, Microsoft MVP for .NET. Likes .NET, Math, Mechanical Engineering, Evolutionary Algorithms, Blogging.

Posted on 01/03/2010, in Project Euler and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s