Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help printing out large integers. I need to print a 64bit int with my pro

ID: 3871084 • Letter: I

Question

I need help printing out large integers. I need to print a 64bit int with my program, and I thought I had the code correct to do that. This works to print the lower fibonacci numbers, but the large ones are not working. Please take a look and tell me what you think. Num in main is where you should be setting the fibonacci number.

#include<iostream>
#include<time.h>
#include<stdint.h>
#include<cstdint>
#include<chrono>

using namespace std;

long long fibonacci(long long num, int* fibCache);
int main()
{
uint64_t num =40;

int* fibCache = new int[num + 1];
fibCache[0] = 0;
fibCache[1] = 1;

for (int i = 2; i <= num; i++)
fibCache[i] = -1;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::cout << num << " is " << fibonacci(num, fibCache) << std::endl;
std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now();
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() <<std::endl;
delete [] fibCache;
return 0;
}

long long fibonacci(long long num, int* fibCache)
{

if (fibCache[num] == -1)
{

fibCache[num] = fibonacci(num - 1, fibCache) + fibonacci(num - 2, fibCache);
}

return fibCache[num];
}

Explanation / Answer

#include<iostream>
#include<time.h>
///#include<stdint.h>
//#include<cstdint>
//#include<chrono>
using namespace std;
unsigned long long fibonacci(unsigned long long num, unsigned long long* fibCache);
int main()
{
unsigned long long int num =1010;
unsigned long long* fibCache = new unsigned long long[num + 1];
fibCache[0] = 0;
fibCache[1] = 1;
for (unsigned long long i = 2; i <= num; i++)
fibCache[i] = -1;
// std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::cout << num << " is " << fibonacci(num, fibCache) << std::endl;
//std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now();
//std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() <<std::endl;
delete [] fibCache;
return 0;
}
unsigned long long fibonacci(unsigned long long num, unsigned long long* fibCache)
{
if (fibCache[num] == -1)
{
fibCache[num] = fibonacci(num - 1, fibCache) + fibonacci(num - 2, fibCache);
}
return fibCache[num];
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote