mirror of https://github.com/TheAlgorithms/C
[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 <122662061+CascadingCascade@users.noreply.github.com> * docs: Documenting the code * test: Add test * fix: fix the test expression Co-authored-by: Sharon "Cass" Cassidy <122662061+CascadingCascade@users.noreply.github.com> * 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 <122662061+CascadingCascade@users.noreply.github.com> * fix: Increase buffer size * fix: Doesn't accept lengthy characters * fix: Accepts empty characters * fix: fibonacci.c Co-authored-by: Sharon "Cass" Cassidy <122662061+CascadingCascade@users.noreply.github.com> * feat: Add wikipedia link * feat: Add author Co-authored-by: David Leal <halfpacho@gmail.com> * feat: Record time duration of function execution * chore: apply suggestions from code review Co-authored-by: Sharon "Cass" Cassidy <monadicdiffusive@proton.me> * chore: apply suggestions from code review Co-authored-by: Sharon "Cass" Cassidy <monadicdiffusive@proton.me> --------- Co-authored-by: Sharon "Cass" Cassidy <122662061+CascadingCascade@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: Sharon "Cass" Cassidy <monadicdiffusive@proton.me>
This commit is contained in:
parent
b5b2218e60
commit
144442afba
121
math/fibonacci.c
121
math/fibonacci.c
|
@ -1,23 +1,124 @@
|
||||||
#include <stdio.h>
|
/**
|
||||||
|
* @file
|
||||||
|
* @brief Program to print the nth term of the Fibonacci series.
|
||||||
|
* @details
|
||||||
|
* Fibonacci series generally starts from 0 and 1. Every next term in
|
||||||
|
* the series is equal to the sum of the two preceding terms.
|
||||||
|
* For further info: https://en.wikipedia.org/wiki/Fibonacci_sequence
|
||||||
|
*
|
||||||
|
* @author [Luiz Carlos Aguiar C](https://github.com/IKuuhakuI)
|
||||||
|
* @author [Niranjan](https://github.com/niranjank2022)
|
||||||
|
*/
|
||||||
|
|
||||||
// Fibonnacci function
|
#include <assert.h> /// for assert()
|
||||||
int fib(int number)
|
#include <errno.h> /// for errno - to determine whether there is an error while using strtol()
|
||||||
|
#include <stdio.h> /// for input, output
|
||||||
|
#include <stdlib.h> /// for exit() - to exit the program
|
||||||
|
#include <time.h> /// to calculate time taken by fib()
|
||||||
|
/**
|
||||||
|
* @brief Determines the nth Fibonacci term
|
||||||
|
* @param number - n in "nth term" and it can't be negative as well as zero
|
||||||
|
* @return nth term in unsigned type
|
||||||
|
* @warning
|
||||||
|
* Only till 47th and 48th fibonacci element can be stored in `int` and
|
||||||
|
* `unsigned int` respectively (takes more than 20 seconds to print)
|
||||||
|
*/
|
||||||
|
unsigned int fib(int number)
|
||||||
{
|
{
|
||||||
if (number == 1 || number == 2)
|
// Check for negative integers
|
||||||
|
if (number <= 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Illegal Argument Is Passed!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Base conditions
|
||||||
|
if (number == 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (number == 2)
|
||||||
return 1;
|
return 1;
|
||||||
else
|
|
||||||
|
// Recursive call to the function
|
||||||
return fib(number - 1) + fib(number - 2);
|
return fib(number - 1) + fib(number - 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the input from the user
|
||||||
|
* @return valid argument to the fibonacci function
|
||||||
|
*/
|
||||||
|
int getInput(void)
|
||||||
|
{
|
||||||
|
int num, excess_len;
|
||||||
|
char buffer[3], *endPtr;
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{ // Repeat until a valid number is entered
|
||||||
|
printf("Please enter a valid number:");
|
||||||
|
fgets(buffer, 3, stdin); // Inputs the value from user
|
||||||
|
|
||||||
|
excess_len = 0;
|
||||||
|
if (!(buffer[0] == '\n' ||
|
||||||
|
buffer[1] == '\n' ||
|
||||||
|
buffer[2] == '\n')) {
|
||||||
|
while (getchar() != '\n') excess_len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
num = strtol(buffer, &endPtr,
|
||||||
|
10); // Attempts to convert the string to integer
|
||||||
|
|
||||||
|
// Checking the input
|
||||||
|
if ( // The number is too large
|
||||||
|
(excess_len > 0 || num > 48) ||
|
||||||
|
// Characters other than digits are included in the input
|
||||||
|
(*endPtr != '\0' && *endPtr != '\n') ||
|
||||||
|
// No characters are entered
|
||||||
|
endPtr == buffer)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nEntered digit: %d (it might take sometime)\n", num);
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief self-test implementation
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static void test()
|
||||||
|
{
|
||||||
|
assert(fib(5) == 3);
|
||||||
|
assert(fib(2) == 1);
|
||||||
|
assert(fib(9) == 21);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main function
|
||||||
|
* @return 0 on exit
|
||||||
|
*/
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int number;
|
// Performing the test
|
||||||
|
test();
|
||||||
|
printf("Tests passed...\n");
|
||||||
|
|
||||||
// Asks for the number that is in n position in Fibonnacci sequence
|
// Getting n
|
||||||
printf("Number: ");
|
printf(
|
||||||
scanf("%d", &number);
|
"Enter n to find nth fibonacci element...\n"
|
||||||
|
"Note: You would be asked to enter input until valid number ( less "
|
||||||
|
"than or equal to 48 ) is entered.\n");
|
||||||
|
|
||||||
printf("%d \n", fib(number));
|
int number = getInput();
|
||||||
|
clock_t start, end;
|
||||||
|
|
||||||
|
start = clock();
|
||||||
|
printf("Fibonacci element %d is %u ", number, fib(number));
|
||||||
|
end = clock();
|
||||||
|
|
||||||
|
printf("in %.3f seconds.\n", ((double)(end - start)) / CLOCKS_PER_SEC );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue