Search This Blog

Tuesday, December 28, 2010

Euler Problem 34

This was an easy one.. at last!

http://projecteuler.net/index.php?section=problems&id=34

______________________

static void Main(string[] args)
       {

           long bigsum = 0;

           for (long i = 3; i < 1000000; i++)
           {
               string s = i.ToString();
               long sum = 0;
               foreach (char c in s)  //split up to chars
               {
                  sum+= factorial(c-48);   //convert from ASCII to numerical value of char (c)
                  if (sum > i) { break; }
               }

               if (sum == i) {bigsum += sum;
               }

           }

           Console.WriteLine(bigsum);
           Console.ReadLine();

       }

static long factorial(long n)
        {
            long tot = 1;
            for (long i = 1; i < n+1; i++)
            {
                tot *= i;

            }
            return tot;
        }

No comments: