dynamically large factorial computation

This commit is contained in:
Krishna Vedala 2020-04-28 18:29:36 -04:00
parent 447b1160a4
commit f0465347ae
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
2 changed files with 113 additions and 67 deletions

View File

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
/**
Returns the \f$n^{th}\f$ and \f$n+1^{th}\f$ Fibonacci number.
The return variables are C & D respectively.
*/
void fib(unsigned long n, unsigned long *C, unsigned long *D)
{
//Out of Range checking
if (n < 0)
{
printf("\nNo Such term !\n");
exit(0);
}
unsigned long a, b, c, d;
if (n == 0)
{
C[0] = 0;
if (D)
D[0] = 1;
return;
}
fib(n >> 1, &c, &d); /**< Compute F(n/2) */
a = c * ((d << 1) - c);
b = c * c + d * d;
if (n % 2 == 0) /**< If n is even */
{
C[0] = a;
if (D)
D[0] = b;
return;
}
/**< If n is odd */
C[0] = b;
if (D)
D[0] = a + b;
return;
}
int main(int argc, char *argv[])
{
unsigned long number, result;
setlocale(LC_NUMERIC, ""); // format the printf output
//Asks for the number/position of term in Fibonnacci sequence
if (argc == 2)
number = atoi(argv[1]);
else
{
printf("Enter the value of n(n starts from 0 ): ");
scanf("%lu", &number);
}
fib(number, &result, NULL);
printf("The nth term is : %'lu \n", result);
return 0;
}

View File

@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "function_timer.h"
/**
* dynamically large number
**/
typedef struct _large_num
{
char *digits; /**< array to store individual digits */
unsigned int num_digits; /**< number of digits in the number */
} large_num;
/**
* create a new large number
**/
large_num *new_number(void)
{
large_num *new_num = (large_num *)malloc(sizeof(large_num));
new_num->num_digits = 1;
new_num->digits = (char *)malloc(1 * sizeof(char));
new_num->digits[0] = 1;
return new_num;
}
/**
* delete all memory allocated for large number
**/
void delete_number(large_num *num)
{
free(num->digits);
free(num);
}
/**
* add a digit to the large number
**/
void add_digit(large_num *num, unsigned int value)
{
if (value > 9)
{
fprintf(stderr, "digit > 9!!\n");
delete_number(num);
exit(EXIT_FAILURE);
}
num->num_digits++;
num->digits = (char *)realloc(num->digits, num->num_digits * sizeof(char));
num->digits[num->num_digits - 1] = value;
}
/**
* multiply large number with another integer and
* store the result in the same large number
**/
void multiply(large_num *num, unsigned long n)
{
int i;
unsigned long carry = 0, temp;
for (i = 0; i < num->num_digits; i++)
{
temp = num->digits[i] * n;
temp += carry;
if (temp < 10)
carry = 0;
else
{
carry = temp / 10;
temp = temp % 10;
}
num->digits[i] = temp;
}
while (carry != 0)
{
add_digit(num, carry % 10);
carry /= 10;
}
}
int main(int argc, char *argv[])
{
int number, i;
//Asks for the number/position of term in Fibonnacci sequence
if (argc == 2)
number = atoi(argv[1]);
else
{
printf("Enter the value of n(n starts from 0 ): ");
scanf("%d", &number);
}
large_num *result = new_number();
function_timer *timer = new_timer();
clock_t start_time = clock();
start_timer(timer);
for (i = 2; i <= number; i++)
/* Multiply every number from 2 thru N */
multiply(result, i);
double time_taken = end_timer_delete(timer) * (double)1e3;
// time_taken = (clock() - start_time) / (double) CLOCKS_PER_SEC;
printf("%d! = ", number);
for (i = result->num_digits; i > 0; i--)
putchar(result->digits[i - 1] + '0');
printf("\nTime taken: %.4g ms\n", time_taken);
delete_number(result);
return 0;
}