ernie.WPI.EDU> cat class17b.C // file class17b.C, cs2223, D97/98 // // Fibonacci sequence using analytical solution // 09Apr08 NW, First version // includes #include "headers.h" #include "Timer.h" // see Class 2 // definitions #define FIB_MAX 250 // prototype double fib(int); // Fibonacci sequence // main int main () { Timer *timer; timer = new Timer; for (int n = 0; n <= FIB_MAX; n++) { cout << "n = " << n << " fib(n) = " << fib(n) << endl; } cout << endl; delete timer; // print out the time return 0; } // end main() double fib(int n) // Fibonacci sequence { const double sqrt5 = sqrt(5.0); // constants const double phi1 = (1 + sqrt5) / 2.0; const double phi2 = (1 - sqrt5) / 2.0; return (pow(phi1,n) - pow(phi2,n)) / sqrt5; } // end fib() // Copyright 1998 Norman Wittels ernie.WPI.EDU> g++ class17b.C Timer.C -lm ernie.WPI.EDU> a.out n = 0 fib(n) = 0 n = 1 fib(n) = 1 n = 2 fib(n) = 1 n = 3 fib(n) = 2 n = 4 fib(n) = 3 n = 5 fib(n) = 5 n = 6 fib(n) = 8 n = 7 fib(n) = 13 n = 8 fib(n) = 21 n = 9 fib(n) = 34 n = 10 fib(n) = 55 n = 11 fib(n) = 89 n = 12 fib(n) = 144 n = 13 fib(n) = 233 n = 14 fib(n) = 377 n = 15 fib(n) = 610 n = 16 fib(n) = 987 n = 17 fib(n) = 1597 n = 18 fib(n) = 2584 n = 19 fib(n) = 4181 n = 20 fib(n) = 6765 n = 21 fib(n) = 10946 n = 22 fib(n) = 17711 n = 23 fib(n) = 28657 n = 24 fib(n) = 46368 n = 25 fib(n) = 75025 n = 26 fib(n) = 121393 n = 27 fib(n) = 196418 n = 28 fib(n) = 317811 n = 29 fib(n) = 514229 n = 30 fib(n) = 832040 n = 31 fib(n) = 1.34627e+06 n = 32 fib(n) = 2.17831e+06 n = 33 fib(n) = 3.52458e+06 n = 34 fib(n) = 5.70289e+06 n = 35 fib(n) = 9.22747e+06 n = 36 fib(n) = 1.49304e+07 n = 37 fib(n) = 2.41578e+07 n = 38 fib(n) = 3.90882e+07 n = 39 fib(n) = 6.3246e+07 n = 40 fib(n) = 1.02334e+08 n = 41 fib(n) = 1.6558e+08 n = 42 fib(n) = 2.67914e+08 n = 43 fib(n) = 4.33494e+08 n = 44 fib(n) = 7.01409e+08 n = 45 fib(n) = 1.1349e+09 n = 46 fib(n) = 1.83631e+09 n = 47 fib(n) = 2.97122e+09 n = 48 fib(n) = 4.80753e+09 n = 49 fib(n) = 7.77874e+09 n = 50 fib(n) = 1.25863e+10 n = 51 fib(n) = 2.0365e+10 n = 52 fib(n) = 3.29513e+10 n = 53 fib(n) = 5.33163e+10 n = 54 fib(n) = 8.62676e+10 n = 55 fib(n) = 1.39584e+11 n = 56 fib(n) = 2.25851e+11 n = 57 fib(n) = 3.65435e+11 n = 58 fib(n) = 5.91287e+11 n = 59 fib(n) = 9.56722e+11 n = 60 fib(n) = 1.54801e+12 n = 61 fib(n) = 2.50473e+12 n = 62 fib(n) = 4.05274e+12 n = 63 fib(n) = 6.55747e+12 n = 64 fib(n) = 1.06102e+13 n = 65 fib(n) = 1.71677e+13 n = 66 fib(n) = 2.77779e+13 n = 67 fib(n) = 4.49456e+13 n = 68 fib(n) = 7.27235e+13 n = 69 fib(n) = 1.17669e+14 n = 70 fib(n) = 1.90392e+14 n = 71 fib(n) = 3.08062e+14 n = 72 fib(n) = 4.98454e+14 n = 73 fib(n) = 8.06516e+14 n = 74 fib(n) = 1.30497e+15 n = 75 fib(n) = 2.11149e+15 n = 76 fib(n) = 3.41645e+15 n = 77 fib(n) = 5.52794e+15 n = 78 fib(n) = 8.94439e+15 n = 79 fib(n) = 1.44723e+16 n = 80 fib(n) = 2.34167e+16 n = 81 fib(n) = 3.78891e+16 n = 82 fib(n) = 6.13058e+16 n = 83 fib(n) = 9.91949e+16 n = 84 fib(n) = 1.60501e+17 n = 85 fib(n) = 2.59695e+17 n = 86 fib(n) = 4.20196e+17 n = 87 fib(n) = 6.79892e+17 n = 88 fib(n) = 1.10009e+18 n = 89 fib(n) = 1.77998e+18 n = 90 fib(n) = 2.88007e+18 n = 91 fib(n) = 4.66005e+18 n = 92 fib(n) = 7.54011e+18 n = 93 fib(n) = 1.22002e+19 n = 94 fib(n) = 1.97403e+19 n = 95 fib(n) = 3.19404e+19 n = 96 fib(n) = 5.16807e+19 n = 97 fib(n) = 8.36211e+19 n = 98 fib(n) = 1.35302e+20 n = 99 fib(n) = 2.18923e+20 n = 100 fib(n) = 3.54225e+20 n = 101 fib(n) = 5.73148e+20 n = 102 fib(n) = 9.27373e+20 n = 103 fib(n) = 1.50052e+21 n = 104 fib(n) = 2.42789e+21 n = 105 fib(n) = 3.92841e+21 n = 106 fib(n) = 6.35631e+21 n = 107 fib(n) = 1.02847e+22 n = 108 fib(n) = 1.6641e+22 n = 109 fib(n) = 2.69257e+22 n = 110 fib(n) = 4.35668e+22 n = 111 fib(n) = 7.04925e+22 n = 112 fib(n) = 1.14059e+23 n = 113 fib(n) = 1.84552e+23 n = 114 fib(n) = 2.98611e+23 n = 115 fib(n) = 4.83163e+23 n = 116 fib(n) = 7.81774e+23 n = 117 fib(n) = 1.26494e+24 n = 118 fib(n) = 2.04671e+24 n = 119 fib(n) = 3.31165e+24 n = 120 fib(n) = 5.35836e+24 n = 121 fib(n) = 8.67001e+24 n = 122 fib(n) = 1.40284e+25 n = 123 fib(n) = 2.26984e+25 n = 124 fib(n) = 3.67267e+25 n = 125 fib(n) = 5.94251e+25 n = 126 fib(n) = 9.61519e+25 n = 127 fib(n) = 1.55577e+26 n = 128 fib(n) = 2.51729e+26 n = 129 fib(n) = 4.07306e+26 n = 130 fib(n) = 6.59035e+26 n = 131 fib(n) = 1.06634e+27 n = 132 fib(n) = 1.72538e+27 n = 133 fib(n) = 2.79172e+27 n = 134 fib(n) = 4.51709e+27 n = 135 fib(n) = 7.30881e+27 n = 136 fib(n) = 1.18259e+28 n = 137 fib(n) = 1.91347e+28 n = 138 fib(n) = 3.09606e+28 n = 139 fib(n) = 5.00953e+28 n = 140 fib(n) = 8.10559e+28 n = 141 fib(n) = 1.31151e+29 n = 142 fib(n) = 2.12207e+29 n = 143 fib(n) = 3.43358e+29 n = 144 fib(n) = 5.55565e+29 n = 145 fib(n) = 8.98924e+29 n = 146 fib(n) = 1.45449e+30 n = 147 fib(n) = 2.35341e+30 n = 148 fib(n) = 3.8079e+30 n = 149 fib(n) = 6.16131e+30 n = 150 fib(n) = 9.96922e+30 n = 151 fib(n) = 1.61305e+31 n = 152 fib(n) = 2.60997e+31 n = 153 fib(n) = 4.22303e+31 n = 154 fib(n) = 6.833e+31 n = 155 fib(n) = 1.1056e+32 n = 156 fib(n) = 1.7889e+32 n = 157 fib(n) = 2.89451e+32 n = 158 fib(n) = 4.68341e+32 n = 159 fib(n) = 7.57792e+32 n = 160 fib(n) = 1.22613e+33 n = 161 fib(n) = 1.98392e+33 n = 162 fib(n) = 3.21006e+33 n = 163 fib(n) = 5.19398e+33 n = 164 fib(n) = 8.40404e+33 n = 165 fib(n) = 1.3598e+34 n = 166 fib(n) = 2.20021e+34 n = 167 fib(n) = 3.56001e+34 n = 168 fib(n) = 5.76021e+34 n = 169 fib(n) = 9.32022e+34 n = 170 fib(n) = 1.50804e+35 n = 171 fib(n) = 2.44007e+35 n = 172 fib(n) = 3.94811e+35 n = 173 fib(n) = 6.38817e+35 n = 174 fib(n) = 1.03363e+36 n = 175 fib(n) = 1.67245e+36 n = 176 fib(n) = 2.70607e+36 n = 177 fib(n) = 4.37852e+36 n = 178 fib(n) = 7.08459e+36 n = 179 fib(n) = 1.14631e+37 n = 180 fib(n) = 1.85477e+37 n = 181 fib(n) = 3.00108e+37 n = 182 fib(n) = 4.85585e+37 n = 183 fib(n) = 7.85694e+37 n = 184 fib(n) = 1.27128e+38 n = 185 fib(n) = 2.05697e+38 n = 186 fib(n) = 3.32825e+38 n = 187 fib(n) = 5.38522e+38 n = 188 fib(n) = 8.71347e+38 n = 189 fib(n) = 1.40987e+39 n = 190 fib(n) = 2.28122e+39 n = 191 fib(n) = 3.69109e+39 n = 192 fib(n) = 5.9723e+39 n = 193 fib(n) = 9.66339e+39 n = 194 fib(n) = 1.56357e+40 n = 195 fib(n) = 2.52991e+40 n = 196 fib(n) = 4.09348e+40 n = 197 fib(n) = 6.62339e+40 n = 198 fib(n) = 1.07169e+41 n = 199 fib(n) = 1.73403e+41 n = 200 fib(n) = 2.80571e+41 n = 201 fib(n) = 4.53974e+41 n = 202 fib(n) = 7.34545e+41 n = 203 fib(n) = 1.18852e+42 n = 204 fib(n) = 1.92306e+42 n = 205 fib(n) = 3.11158e+42 n = 206 fib(n) = 5.03465e+42 n = 207 fib(n) = 8.14623e+42 n = 208 fib(n) = 1.31809e+43 n = 209 fib(n) = 2.13271e+43 n = 210 fib(n) = 3.4508e+43 n = 211 fib(n) = 5.58351e+43 n = 212 fib(n) = 9.0343e+43 n = 213 fib(n) = 1.46178e+44 n = 214 fib(n) = 2.36521e+44 n = 215 fib(n) = 3.82699e+44 n = 216 fib(n) = 6.1922e+44 n = 217 fib(n) = 1.00192e+45 n = 218 fib(n) = 1.62114e+45 n = 219 fib(n) = 2.62306e+45 n = 220 fib(n) = 4.2442e+45 n = 221 fib(n) = 6.86726e+45 n = 222 fib(n) = 1.11115e+46 n = 223 fib(n) = 1.79787e+46 n = 224 fib(n) = 2.90902e+46 n = 225 fib(n) = 4.70689e+46 n = 226 fib(n) = 7.61591e+46 n = 227 fib(n) = 1.23228e+47 n = 228 fib(n) = 1.99387e+47 n = 229 fib(n) = 3.22615e+47 n = 230 fib(n) = 5.22002e+47 n = 231 fib(n) = 8.44617e+47 n = 232 fib(n) = 1.36662e+48 n = 233 fib(n) = 2.21124e+48 n = 234 fib(n) = 3.57786e+48 n = 235 fib(n) = 5.78909e+48 n = 236 fib(n) = 9.36695e+48 n = 237 fib(n) = 1.5156e+49 n = 238 fib(n) = 2.4523e+49 n = 239 fib(n) = 3.9679e+49 n = 240 fib(n) = 6.4202e+49 n = 241 fib(n) = 1.03881e+50 n = 242 fib(n) = 1.68083e+50 n = 243 fib(n) = 2.71964e+50 n = 244 fib(n) = 4.40047e+50 n = 245 fib(n) = 7.12011e+50 n = 246 fib(n) = 1.15206e+51 n = 247 fib(n) = 1.86407e+51 n = 248 fib(n) = 3.01613e+51 n = 249 fib(n) = 4.8802e+51 n = 250 fib(n) = 7.89633e+51 Process 0 took 0.016666 seconds ernie.WPI.EDU>