-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
37 lines (26 loc) · 862 Bytes
/
Solution.java
File metadata and controls
37 lines (26 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package hackrank.algorithm.dynamic.fibmod;
import java.math.BigInteger;
import java.util.Scanner;
/**
* Fibonacci Modified Challenge
*
* @see https://www.hackerrank.com/challenges/fibonacci-modified
*/
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int firstTerm = scanner.nextInt();
int secondTerm = scanner.nextInt();
int term = scanner.nextInt();
scanner.close();
BigInteger prevPrev = BigInteger.valueOf(firstTerm);
BigInteger prev = BigInteger.valueOf(secondTerm);
BigInteger current = null;
for (int i = 3; i <= term; i++) {
current = prev.multiply(prev).add(prevPrev);
prevPrev = prev;
prev = current;
}
System.out.println(current.toString());
}
}