-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
54 lines (37 loc) · 1.41 KB
/
solution.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class solution {
// Complete the fibonacciModified function below.
static BigInteger fibonacciModified(int t1, int t2, int n) {
// since we are dealing with squares integer overflow may occur
// that is why we use BigInteger here
BigInteger a = new BigInteger(Integer.toString(t1));
BigInteger b = new BigInteger(Integer.toString(t2));
// if n==1 or n==0 the answer will be zero
BigInteger c = new BigInteger("0");
for (int i = 2; i < n; i++) {
c = a.add(b.multiply(b));
a = b;
b = c;
}
return c;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] t1T2n = scanner.nextLine().split(" ");
int t1 = Integer.parseInt(t1T2n[0]);
int t2 = Integer.parseInt(t1T2n[1]);
int n = Integer.parseInt(t1T2n[2]);
BigInteger result = fibonacciModified(t1, t2, n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}