This Programme returns the Nth fibonacci as a string.
More...
#include <cstdint>
#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
25 {
27
28
29 int carry = 0;
30
31
32 while (a.length() < b.
length()) {
33 a = "0" + a;
34 }
35
36
37 while (b.
length() < a.length()) {
38 b = "0" + b;
39 }
40
41
42 for (int i = a.length() - 1; i >= 0; i--) {
43 char val = static_cast<char>(((a[i] - 48) + (b[i] - 48)) + 48 + carry);
44 if (val > 57) {
45 carry = 1;
46 val -= 10;
47 } else {
48 carry = 0;
49 }
50 temp = val + temp;
51 }
52
53
54 if (carry == 1) {
55 temp = "1" + temp;
56 }
57
58
59 while (temp[0] ==
'0' && temp.
length() > 1) {
61 }
62
63 return temp;
64}
◆ fib_Accurate()
| void fib_Accurate |
( |
uint64_t | n | ) |
|
Fibonacci iterator
- Parameters
-
| [in] | n | n^th Fibonacci number |
69 {
73 for (uint64_t i = 0; i < n; i++) {
74 tmp =
add(fibMinus1, fibMinus2);
75 fibMinus2 = fibMinus1;
76 fibMinus1 = tmp;
77 }
79}
std::string add(std::string a, std::string b)
Definition string_fibonacci.cpp:25
◆ main()
main function
82 {
83 int n;
84 std::cout <<
"Enter whatever number N you want to find the fibonacci of\n";
88
89 return 0;
90}
void fib_Accurate(uint64_t n)
Definition string_fibonacci.cpp:69