Skip to content

Commit 144442a

Browse files
niranjank2022CascadingCascadePanquesito7
authored
[feat/docs]: improve the Fibonacci algorithm (#1232)
* Improve the documentation of factorial.c * Improve the documentation of fibonacci.c * Update starting terms of fibonacci as 0 and 1 * Update math/fibonacci.c Co-authored-by: Sharon "Cass" Cassidy <[email protected]> * docs: Documenting the code * test: Add test * fix: fix the test expression Co-authored-by: Sharon "Cass" Cassidy <[email protected]> * feat: Restrict non-integer inputs * fix: Change atoi() to sscanf() * fix: Change atoi() to sscanf() * fix: scanf() to getInput() * fix: while, continue and break Co-authored-by: Sharon "Cass" Cassidy <[email protected]> * fix: Increase buffer size * fix: Doesn't accept lengthy characters * fix: Accepts empty characters * fix: fibonacci.c Co-authored-by: Sharon "Cass" Cassidy <[email protected]> * feat: Add wikipedia link * feat: Add author Co-authored-by: David Leal <[email protected]> * feat: Record time duration of function execution * chore: apply suggestions from code review Co-authored-by: Sharon "Cass" Cassidy <[email protected]> * chore: apply suggestions from code review Co-authored-by: Sharon "Cass" Cassidy <[email protected]> --------- Co-authored-by: Sharon "Cass" Cassidy <[email protected]> Co-authored-by: David Leal <[email protected]> Co-authored-by: Sharon "Cass" Cassidy <[email protected]>
1 parent b5b2218 commit 144442a

File tree

1 file changed

+113
-12
lines changed

1 file changed

+113
-12
lines changed

math/fibonacci.c

+113-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,124 @@
1-
#include <stdio.h>
1+
/**
2+
* @file
3+
* @brief Program to print the nth term of the Fibonacci series.
4+
* @details
5+
* Fibonacci series generally starts from 0 and 1. Every next term in
6+
* the series is equal to the sum of the two preceding terms.
7+
* For further info: https://en.wikipedia.org/wiki/Fibonacci_sequence
8+
*
9+
* @author [Luiz Carlos Aguiar C](https://github.com/IKuuhakuI)
10+
* @author [Niranjan](https://github.com/niranjank2022)
11+
*/
212

3-
// Fibonnacci function
4-
int fib(int number)
13+
#include <assert.h> /// for assert()
14+
#include <errno.h> /// for errno - to determine whether there is an error while using strtol()
15+
#include <stdio.h> /// for input, output
16+
#include <stdlib.h> /// for exit() - to exit the program
17+
#include <time.h> /// to calculate time taken by fib()
18+
/**
19+
* @brief Determines the nth Fibonacci term
20+
* @param number - n in "nth term" and it can't be negative as well as zero
21+
* @return nth term in unsigned type
22+
* @warning
23+
* Only till 47th and 48th fibonacci element can be stored in `int` and
24+
* `unsigned int` respectively (takes more than 20 seconds to print)
25+
*/
26+
unsigned int fib(int number)
527
{
6-
if (number == 1 || number == 2)
28+
// Check for negative integers
29+
if (number <= 0)
30+
{
31+
fprintf(stderr, "Illegal Argument Is Passed!\n");
32+
exit(EXIT_FAILURE);
33+
}
34+
35+
// Base conditions
36+
if (number == 1)
37+
return 0;
38+
39+
if (number == 2)
740
return 1;
8-
else
9-
return fib(number - 1) + fib(number - 2);
41+
42+
// Recursive call to the function
43+
return fib(number - 1) + fib(number - 2);
1044
}
1145

46+
/**
47+
* @brief Get the input from the user
48+
* @return valid argument to the fibonacci function
49+
*/
50+
int getInput(void)
51+
{
52+
int num, excess_len;
53+
char buffer[3], *endPtr;
54+
55+
while (1)
56+
{ // Repeat until a valid number is entered
57+
printf("Please enter a valid number:");
58+
fgets(buffer, 3, stdin); // Inputs the value from user
59+
60+
excess_len = 0;
61+
if (!(buffer[0] == '\n' ||
62+
buffer[1] == '\n' ||
63+
buffer[2] == '\n')) {
64+
while (getchar() != '\n') excess_len++;
65+
}
66+
67+
num = strtol(buffer, &endPtr,
68+
10); // Attempts to convert the string to integer
69+
70+
// Checking the input
71+
if ( // The number is too large
72+
(excess_len > 0 || num > 48) ||
73+
// Characters other than digits are included in the input
74+
(*endPtr != '\0' && *endPtr != '\n') ||
75+
// No characters are entered
76+
endPtr == buffer)
77+
{
78+
continue;
79+
}
80+
81+
break;
82+
}
83+
84+
printf("\nEntered digit: %d (it might take sometime)\n", num);
85+
return num;
86+
}
87+
88+
/**
89+
* @brief self-test implementation
90+
* @return void
91+
*/
92+
static void test()
93+
{
94+
assert(fib(5) == 3);
95+
assert(fib(2) == 1);
96+
assert(fib(9) == 21);
97+
}
98+
99+
/**
100+
* @brief Main function
101+
* @return 0 on exit
102+
*/
12103
int main()
13104
{
14-
int number;
105+
// Performing the test
106+
test();
107+
printf("Tests passed...\n");
15108

16-
// Asks for the number that is in n position in Fibonnacci sequence
17-
printf("Number: ");
18-
scanf("%d", &number);
109+
// Getting n
110+
printf(
111+
"Enter n to find nth fibonacci element...\n"
112+
"Note: You would be asked to enter input until valid number ( less "
113+
"than or equal to 48 ) is entered.\n");
19114

20-
printf("%d \n", fib(number));
115+
int number = getInput();
116+
clock_t start, end;
21117

118+
start = clock();
119+
printf("Fibonacci element %d is %u ", number, fib(number));
120+
end = clock();
121+
122+
printf("in %.3f seconds.\n", ((double)(end - start)) / CLOCKS_PER_SEC );
22123
return 0;
23-
}
124+
}

0 commit comments

Comments
 (0)