Euler Problem 65

The trauma I went through on Problem 64 made this one a breeze. I also used BigFractions class from this website.

public String algorithm ()
    {
        int [] a = new int [100];
        a[0] = 2;
        int k = 1;
        for (int i = 1; i <= 99; i=i+3)
        {
            if (i <= 99)
                a[i] = 1;
            if (i+1 <= 99)
                a[i+1] = 2*k++;
            if (i+2 <= 99)
                a[i+2] = 1;
        }
        BigFraction F = new BigFraction(a[99],1);
        for (int i = 99; i >= 1; i--)
        {
            F = F.inverse();
            F = F.add(a[i-1]);
        }
        int sum = 0;
        String num = F.numerator().toString();
        for (int n = 0; n <= num.length()-1; n++)
            sum += Integer.parseInt(num.substring(n, n+1));
        return String.format("\nAnswer = %s\n", String.valueOf(sum));
    }

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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