-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTribonacci.java
More file actions
52 lines (46 loc) · 1.44 KB
/
Tribonacci.java
File metadata and controls
52 lines (46 loc) · 1.44 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
/**
* Write a program called Tribonacci to display the first 20 Tribonacci numbers
* F(n), where F(n) = F(n–1) + F(n–2) + F(n–3) and T(1) = T(2) = 1, and T(3)=2.
* Also compute their average.
*/
package javaexercises.flowcontrol;
/**
*
* @author User
*/
public class Tribonacci {
public static void main(String[] args) {
Tribonacci aTribonacci = new Tribonacci();
aTribonacci.printTribonacciAndAverage(20);
}
public void printTribonacciAndAverage(int n)
{
long fb1 = 1;
long fb2 = 1;
long fb3 = 2;
long fbn = 0;
long sum = 0;
if (n <= 0) {
System.out.println("Please correct number of items and try again.");
return;
}
System.out.println("The first " + n + " Tribonacci numbers are:");
for(int i = 1; i <= n; i++) {
switch (i) {
case 1: fbn = fb1; break;
case 2: fbn = fb2; break;
case 3: fbn = fb3; break;
default:
fbn = fb1 + fb2 + fb3;
fb1 = fb2;
fb2 = fb3;
fb3 = fbn;
}
sum += fbn;
System.out.print(fbn + " ");
}
System.out.println();
System.out.printf("The sum is %1$d \n", sum);
System.out.printf("The average is %.4f \n", (double)sum / n);
}
}