Project Euler > Problem 140 > Modified Fibonacci golden nuggets (Java Solution)

Problem:

Consider the infinite polynomial series AG(x) = xG1 + x2G2 + x3G3 + ..., where Gk is the kth term of the second order recurrence relation Gk = Gk[−]1 + Gk[−]2, G1 = 1 and G2 = 4; that is, 1, 4, 5, 9, 14, 23, ... .

For this problem we shall be concerned with values of x for which AG(x) is a positive integer.

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

x AG(x)
([√]5[−]1)/4 1
2/5 2
([√]22[−]2)/6 3
([√]137[−]5)/14 4
1/2 5

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

Find the sum of the first thirty golden nuggets.


Solution:

210

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

public interface EulerSolution{

public String run();

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


public final class p040 implements EulerSolution {

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


public String run() {
StringBuilder sb = new StringBuilder();
for (int i = 1; i < 1000000; i++)
sb.append(i);

int prod = 1;
for (int i = 0; i <= 6; i++)
prod *= sb.charAt(Library.pow(10, i) - 1) - '0';
return Integer.toString(prod);
}

}


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