This Programme returns the Nth fibonacci as a string.
More...
#include <iostream>
#include <cstring>
This Programme returns the Nth fibonacci as a string.
The method used is manual addition with carry and placing it in a string which is called string addition This makes it have no bounds or limits
- See also
- fibonacci_large.cpp, fibonacci_fast.cpp, fibonacci.cpp
◆ add()
function to add two string numbers
- Parameters
-
[in] | a | first number in string to add |
[in] | b | second number in string to add |
- Returns
- sum as a std::string
24 {
26
27
28 int carry = 0;
29
30
31 while (a.length() < b.
length()) {
32 a = "0" + a;
33 }
34
35
36 while (b.
length() < a.length()) {
37 b = "0" + b;
38 }
39
40
41 for (int i = a.length() - 1; i >= 0; i--) {
42 char val = static_cast<char>(((a[i] - 48) + (b[i] - 48)) + 48 + carry);
43 if (val > 57) {
44 carry = 1;
45 val -= 10;
46 } else {
47 carry = 0;
48 }
49 temp = val + temp;
50 }
51
52
53 if (carry == 1) {
54 temp = "1" + temp;
55 }
56
57
58 while (temp[0] ==
'0' && temp.
length() > 1) {
60 }
61
62 return temp;
63}
◆ fib_Accurate()
void fib_Accurate |
( |
uint64_t |
n | ) |
|
Fibonacci iterator
- Parameters
-
[in] | n | n^th Fibonacci number |
68 {
72 for (uint64_t i = 0; i < n; i++) {
73 tmp =
add(fibMinus1, fibMinus2);
74 fibMinus2 = fibMinus1;
75 fibMinus1 = tmp;
76 }
78}
std::string add(std::string a, std::string b)
Definition: string_fibonacci.cpp:24
◆ main()
main function
81 {
82 int n;
83 std::cout <<
"Enter whatever number N you want to find the fibonacci of\n";
87
88 return 0;
89}
void fib_Accurate(uint64_t n)
Definition: string_fibonacci.cpp:68