Search This Blog

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;
                        }
                    }

 

 


                }

            }

No comments: