Question: C++ Please Use your C++ Big Integer code to compute the sum of the first 300 Fibonacci. Hint - your answer will be a 63-digit
C++ Please
Use your C++ "Big Integer" code to compute the sum of the first 300 Fibonacci.
Hint - your answer will be a 63-digit number. Copy and Paste is your friend here.
*The number starts with 5.
I already tried all answers on chegg, and they are incorrect. So, please do not just copy and paste them.
Code to be modified to find the sum
Big integer code:
#include
using namespace std;
class bigInt {
public: string number; bigInt(string s = "") { number = s; }
bigInt operator +(bigInt other) { string a = this->number; string b = other.number; string res = ""; int i, j; int carry = 0; for (i = a.size() - 1, j = b.size() - 1; i >= 0 && j >= 0; i--, j--) {
int sum = (a[i] + b[j] - '0' - '0' + carry); res += (sum % 10 + '0'); carry = sum / 10; } while (i >= 0) {
int sum = (a[i] - '0' + carry); res += (sum % 10 + '0'); carry = sum / 10; i--; } while (j >= 0) {
int sum = (b[j] - '0' + carry); res += (sum % 10 + '0'); carry = sum / 10; j--; } if (carry > 0) { res += (carry + '0'); } reverse(res.begin(), res.end()); return bigInt(res); } };
bigInt arr[2000];
bigInt fib(int n) { if (n <= 2) { return bigInt("1"); }
if (arr[n - 1].number != "") { return arr[n - 1]; } arr[n - 1] = fib(n - 1) + fib(n - 2);
return arr[n - 1]; }
int main() { clock_t start, finish; double elapsedTime; start = clock();
int arr[6] = { 100, 250, 300, 400, 500, 1000 }; for (int i = 0; i < 6; i++) { cout << arr[i] << "The Fibonacci Number: " << fib(arr[i]).number << endl << endl; }
finish = clock(); elapsedTime = (double)(finish - start) / CLOCKS_PER_SEC; cout << "Time Taken: " << elapsedTime << "s" << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
