-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler25.java
More file actions
47 lines (38 loc) · 742 Bytes
/
euler25.java
File metadata and controls
47 lines (38 loc) · 742 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
38
39
40
41
42
43
44
45
46
47
/*
What is the first term in Fibonacci sequence to contain 1000 digits?
*/
import java.math.BigInteger;
class Euler25
{
public static void main(String argz[])
{
int i = 0;
while(true)
{
if(fibonacciGenerator(i).toString().length() >= 1000) break;
++i;
}
System.out.println(i+1);
}
static BigInteger fibonacciGenerator(int termNo)
{
if(termNo == 1)
{
return new BigInteger("1");
}
BigInteger first = new BigInteger("1");
BigInteger second = new BigInteger("1");
for(int i = termNo; i>1; i--)
{
if (first.compareTo(second) == 1)
{
second = first.add(second);
}
else
{
first = first.add(second);
}
}
return first.compareTo(second) == 1 ? first : second;
}
}