diff --git a/math/fibonacci.c b/math/fibonacci.c index 3af48962..60752e7a 100644 --- a/math/fibonacci.c +++ b/math/fibonacci.c @@ -1,23 +1,124 @@ -#include +/** + * @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 -int fib(int number) +#include /// for assert() +#include /// for errno - to determine whether there is an error while using strtol() +#include /// for input, output +#include /// for exit() - to exit the program +#include /// 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; - else - return fib(number - 1) + fib(number - 2); + + // Recursive call to the function + 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 number; + // Performing the test + test(); + printf("Tests passed...\n"); - // Asks for the number that is in n position in Fibonnacci sequence - printf("Number: "); - scanf("%d", &number); + // Getting n + printf( + "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; -} \ No newline at end of file +}