 |
Java
1.0
|
Go to the documentation of this file.
24 private long[]
fibs =
null;
47 public static void main(String[] args) {
49 if (args.length > 0) {
51 n = Integer.parseInt(args[0]);
52 }
catch (NumberFormatException e) {
53 System.err.println(
"Argument must be an integer");
60 System.out.println(f2);
61 System.out.println(f);
62 System.out.println(String.format(
"%12s %12s %12s %12s",
"N",
"Fib(N)",
"Dumb",
"Smart"));
63 System.out.println(String.format(
"%12s %12s %12s %12s",
"-------",
"-------",
"-------",
"-------"));
64 for (
int i = 1; i <= n; ++i ) {
67 long dumb_count = f.
ncalls;
68 long smart_result = f.
fib(i);
69 long smart_count = f.
ncalls;
70 System.out.println (String.format (
"%12d %12d %12d %12d", i, smart_result, dumb_count, smart_count));
80 long n0 = 0, n1 = 1, n2;
81 System.out.print(n0 +
" " + n1 +
" ");
83 for (
int i = 0; i < n-2; i++) {
85 System.out.print(n2 +
" ");
89 System.out.println(
"\n");
102 if ( n <= 1 )
return n;
129 private long fib (
int n,
long s1,
long s2) {
134 return fib(n-1,s1+s2,s1);
147 if ( n <= 1 )
return n;
150 if (
fibs ==
null ||
fibs.length <= n )
151 fibs =
new long [n+1];
174 if (
fibs !=
null ) {
175 for (
int i = 0; i <
fibs.length; ++i )
176 s += Long.toString (
fibs[i]) +
" ";
177 s += String.format(
"\nNumber of calls = %d\n",
ncalls);
int nf
hold the number of terms to be generated
Class for generating the Fibonacci sequence.
long fib(int n, long s1, long s2)
Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls.
long Fibo(int n)
Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls.
long ncalls
hold the number of recursive calls
long fib(int n)
Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls.
long[] fibs
hold the Fibonacci sequence
Fibonacci(int n)
Constructs a Fibonacci sequence from 0 to n-1.
static void main(String[] args)
Main function for testing.
String toString()
Used to print a Fibonacci object.
void fibo(int n)
Prints the Fibonacci sequence.
long fiboDumb(int n)
This is a naive recursive version.