Compute factorial of arbitrarily large numbers by storing individual digits in a byte.
More...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Compute factorial of arbitrarily large numbers by storing individual digits in a byte.
- Author
- Krishna Vedala
◆ large_num
◆ add_digit()
void add_digit |
( |
large_num * |
num, |
|
|
unsigned int |
value |
|
) |
| |
add a digit to the large number
- Parameters
-
[in,out] | num | |
[in] | value | value of the digit to insert |
52 fprintf(stderr,
"digit > 9!!\n");
◆ delete_number()
delete all memory allocated for large number
- Parameters
-
[in] | num | pointer to large_num to delete |
◆ main()
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
main function
100 number = atoi(argv[1]);
103 printf(
"Enter the value of n(n starts from 0 ): ");
104 scanf(
"%d", &number);
109 clock_t start_time = clock();
110 for (i = 2; i <= number; i++)
112 double time_taken = (clock() - start_time) * (
double)1e3 / CLOCKS_PER_SEC;
115 printf(
"%d! = ", number);
117 putchar(result->
digits[i - 1] +
'0');
118 printf(
"\nTime taken: %.4g ms\n", time_taken);
◆ multiply()
void multiply |
( |
large_num * |
num, |
|
|
unsigned long |
n |
|
) |
| |
multiply large number with another integer and store the result in the same large number
69 unsigned long carry = 0, temp;
◆ new_number()
create a new large number
- Returns
- pointer to a large number
28 new_num->
digits = (
char *)malloc(1 *
sizeof(
char));