Tag Archives: fraction

Euler Problem 57

With a little research, you realize that this continued fraction is related to Pell numbers and Companion Pell numbers. Wikipedia has some simple formulas to implement to calculate a given numerator or denominator. Just be sure to use BigInteger class for the number representation as they can get pretty big.

    import java.math.*;

    public class Euler57
    {
        public static void main(String[] args)
        {
            Euler57 e = new Euler57();
            System.out.format(“Problem: %s\n”, e.getClass().getName());
            System.out.format(“Answer = %s\n”, e.Problem());
        }
        public String Problem ()
        {
            int count = 0;
            BigInteger [] N = new BigInteger[1001];
            BigInteger [] D = new BigInteger[1001];
            for (int f = 1; f  2)
                {
                    N[f] = N[f-1].multiply(BigInteger.valueOf(2));
                    N[f] = N[f].add(N[f-2]);
                    D[f] = D[f-1].multiply(BigInteger.valueOf(2));
                    D[f] = D[f].add(D[f-2]);
                }
                if (N[f].toString().length() > D[f].toString().length())
                    count++;
            }
            return String.valueOf(count);
        }
    }