Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs, Tests, Fix: Fit fibonacci_large.cpp to contributing guideline #2909

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
111 changes: 59 additions & 52 deletions math/fibonacci_large.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
/**

Check notice on line 1 in math/fibonacci_large.cpp

View workflow job for this annotation

GitHub Actions / Code Formatter

Run clang-format on math/fibonacci_large.cpp

File math/fibonacci_large.cpp does not conform to Custom style guidelines. (lines 15)
* @file
* @brief Computes N^th Fibonacci number given as
* input argument. Uses custom build arbitrary integers library
* to perform additions and other operations.
*
* @details
* Took 0.608246 seconds to compute 50,000^th Fibonacci
* number that contains 10450 digits!
*
* \author [Krishna Vedala](https://github.com/kvedala)
* @author [Krishna Vedala](https://github.com/kvedala)
* @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
*/

#include <cinttypes>
#include <ctime>
#include <iostream>
#include <cinttypes> /// for uint64_t
#include <ctime> /// for clock_t, std::clock, CLOCKS_PER_SEC
#include <iostream> /// for IO operations
#include <stdexcept> /// for std::invalid_argument
#include <cassert> /// for assert

#include "./large_number.h"
#include "./large_number.h" /// for large_number class and related functions

/** Compute fibonacci numbers using the relation
* \f[f(n)=f(n-1)+f(n-2)\f]
* and returns the result as a large_number type.
/**
* @brief Function to compute the n-th Fibonacci number.
* @details \f[f(n)=f(n-1)+f(n-2)\f]
* @param n The position in the Fibonacci sequence.
* @return The n-th Fibonacci number.
*/
large_number fib(uint64_t n) {
large_number f0(1);
Expand All @@ -35,51 +40,53 @@
return f1;
}

int main(int argc, char *argv[]) {
uint64_t N;
if (argc == 2) {
N = strtoull(argv[1], NULL, 10);
} else {
std::cout << "Enter N: ";
std::cin >> N;
}

clock_t start_time = std::clock();
large_number result = fib(N);
clock_t end_time = std::clock();
double time_taken = static_cast<double>(end_time - start_time) /
static_cast<double>(CLOCKS_PER_SEC);

std::cout << std::endl
<< N << "^th Fibonacci number: " << result << std::endl
<< "Number of digits: " << result.num_digits() << std::endl
<< "Time taken: " << std::scientific << time_taken << " s"
<< std::endl;
/**
* @brief Test function to validate the fib function.
* @details This function tests the fib function by comparing its result for a
* specific input (5000) with a precomputed expected result.
*/
void test() {
uint64_t N = 5000;
large_number expected_result(
"387896845438832563370191630832590531208212771464624510616059721489"
"555013904403709701082291646221066947929345285888297381348310200895"
"498294036143015691147893836421656394410691021450563413370655865623"
"825465670071252592990385493381392883637834751890876297071203333705"
"292310769300851809384980180384781399674888176555465378829164426891"
"298038461377896902150229308247566634622492307188332480328037503913"
"035290330450584270114763524227021093463769910400671417488329842289"
"149127310405432875329804427367682297724498774987455569190770388063"
"704683279481135897373999311010621930814901857081539785437919530561"
"751076105307568878376603366735544525884488624161921055345749367589"
"784902798823435102359984466393485325641195222185956306047536464547"
"076033090242080638258492915645287629157575914234380914230291749108"
"898415520985443248659407979357131684169286803954530954538869811466"
"508206686289742063932343848846524098874239587380197699382031717420"
"893226546887936400263079778005875912967138963421425257911687275560"
"0360311370547754724604639987588046985178408674382863125");

N = 5000;
if (fib(N) ==
large_number(
"387896845438832563370191630832590531208212771464624510616059721489"
"555013904403709701082291646221066947929345285888297381348310200895"
"498294036143015691147893836421656394410691021450563413370655865623"
"825465670071252592990385493381392883637834751890876297071203333705"
"292310769300851809384980180384781399674888176555465378829164426891"
"298038461377896902150229308247566634622492307188332480328037503913"
"035290330450584270114763524227021093463769910400671417488329842289"
"149127310405432875329804427367682297724498774987455569190770388063"
"704683279481135897373999311010621930814901857081539785437919530561"
"751076105307568878376603366735544525884488624161921055345749367589"
"784902798823435102359984466393485325641195222185956306047536464547"
"076033090242080638258492915645287629157575914234380914230291749108"
"898415520985443248659407979357131684169286803954530954538869811466"
"508206686289742063932343848846524098874239587380197699382031717420"
"893226546887936400263079778005875912967138963421425257911687275560"
"0360311370547754724604639987588046985178408674382863125"))
std::cout << "Test for " << N << "^th Fibonacci number passed!"
<< std::endl;
else
std::cerr << "Test for " << N << "^th Fibonacci number failed!"
<< std::endl;
try {
large_number result = fib(N);
if (result == expected_result) {
std::cout << "Test for " << N << "^th Fibonacci number passed!"
<< std::endl;
} else {
throw std::invalid_argument("Test for " + std::to_string(N) +
"^th Fibonacci number failed");
}
} catch (const std::invalid_argument& e) {
assert(e.what() == std::string("Test for " + std::to_string(N) +
"^th Fibonacci number failed"));
}
}

/**
* @brief Main function.
* @details This function is the entry point of the program. It calls the test
* function to validate the correctness of the fib function.
* @return 0 on successful program exit.
*/
int main() {
test();
return 0;
}
Loading