]> www.ginac.de Git - cln.git/blob - examples/fibonacci.cc
Initial revision
[cln.git] / examples / fibonacci.cc
1 // Compute and print the n-th Fibonacci number.
2
3 // We work with integers and real numbers.
4 #include <cl_integer.h>
5 #include <cl_real.h>
6
7 // We do I/O.
8 #include <cl_io.h>
9 #include <cl_integer_io.h>
10
11 // We use the timing functions.
12 #include <cl_timing.h>
13
14 // Declare the exit() function.
15 #include <stdlib.h>
16
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.)
23 // Replace m by m+1:
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
28 // hence
29 //      F_(2n+2) = F_(n+1) * (2*F_n + F_(n+1))
30
31 struct twofibs {
32         cl_I u; // F_n
33         cl_I v; // F_(n+1)
34         // Constructor.
35         twofibs (const cl_I& uu, const cl_I& vv) : u (uu), v (vv) {}
36 };
37
38 // Returns F_n and F_(n+1). Assume n>=0.
39 static const twofibs fibonacci2 (int n)
40 {
41         if (n==0)
42                 return twofibs(0,1);
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);
49         if (n==2*m) {
50                 // n = 2*m
51                 cl_I uv2 = square(Fm.v - Fm.u);
52                 return twofibs(v2 - uv2, u2 + v2);
53         } else {
54                 // n = 2*m+1
55                 cl_I uv2 = square(Fm.u + Fm.v);
56                 return twofibs(u2 + v2, uv2 - u2);
57         }
58 }
59
60 // Returns just F_n. Assume n>=0.
61 const cl_I fibonacci (int n)
62 {
63         if (n==0)
64                 return 0;
65         int m = n/2; // floor(n/2)
66         twofibs Fm = fibonacci2(m);
67         if (n==2*m) {
68                 // n = 2*m
69                 // Here we don't use the squaring formula because
70                 // one multiplication is cheaper than two squarings.
71                 cl_I& u = Fm.u;
72                 cl_I& v = Fm.v;
73                 return u * ((v << 1) - u);
74         } else {
75                 // n = 2*m+1
76                 cl_I u2 = square(Fm.u);
77                 cl_I v2 = square(Fm.v);
78                 return u2 + v2;
79         }
80 }
81
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)
85 {
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 );
91 }
92
93 #ifndef TIMING
94
95 int main (int argc, char* argv[])
96 {
97         if (argc != 2) {
98                 fprint(cl_stderr, "Usage: fibonacci n\n");
99                 exit(1);
100         }
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");
107 }
108
109 #else // TIMING
110
111 int main (int argc, char* argv[])
112 {
113         int repetitions = 1;
114         if ((argc >= 3) && !strcmp(argv[1],"-r")) {
115                 repetitions = atoi(argv[2]);
116                 argc -= 2; argv += 2;
117         }
118         if (argc != 2) {
119                 fprint(cl_stderr, "Usage: fibonacci n\n");
120                 exit(1);
121         }
122         int n = atoi(argv[1]);
123         { CL_TIMING;
124                 fprint(cl_stdout, "fib(");
125                 fprintdecimal(cl_stdout, n);
126                 fprint(cl_stdout, ") = ");
127                 for (int rep = repetitions-1; rep > 0; rep--)
128                         fibonacci(n);
129                 fprint(cl_stdout, fibonacci(n));
130                 fprint(cl_stdout, "\n");
131         }
132         { CL_TIMING;
133                 fprint(cl_stdout, "fib(");
134                 fprintdecimal(cl_stdout, n);
135                 fprint(cl_stdout, ") = ");
136                 for (int rep = repetitions-1; rep > 0; rep--)
137                         fibonacci_slow(n);
138                 fprint(cl_stdout, fibonacci_slow(n));
139                 fprint(cl_stdout, "\n");
140         }
141 }
142
143 #endif