Euler Problem 92


Euler Problem 92

Investigating a square digits number chain with a surprising property.

Solution:

Implementacija jednostavna onako kao je u zadatku definisano:


static void Main(string[] args)
{
 long counter = 0;
 for (int i = 1; i < 10000000; i++)
 {
 if (IsArrivedAt89(i))
  counter++;
 }
 Console.WriteLine(counter);
 Console.Read();
}
static bool IsArrivedAt89(int n)
{
 if (n == 1)
  return false;
 else if (n == 89)
  return true;
 else
 return IsArrivedAt89(Next(n));
}
static int Next(int n)
{
 int value=0;
 char[] str = n.ToString().ToArray();
 for (int i = 0; i < str.Length; i++)
 {
  int temp=int.Parse(str[i].ToString());
  value += temp * temp;
 }
 return value;
}

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 C#, 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