string_fibonacci.cpp File Reference

This Programme returns the Nth fibonacci as a string. More...

`#include <iostream>`
`#include <cstring>`
Include dependency graph for string_fibonacci.cpp:

## Functions

std::string | add (std::string a, std::string b)
void | fib_Accurate (uint64_t n)
int | main ()

## Detailed Description

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

## Function Documentation

## add()

std::string add | ( | std::string | a,
|  | std::string | b
| ) |  |

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

{
std::string temp = "";
// carry flag
int carry = 0;
// fills up with zeros
while (a.length() < b.length()) {
a = "0" + a;
}
// fills up with zeros
while (b.length() < a.length()) {
b = "0" + b;
}
// adds the numbers a and b
for (int i = a.length() - 1; i >= 0; i--) {
char val = static_cast<char>(((a[i] - 48) + (b[i] - 48)) + 48 + carry);
if (val > 57) {
carry = 1;
val -= 10;
} else {
carry = 0;
}
temp = val + temp;
}
// processes the carry flag
if (carry == 1) {
temp = "1" + temp;
}
// removes leading zeros.
while (temp[0] == '0' && temp.length() > 1) {
temp = temp.substr(1);
}
return temp;
}
std::string
std::string::length
T length(T... args)
std::string::substr
T substr(T... args)
Here is the call graph for this function:

## fib_Accurate()

void fib_Accurate | ( | uint64_t | n | ) |

Fibonacci iterator

Parameters

  [in] | n | n^th Fibonacci number

{
std::string tmp = "";
std::string fibMinus1 = "1";
std::string fibMinus2 = "0";
for (uint64_t i = 0; i < n; i++) {
tmp = add(fibMinus1, fibMinus2);
fibMinus2 = fibMinus1;
fibMinus1 = tmp;
}
std::cout << fibMinus2;
}
std::cout
add
std::string add(std::string a, std::string b)
Definition: string_fibonacci.cpp:24
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

main function

{
int n;
std::cout << "Enter whatever number N you want to find the fibonacci of\n";
std::cin >> n;
std::cout << n << " th Fibonacci is \n";
fib_Accurate(n);
return 0;
}
std::cin
fib_Accurate
void fib_Accurate(uint64_t n)
Definition: string_fibonacci.cpp:68
Here is the call graph for this function: