Search This Blog

Sunday, December 19, 2010

Euler Problem 30

Iterate.. split up to sep characters… power of 5 to each.. sum… is it equal to the current iteration variable.. if so.. add to the big sum.. repeat…

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

____________

static void Main(string[] args)
        {
            int bigsum = 0;

            for (int i = 2; i < 10000000; i++)
            {

                string s = i.ToString();
                int sum = 0;

                for (int x=0;x<s.Length;x++)
                {
                    int c = Convert.ToInt32( s.Substring(x, 1));

                    sum += Convert.ToInt32(Math.Pow(c,5));
                }

                if (sum == i)
                {
                    Console.WriteLine(i);
                    bigsum += sum;
                }


            }

            Console.WriteLine(">> "+bigsum);
             Console.ReadLine();

        }

Saturday, December 18, 2010

Euler Problem 29

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

This is easy … IF… you can handle big numbers…. which is possible in .Net4.. or.. using a BigInteger class..

I used the CodeProject class from Chew Keong TAN

http://www.codeproject.com/KB/cs/biginteger.aspx

then ran the simple iteration as follows…

__________________

static void Main(string[] args)
       {
          
           List<BigInteger> ans = new List<BigInteger>();

           for (int a = 2; a < 101; a++)
           {
               for (int b = 2; b < 101; b++)
               {

                   BigInteger bg = pow(a, b);

                   if (!ans.Contains(bg))
                   {
                       ans.Add(bg);
                   }

               }


           }

           Console.WriteLine(ans.Count);

            Console.ReadLine();

       }


       static BigInteger pow(int a, int b)
       {
           BigInteger res = 1;

           for (int t = 1; t < b+1; t++)
           {
               res *= a;
           }

           return res;
       }

Euler Problem 28

Hmmm.. so a spiral… going out from the centre…

Well this isn’t the smoothest solution but I noticed the following from the example given:

21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13

the numbers from the 4 diagonals are:

1,9,25     1,3,13      1,5,17    1,7,21

So I added another lap around the square and found the intervals between numbers were:

1,9,25,49     1,3,13,31      1,5,17,37    1,7,21,43
8  16   24          2  10  18        4 12  20      6  14  22
   8   8               8   8              8   8            8   8 

Hmmmm.. so.. the first diagonal (1,9,25) just goes up in multiples of 8… and so do the others but from different starting points.

So I just made an app which cycled thru building the numbers for each diagonal and adding to a list.. then pull them in order from the list and calc the sum! The only thing to remember is that in the sum you should only count the 1 in the centre a single time.. so I just did a –3… found this out from testing it against their example of a 5x5 square.

Oh and the other thing is when to stop adding… so I just determined this by going until the max value in the first diagonal reached the square of the size…

Like I said.. a bit messy.. but got me the answer first time!

________

static void Main(string[] args)
       {
           int size=  1001;
           long d1 = 1;
           long d2 = 3;
           long d3 = 5;
           long d4 = 7;
           int i = 0;
           long offsetd2 = 2;
           long offsetd3 = 4;
           long offsetd4 = 6;

           List<long> l1 = new List<long>();
           List<long> l2 = new List<long>();
           List<long> l3 = new List<long>();
           List<long> l4 = new List<long>();

           l2.Add(1);
           l2.Add(3);
           l3.Add(1);
           l3.Add(5);
           l4.Add(1);
           l4.Add(7);

           while (d1<((size*size)+1))
           {
               offsetd2 += 8;
               offsetd3 += 8;
               offsetd4 += 8;

               d1 += 8 * i;
               d2 += offsetd2;
               d3 += offsetd3;
               d4 += offsetd4;

               l1.Add(d1);
               l2.Add(d2);
               l3.Add(d3);
               l4.Add(d4);

         
           i++;
           }

           //sort out starting positions

          long sum = 0;

           for (int w = 0; w < (i-1); w++)
           {
               sum += l1[w] + l2[w] + l3[w] + l4[w];
           }

           Console.WriteLine(sum-3);  //1 is only counted on one of the diagonals so -3 from total

            Console.ReadLine();

       }

Euler Problem 27

Requires… a prime number solver… done that earlier.. so.. actually quite an easy problem..

static void Main(string[] args)
{

     // n² + an + b,
     int max = 0;
     int maxx = 0;

     for (int a = -1000; a < 1000; a++)
     {

         for (int b = -1000; b < 1000; b++)
         {
             max = 0;

             for (int n = 0; n < 500; n++)  //number of primes in a row
             {
                int p= (n * n) + (a * n) + b;
                
                 if (isprime(p) && p> 1)
                 {
                     max++;

                     if (max > maxx)
                     {
                         maxx = max;
                       
                         Console.WriteLine(a + "," + b + "," + maxx);
                     }
                 }
                 else
                 {
                     max = 0;
                     break;
                  
                 }
             }
         }

     }

      Console.ReadLine();

}

Euler Problem 9

Did this a while ago but didn’t post… so here it is…

 

static void Main(string[] args)
       {

           for (int a = 1; a < 100000; a++)
           {
               for (int b = a; b < 100000; b++)
               {

                   double c = Math.Sqrt((a*a)+(b*b));
                   if (a + b + c == 1000)
                   {
                       Console.WriteLine(a + "," + b + "," + c);
                       Console.WriteLine(a * b * c);
                   }

               }


           }

       }

Friday, December 17, 2010

Euler Problem 26

Difficult!!

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

Needed help with this one.. from here … thanks!!

http://universequeen.org/archives/133

____________________

 

static void Main(string[] args)
        {
           
            int max=0;
            int max_d=0;

            for (int d=2;d<1000;d++)
            {
                 double s = 1f/Convert.ToDouble(d);
               
                int l = reclencycle(d);

                if (l> max)
                {
                    max=l;
                    max_d=d;
                }

            }

            Console.WriteLine(max_d);
            Console.ReadLine();

        }

 

        static int reclencycle(int n)
        {
         
        int[] q = new int[1000];
        int[] r = new int[1000];
 
        r[0] = 1;
        q[0] = 0;
 
        for( int i=1; i<1000; i++ )
        {
            q[i] = r[i-1]*10/n;
            r[i] = r[i-1]*10 - q[i]*n;
 
            for( int j=1; j<i; j++ )
            {
                if( q[j] == q[i] && r[j] == r[i] )
                {
                    return (i-j);
                }
            }
 
        }
 
        return 0;
    }

Thursday, December 16, 2010

Euler Problem 25

Ahh.. back to good old problem 16 again.. and adapt the code…

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

_______________

           int[] c = new int[1000];
            int[] d = new int[1000];
            int[] e = new int[1000];

            c[0] = 1; //starting condition
            d[0] = 1;
            e[0] = 0;

            int counter = 2;// starts with 1,1,.. needs to be offset in count so F12=144

            while (c[999]==0) 
            {

                counter++;
                //do mult in place
                for (int n = 0; n < 1000; n++)
                {
                    e[n] = d[n];
                    d[n] = c[n];
                    c[n] = c[n]+e[n];

                }


                //sort out carries across to right
                for (int n = 0; n < 999; n++)
                {
                    while (c[n] > 9)
                    {
                        if (c[n] >= 10)
                        {
                            c[n + 1] += 1;
                            c[n] -= 10;
                        }
                    }

                    while (d[n] > 9)
                    {
                        if (d[n] >= 10)
                        {
                            d[n + 1] += 1;
                            d[n] -= 10;
                        }
                    }

                    while (e[n] > 9)
                    {
                        if (e[n] >= 10)
                        {
                            e[n + 1] += 1;
                            e[n] -= 10;
                        }
                    }

 

 


                }

            }