[ create a new paste ] login | about

Link: http://codepad.org/QKDjiOV3    [ raw code | output | fork | 1 comment ]

mohit_at_codepad - C++, pasted on Jun 6:
/// Calculate F(N+1) from F(N) where
///   F(N) is Nth Fibonacci number
///   N > 10
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

/**
 * @brief Calculate next Fibonacci number in series from
 * previous Fibonacci.
 *
 * @param [in] lastFibonacci F(N) for N > 10
 * @ret F(N+1)
 *
 * int_type should be integral type and not real.
 * If int_type is some big number type, it should provide
 * initialization from type double.
 */
template<typename int_type>
int_type findNextFibonacci(int_type lastFibonacci)
{
  const double goldenRatio = 0.5 * sqrt(5.0) + 0.5;  // FIXME: Change type to something more precise
  return static_cast<int_type>(0.5 + goldenRatio * lastFibonacci);
}

static void testFindNextFibonacciFunctionFor(int number, int expected) {
  const int actual = findNextFibonacci(number);
  int len = 10;
  if(actual != expected) {
    cout << " *** ERROR *** ";
    len = 5;
  }
  cout << "Input = "      << setw(len) << number
       << ", Expected = " << setw(len) << expected
       << ", Actual = "   << setw(len) << actual
       << endl;
}

int main() {
  const int fibsize = 47;
  int fibs[fibsize] = {0, 1};
  for(int i = 2; i < fibsize; ++i) {
    fibs[i] = fibs[i-1] + fibs[i-2];
  }
  for(int i = 1; i < fibsize; ++i) {
    testFindNextFibonacciFunctionFor(fibs[i-1], fibs[i]);
  }

  // Let's test whether it works for Lucas numbers or not
  cout << endl;
  fibs[0] = 2;
  fibs[1] = 1;
  for(int i = 2; i < fibsize-2; ++i) {
    fibs[i] = fibs[i-1] + fibs[i-2];
  }
  for(int i = 1; i < fibsize-2; ++i) {
    testFindNextFibonacciFunctionFor(fibs[i-1], fibs[i]);
  }
  
  return 0;
}


Output:
 *** ERROR *** Input =     0, Expected =     1, Actual =     0
 *** ERROR *** Input =     1, Expected =     1, Actual =     2
Input =          1, Expected =          2, Actual =          2
Input =          2, Expected =          3, Actual =          3
Input =          3, Expected =          5, Actual =          5
Input =          5, Expected =          8, Actual =          8
Input =          8, Expected =         13, Actual =         13
Input =         13, Expected =         21, Actual =         21
Input =         21, Expected =         34, Actual =         34
Input =         34, Expected =         55, Actual =         55
Input =         55, Expected =         89, Actual =         89
Input =         89, Expected =        144, Actual =        144
Input =        144, Expected =        233, Actual =        233
Input =        233, Expected =        377, Actual =        377
Input =        377, Expected =        610, Actual =        610
Input =        610, Expected =        987, Actual =        987
Input =        987, Expected =       1597, Actual =       1597
Input =       1597, Expected =       2584, Actual =       2584
Input =       2584, Expected =       4181, Actual =       4181
Input =       4181, Expected =       6765, Actual =       6765
Input =       6765, Expected =      10946, Actual =      10946
Input =      10946, Expected =      17711, Actual =      17711
Input =      17711, Expected =      28657, Actual =      28657
Input =      28657, Expected =      46368, Actual =      46368
Input =      46368, Expected =      75025, Actual =      75025
Input =      75025, Expected =     121393, Actual =     121393
Input =     121393, Expected =     196418, Actual =     196418
Input =     196418, Expected =     317811, Actual =     317811
Input =     317811, Expected =     514229, Actual =     514229
Input =     514229, Expected =     832040, Actual =     832040
Input =     832040, Expected =    1346269, Actual =    1346269
Input =    1346269, Expected =    2178309, Actual =    2178309
Input =    2178309, Expected =    3524578, Actual =    3524578
Input =    3524578, Expected =    5702887, Actual =    5702887
Input =    5702887, Expected =    9227465, Actual =    9227465
Input =    9227465, Expected =   14930352, Actual =   14930352
Input =   14930352, Expected =   24157817, Actual =   24157817
Input =   24157817, Expected =   39088169, Actual =   39088169
Input =   39088169, Expected =   63245986, Actual =   63245986
Input =   63245986, Expected =  102334155, Actual =  102334155
Input =  102334155, Expected =  165580141, Actual =  165580141
Input =  165580141, Expected =  267914296, Actual =  267914296
Input =  267914296, Expected =  433494437, Actual =  433494437
Input =  433494437, Expected =  701408733, Actual =  701408733
Input =  701408733, Expected = 1134903170, Actual = 1134903170
Input = 1134903170, Expected = 1836311903, Actual = 1836311903

 *** ERROR *** Input =     2, Expected =     1, Actual =     3
 *** ERROR *** Input =     1, Expected =     3, Actual =     2
 *** ERROR *** Input =     3, Expected =     4, Actual =     5
 *** ERROR *** Input =     4, Expected =     7, Actual =     6
Input =          7, Expected =         11, Actual =         11
Input =         11, Expected =         18, Actual =         18
Input =         18, Expected =         29, Actual =         29
Input =         29, Expected =         47, Actual =         47
Input =         47, Expected =         76, Actual =         76
Input =         76, Expected =        123, Actual =        123
Input =        123, Expected =        199, Actual =        199
Input =        199, Expected =        322, Actual =        322
Input =        322, Expected =        521, Actual =        521
Input =        521, Expected =        843, Actual =        843
Input =        843, Expected =       1364, Actual =       1364
Input =       1364, Expected =       2207, Actual =       2207
Input =       2207, Expected =       3571, Actual =       3571
Input =       3571, Expected =       5778, Actual =       5778
Input =       5778, Expected =       9349, Actual =       9349
Input =       9349, Expected =      15127, Actual =      15127
Input =      15127, Expected =      24476, Actual =      24476
Input =      24476, Expected =      39603, Actual =      39603
Input =      39603, Expected =      64079, Actual =      64079
Input =      64079, Expected =     103682, Actual =     103682
Input =     103682, Expected =     167761, Actual =     167761
Input =     167761, Expected =     271443, Actual =     271443
Input =     271443, Expected =     439204, Actual =     439204
Input =     439204, Expected =     710647, Actual =     710647
Input =     710647, Expected =    1149851, Actual =    1149851
Input =    1149851, Expected =    1860498, Actual =    1860498
Input =    1860498, Expected =    3010349, Actual =    3010349
Input =    3010349, Expected =    4870847, Actual =    4870847
Input =    4870847, Expected =    7881196, Actual =    7881196
Input =    7881196, Expected =   12752043, Actual =   12752043
Input =   12752043, Expected =   20633239, Actual =   20633239
Input =   20633239, Expected =   33385282, Actual =   33385282
Input =   33385282, Expected =   54018521, Actual =   54018521
Input =   54018521, Expected =   87403803, Actual =   87403803
Input =   87403803, Expected =  141422324, Actual =  141422324
Input =  141422324, Expected =  228826127, Actual =  228826127
Input =  228826127, Expected =  370248451, Actual =  370248451
Input =  370248451, Expected =  599074578, Actual =  599074578
Input =  599074578, Expected =  969323029, Actual =  969323029
Input =  969323029, Expected = 1568397607, Actual = 1568397607


Create a new paste based on this one


Comments:
posted by mohit_at_codepad on Jun 6
== Error in Fibonacci numbers ==
For N = 0, Error by value = 1, Error by percentage = 100
For N = 1, Error by value = 1, Error by percentage = 100
For N > 1, Error by value = 0, Error by percentage = 0
Sum of signed error = 0

== Error in Lucas numbers ==
For N = 0, Error by value = 2, Error by percentage = 200
For N = 1, Error by value = 1, Error by percentage = 33
For N = 2, Error by value = 1, Error by percentage = 25
For N = 3, Error by value = 1, Error by percentage = 15
Sum of signed error = 1
reply