Euler Problem 36 binary palindromes


The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

I love palindromes so I decided to tackle this problem today, and why not do it in c#

I created a button, that sets of the code to check all the numbers less then a million.

We create a variable to hold the sum of the numbers that are palindromes in both decimal as binary.
Then we create a loop to run one million times, calling the method DoublPal with a long and with an o if that is true, we run it again but then with the binary representation of the same number. When its done we show the number in a message box so we can copy it to place it in the answer box @project euler.
  private void button1_Click(object sender, EventArgs e)
            int sum = 0;
            //int i = Int32.Parse(textBox1.Text);
            for (int i = 0; i < 1000000; i++)
                if (DoublPal((ulong)i, "0") == true)
                    string binary = Convert.ToString(i, 2);
                    if (DoublPal(0, binary) == true)
                        sum += i;
Then comes the method :
 private bool DoublPal(ulong i,string a) {
            string test = null;
            if (i == 0)
                 test = a;
                i = Convert.ToUInt64(a);
                 test = i.ToString();
            char[] charr = test.ToCharArray();
            string rev = null;

            for (int j = 0; j < charr.Length; j++)
                rev += charr[charr.Length - j - 1];
            if (rev.Equals(i.ToString())) { return true; }
            else { return false; }

What we have here is a method that return a bool (either false or true), first we check if the long is higher then 0 if so the method goes on to check if the number is a palindrome.

It does so by taking the number, create an char array and then loops trough that array backwards adding it to the new string (rev) so at this point we have one value I that contains the number as given and a string rev that contains the number reversed. We then us a simple .compare to see if they are the same and return true or false. The second time around we do it with the binary representation, the method sees that I = o and compares the binary string in much the same way as the int. Only this time converting the string it retrieved to a number.

I decided to write the answer because the code wont work if you cp it like this, and also because if you really want to figure these problems out yourself you won’t but take this opportunity to learn something

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s