1 // Compute and print the n-th Fibonacci number.
3 // We work with integers and real numbers.
4 #include <cl_integer.h>
9 #include <cl_integer_io.h>
11 // We use the timing functions.
12 #include <cl_timing.h>
14 // Declare the exit() function.
17 // F_n is defined through the recurrence relation
18 // F_0 = 0, F_1 = 1, F_(n+2) = F_(n+1) + F_n.
19 // The following addition formula holds:
20 // F_(n+m) = F_(m-1) * F_n + F_m * F_(n+1) for m >= 1, n >= 0.
21 // (Proof: For fixed m, the LHS and the RHS satisfy the same recurrence
22 // w.r.t. n, and the initial values (n=0, n=1) agree. Hence all values agree.)
24 // F_(n+m+1) = F_m * F_n + F_(m+1) * F_(n+1) for m >= 0, n >= 0
25 // Now put in m = n, to get
26 // F_(2n) = (F_(n+1)-F_n) * F_n + F_n * F_(n+1) = F_n * (2*F_(n+1) - F_n)
27 // F_(2n+1) = F_n ^ 2 + F_(n+1) ^ 2
29 // F_(2n+2) = F_(n+1) * (2*F_n + F_(n+1))
35 twofibs (const cl_I& uu, const cl_I& vv) : u (uu), v (vv) {}
38 // Returns F_n and F_(n+1). Assume n>=0.
39 static const twofibs fibonacci2 (int n)
43 int m = n/2; // floor(n/2)
44 twofibs Fm = fibonacci2(m);
45 // Since a squaring is cheaper than a multiplication, better use
46 // three squarings instead of one multiplication and two squarings.
47 cl_I u2 = square(Fm.u);
48 cl_I v2 = square(Fm.v);
51 cl_I uv2 = square(Fm.v - Fm.u);
52 return twofibs(v2 - uv2, u2 + v2);
55 cl_I uv2 = square(Fm.u + Fm.v);
56 return twofibs(u2 + v2, uv2 - u2);
60 // Returns just F_n. Assume n>=0.
61 const cl_I fibonacci (int n)
65 int m = n/2; // floor(n/2)
66 twofibs Fm = fibonacci2(m);
69 // Here we don't use the squaring formula because
70 // one multiplication is cheaper than two squarings.
73 return u * ((v << 1) - u);
76 cl_I u2 = square(Fm.u);
77 cl_I v2 = square(Fm.v);
82 // Returns just F_n, computed as the nearest integer to
83 // ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
84 const cl_I fibonacci_slow (int n)
86 // Need a precision of ((1+sqrt(5))/2)^-n.
87 cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
88 cl_R sqrt5 = sqrt(cl_float(5,prec));
89 cl_R phi = (1+sqrt5)/2;
90 return round1( expt(phi,n)/sqrt5 );
95 int main (int argc, char* argv[])
98 fprint(cl_stderr, "Usage: fibonacci n\n");
101 int n = atoi(argv[1]);
102 fprint(cl_stdout, "fib(");
103 fprintdecimal(cl_stdout, n);
104 fprint(cl_stdout, ") = ");
105 fprint(cl_stdout, fibonacci(n));
106 fprint(cl_stdout, "\n");
111 int main (int argc, char* argv[])
114 if ((argc >= 3) && !strcmp(argv[1],"-r")) {
115 repetitions = atoi(argv[2]);
116 argc -= 2; argv += 2;
119 fprint(cl_stderr, "Usage: fibonacci n\n");
122 int n = atoi(argv[1]);
124 fprint(cl_stdout, "fib(");
125 fprintdecimal(cl_stdout, n);
126 fprint(cl_stdout, ") = ");
127 for (int rep = repetitions-1; rep > 0; rep--)
129 fprint(cl_stdout, fibonacci(n));
130 fprint(cl_stdout, "\n");
133 fprint(cl_stdout, "fib(");
134 fprintdecimal(cl_stdout, n);
135 fprint(cl_stdout, ") = ");
136 for (int rep = repetitions-1; rep > 0; rep--)
138 fprint(cl_stdout, fibonacci_slow(n));
139 fprint(cl_stdout, "\n");