-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTribonacciInt.java
More file actions
52 lines (46 loc) · 1.32 KB
/
TribonacciInt.java
File metadata and controls
52 lines (46 loc) · 1.32 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaexercises.difficult;
/**
*
* @author User
*/
public class TribonacciInt {
public static void main(String[] args)
{
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE + 1);
TribonacciInt aTribonacciInt = new TribonacciInt();
aTribonacciInt.printFibonacci();
}
private void printFibonacci()
{
int fb3 = 1;
int fb2 = 1;
int fb1 = 2;
int fbn = 0;
int i = 2;
System.out.printf("F(%1$d) = %2$d\n", 0, fb3);
System.out.printf("F(%1$d) = %2$d\n", 1, fb2);
System.out.printf("F(%1$d) = %2$d\n", 2, fb1);
while (true)
{
i++;
fbn = fb1 + fb2 + fb3;
if (Integer.MAX_VALUE - fb1 > fb2) {
System.out.printf("F(%1$d) = %2$d\n", i, fbn);
}
else {
System.out.printf("F(%1$d) is out of the range of int\n", i);
break;
}
fb3 = fb2;
fb2 = fb1;
fb1 = fbn;
}
}
}