Search This Blog

Wednesday, December 29, 2010

Euler Problem 36

Just count from 1 to n, find equivalent binary, check both base 10 and base 2 values are palindromic.. job done!

Interestingly in .NET to turn binary into base 10 then just do this!

Convert.ToInt32("1001",2)    

1001 ==>> 9

________________

 

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

           for (int i = 1; i < 1000000; i++)
           {
               string binary = IntToString(i, new char[] { '0', '1' });

               if (palindromic(i.ToString()) && palindromic(binary))
               {
                   sum += i;
               }

           }

           Console.WriteLine(sum);
           Console.ReadLine();

       }


       static bool palindromic(string s)
       {
           string rev="";
           for (int i = s.Length;i>0;i--)
           {
               rev += s.Substring(i-1,1);
           }

           if (s == rev)
           {
               return true;
           }

           return false;

       }

No comments: