-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecurse.java
More file actions
40 lines (34 loc) · 872 Bytes
/
Copy pathRecurse.java
File metadata and controls
40 lines (34 loc) · 872 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
public class Recurse {
static long steps = 0;
public static int Sum(int n) {
if (n == 0) {
return 0;
}
return(n + Sum(n-1));
}
public static void printInt(long n) {
if (n >= 10)
printInt(n/10);
System.out.print( (char) ('0' + n % 10));
}
public static long nthFib(int n) {
if (n <= 1) {
return n;
} else {
steps += 2;
return nthFib(n-1) + nthFib(n-2);
}
}
public static void main (String[] args) {
// int s = Sum(10);
// System.out.println("Sum of first 10 digits is " + s);
// printInt(23005448321l);
// System.out.println();
for (int i = 1; i < 15; i++) {
steps = 0;
System.out.println("Fib " + i + " is " + nthFib(i) + "\n\tIt took " + steps + " steps.");
}
// int i = 50;
// System.out.println("Fib " + i + " is " + nthFib(i) + "\n\tIt took " + steps + " steps.");
}
}