4
4
* input argument. Uses custom build arbitrary integers library
5
5
* to perform additions and other operations.
6
6
*
7
+ * @details
7
8
* Took 0.608246 seconds to compute 50,000^th Fibonacci
8
9
* number that contains 10450 digits!
9
10
*
10
- * \ author [Krishna Vedala](https://github.com/kvedala)
11
+ * @ author [Krishna Vedala](https://github.com/kvedala)
11
12
* @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
12
13
*/
13
14
14
- #include < cinttypes>
15
- #include < ctime>
16
- #include < iostream>
15
+ #include < cinttypes> // / for uint64_t
16
+ #include < ctime> // / for clock_t, std::clock, CLOCKS_PER_SEC
17
+ #include < iostream> // / for IO operations
18
+ #include < stdexcept> // / for std::invalid_argument
19
+ #include < cassert> // / for assert
17
20
18
- #include " ./large_number.h"
21
+ #include " ./large_number.h" // / for large_number class and related functions
19
22
20
- /* * Compute fibonacci numbers using the relation
21
- * \f[f(n)=f(n-1)+f(n-2)\f]
22
- * and returns the result as a large_number type.
23
+ /* *
24
+ * @brief Function to compute the n-th Fibonacci number.
25
+ * @details \f[f(n)=f(n-1)+f(n-2)\f]
26
+ * @param n The position in the Fibonacci sequence.
27
+ * @return The n-th Fibonacci number.
23
28
*/
24
29
large_number fib (uint64_t n) {
25
30
large_number f0 (1 );
@@ -35,51 +40,53 @@ large_number fib(uint64_t n) {
35
40
return f1;
36
41
}
37
42
38
- int main (int argc, char *argv[]) {
39
- uint64_t N;
40
- if (argc == 2 ) {
41
- N = strtoull (argv[1 ], NULL , 10 );
42
- } else {
43
- std::cout << " Enter N: " ;
44
- std::cin >> N;
45
- }
46
-
47
- clock_t start_time = std::clock ();
48
- large_number result = fib (N);
49
- clock_t end_time = std::clock ();
50
- double time_taken = static_cast <double >(end_time - start_time) /
51
- static_cast <double >(CLOCKS_PER_SEC);
52
-
53
- std::cout << std::endl
54
- << N << " ^th Fibonacci number: " << result << std::endl
55
- << " Number of digits: " << result.num_digits () << std::endl
56
- << " Time taken: " << std::scientific << time_taken << " s"
57
- << std::endl;
43
+ /* *
44
+ * @brief Test function to validate the fib function.
45
+ * @details This function tests the fib function by comparing its result for a
46
+ * specific input (5000) with a precomputed expected result.
47
+ */
48
+ void test () {
49
+ uint64_t N = 5000 ;
50
+ large_number expected_result (
51
+ " 387896845438832563370191630832590531208212771464624510616059721489"
52
+ " 555013904403709701082291646221066947929345285888297381348310200895"
53
+ " 498294036143015691147893836421656394410691021450563413370655865623"
54
+ " 825465670071252592990385493381392883637834751890876297071203333705"
55
+ " 292310769300851809384980180384781399674888176555465378829164426891"
56
+ " 298038461377896902150229308247566634622492307188332480328037503913"
57
+ " 035290330450584270114763524227021093463769910400671417488329842289"
58
+ " 149127310405432875329804427367682297724498774987455569190770388063"
59
+ " 704683279481135897373999311010621930814901857081539785437919530561"
60
+ " 751076105307568878376603366735544525884488624161921055345749367589"
61
+ " 784902798823435102359984466393485325641195222185956306047536464547"
62
+ " 076033090242080638258492915645287629157575914234380914230291749108"
63
+ " 898415520985443248659407979357131684169286803954530954538869811466"
64
+ " 508206686289742063932343848846524098874239587380197699382031717420"
65
+ " 893226546887936400263079778005875912967138963421425257911687275560"
66
+ " 0360311370547754724604639987588046985178408674382863125" );
58
67
59
- N = 5000 ;
60
- if (fib (N) ==
61
- large_number (
62
- " 387896845438832563370191630832590531208212771464624510616059721489"
63
- " 555013904403709701082291646221066947929345285888297381348310200895"
64
- " 498294036143015691147893836421656394410691021450563413370655865623"
65
- " 825465670071252592990385493381392883637834751890876297071203333705"
66
- " 292310769300851809384980180384781399674888176555465378829164426891"
67
- " 298038461377896902150229308247566634622492307188332480328037503913"
68
- " 035290330450584270114763524227021093463769910400671417488329842289"
69
- " 149127310405432875329804427367682297724498774987455569190770388063"
70
- " 704683279481135897373999311010621930814901857081539785437919530561"
71
- " 751076105307568878376603366735544525884488624161921055345749367589"
72
- " 784902798823435102359984466393485325641195222185956306047536464547"
73
- " 076033090242080638258492915645287629157575914234380914230291749108"
74
- " 898415520985443248659407979357131684169286803954530954538869811466"
75
- " 508206686289742063932343848846524098874239587380197699382031717420"
76
- " 893226546887936400263079778005875912967138963421425257911687275560"
77
- " 0360311370547754724604639987588046985178408674382863125" ))
78
- std::cout << " Test for " << N << " ^th Fibonacci number passed!"
79
- << std::endl;
80
- else
81
- std::cerr << " Test for " << N << " ^th Fibonacci number failed!"
82
- << std::endl;
68
+ try {
69
+ large_number result = fib (N);
70
+ if (result == expected_result) {
71
+ std::cout << " Test for " << N << " ^th Fibonacci number passed!"
72
+ << std::endl;
73
+ } else {
74
+ throw std::invalid_argument (" Test for " + std::to_string (N) +
75
+ " ^th Fibonacci number failed" );
76
+ }
77
+ } catch (const std::invalid_argument& e) {
78
+ assert (e.what () == std::string (" Test for " + std::to_string (N) +
79
+ " ^th Fibonacci number failed" ));
80
+ }
81
+ }
83
82
83
+ /* *
84
+ * @brief Main function.
85
+ * @details This function is the entry point of the program. It calls the test
86
+ * function to validate the correctness of the fib function.
87
+ * @return 0 on successful program exit.
88
+ */
89
+ int main () {
90
+ test ();
84
91
return 0 ;
85
92
}
0 commit comments