From ab0649797bb2f3b7bfbb26de1667484f03495a55 Mon Sep 17 00:00:00 2001 From: Naveen Hegde Date: Tue, 26 Dec 2017 17:46:50 +0530 Subject: [PATCH 1/2] Added String Fibonacci This programme uses String Addition to find Nth fibonacci. --- Others/String Fibonacci.cpp | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Others/String Fibonacci.cpp diff --git a/Others/String Fibonacci.cpp b/Others/String Fibonacci.cpp new file mode 100644 index 000000000..4032f1e16 --- /dev/null +++ b/Others/String Fibonacci.cpp @@ -0,0 +1,73 @@ +//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 + + +#include +#include + +using namespace std; + +std::string add(std::string a, std::string b) +{ + std::string temp = ""; + while ((int)a.length() < (int)b.length()) + { + a = "0" + a; + } + while ((int)b.length() < (int)a.length()) { + b = "0" + b; + } + int carry = 0; + for (int i = a.length() - 1; i >= 0; i--) + { + char val = (char)(((a[i] - 48) + (b[i] - 48)) + 48 + carry); + if (val > 57) + { + carry = 1; + val -= 10; + } + else + { + carry = 0; + } + temp = val + temp; + } + if (carry != 0) + { + temp = "1" + temp; + } + while (temp[0] == '0' && temp.length() != 1) + { + temp = temp.substr(1); + } + return temp; +} + +void fib_Accurate(long long n) +{ + std::string tmp = ""; + std::string fibMinus1 = "1"; + std::string fibMinus2 = "0"; + std::string comma = ", "; + for (long long i = 0; i < n; i++) + { + tmp = add(fibMinus1, fibMinus2); + + fibMinus2 = fibMinus1; + fibMinus1 = tmp; + } + std::cout << fibMinus2; +} + + +int main() +{ + int n; + cout << "Enter whatever number N you want to find the fibonacci of\n"; + cin >> n; + cout << n << " th Fibonacci is \n"; + fib_Accurate(n); + + return 0; +} From 22eab38352076cddd8d6f8dfa39e8cda6a7c8ea5 Mon Sep 17 00:00:00 2001 From: Naveen Hegde Date: Wed, 27 Dec 2017 23:24:38 +0530 Subject: [PATCH 2/2] Update String Fibonacci.cpp removed std:: --- Others/String Fibonacci.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Others/String Fibonacci.cpp b/Others/String Fibonacci.cpp index 4032f1e16..77571b93a 100644 --- a/Others/String Fibonacci.cpp +++ b/Others/String Fibonacci.cpp @@ -8,9 +8,9 @@ using namespace std; -std::string add(std::string a, std::string b) +string add(string a, string b) { - std::string temp = ""; + string temp = ""; while ((int)a.length() < (int)b.length()) { a = "0" + a; @@ -46,10 +46,9 @@ std::string add(std::string a, std::string b) void fib_Accurate(long long n) { - std::string tmp = ""; - std::string fibMinus1 = "1"; - std::string fibMinus2 = "0"; - std::string comma = ", "; + string tmp = ""; + string fibMinus1 = "1"; + string fibMinus2 = "0"; for (long long i = 0; i < n; i++) { tmp = add(fibMinus1, fibMinus2); @@ -57,7 +56,7 @@ void fib_Accurate(long long n) fibMinus2 = fibMinus1; fibMinus1 = tmp; } - std::cout << fibMinus2; + cout << fibMinus2; }