Project Euler 55

Problem 55

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

// コンパイル方法:
// $ mcs 055.cs -r:System.Numerics -out:a.exe

class PE055 {
    static bool IsPalindrome(BigInteger n) {
        var a = n.ToString().ToCharArray();
        Array.Reverse(a);
        return a.SequenceEqual(n.ToString());
    }

    static BigInteger ReverseAdd(BigInteger n) {
        var a = n.ToString().ToCharArray();
        Array.Reverse(a);
        return n + BigInteger.Parse(string.Join("", a));
    }

    static bool IsLychrel(int n) {
        var x = new BigInteger(n);
        for (int i = 0; i < 50; i++) {
            x = ReverseAdd(x);
            if (IsPalindrome(x)) {
                return false;
            }
        }
        return true;
    }

    static void Main() {
        int ans = Enumerable.Range(1, 9999).Where(IsLychrel).Count();
        Console.WriteLine(ans);
    }
}