Project Euler

Project Euler Problem 36

steloflute 2012. 6. 9. 00:07

Problem 36

31 January 2003

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.

(Please note that the palindromic number, in either base, may not include leading zeros.)


Answer:
872187

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.IO;

namespace Euler {
    class Program {
        static bool isPalindrome<T>(T value) {
            var str = value.ToString();
            var to = str.Length / 2;
            for (var i = 0; i <= to; i++) {
                if (str[i] != str[str.Length - 1 - i]) return false;
            }
            return true;
        }

        static void Main(string[] args) {
            Console.WriteLine(Enumerable.Range(1, 1000000)
                .Where(x => isPalindrome(x) && isPalindrome(Convert.ToString(x, 2)))
                .Sum());
        }
    }
}



'Project Euler' 카테고리의 다른 글

Project Euler Problem 38  (0) 2012.06.09
Project Euler Problem 37  (0) 2012.06.09
Project Euler Problem 35  (0) 2012.06.09
Project Euler Problem 34  (0) 2012.06.09
Project Euler Problem 33  (0) 2012.06.09