Project Euler > Problem 137 > Fibonacci golden nuggets (Java Solution)

Problem:

Consider the infinite polynomial series AF(x) = xF1 + x2F2 + x3F3 + ..., where Fk is the kth term in the Fibonacci sequence: 1, 1, 2, 3, 5, 8, ... ; that is, Fk = Fk[−]1 + Fk[−]2, F1 = 1 and F2 = 1.

For this problem we shall be interested in values of x for which AF(x) is a positive integer.

Surprisingly AF(1/2) = (1/2).1 + (1/2)2.1 + (1/2)3.2 + (1/2)4.3 + (1/2)5.5 + ...
= 1/2 + 1/4 + 2/8 + 3/16 + 5/32 + ...
= 2

The corresponding values of x for the first five natural numbers are shown below.

x AF(x)
[√]2[−]1 1
1/2 2
([√]13[−]2)/3 3
([√]89[−]5)/8 4
([√]34[−]3)/5 5

We shall call AF(x) a golden nugget if x is rational, because they become increasingly rarer; for example, the 10th golden nugget is 74049690.

Find the 15th golden nugget.


Solution:

748317

Code:
The solution may include methods that will be found here: Library.java .

public interface EulerSolution{

public String run();

}
/* 
* Solution to Project Euler problem 37
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/


public final class p037 implements EulerSolution {

public static void main(String[] args) {
System.out.println(new p037().run());
}


public String run() {
long sum = 0;
for (int count = 0, n = 10; count < 11; n++) {
if (isTruncatablePrime(n)) {
sum += n;
count++;
}
}
return Long.toString(sum);
}


private static boolean isTruncatablePrime(int n) {
// Test if left-truncatable
for (long i = 10; i <= n; i *= 10) {
if (!Library.isPrime(n % (int)i))
return false;
}

// Test if right-truncatable
for (; n != 0; n /= 10) {
if (!Library.isPrime(n))
return false;
}

return true;
}

}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact