Compute factorial of arbitrarily large numbers by storing individual digits in a byte.
More...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
typedef struct _large_num | large_num |
| dynamically large number
|
|
Compute factorial of arbitrarily large numbers by storing individual digits in a byte.
- Author
- Krishna Vedala
◆ 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");
void delete_number(large_num *num)
delete all memory allocated for large number
Definition: factorial_large_number.c:37
unsigned int num_digits
number of digits in the number
Definition: factorial_large_number.c:17
char * digits
array to store individual digits
Definition: factorial_large_number.c:16
◆ delete_number()
delete all memory allocated for large number
- Parameters
-
[in] | num | pointer to large_num to delete |
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
◆ 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);
large_num * new_number(void)
create a new large number
Definition: factorial_large_number.c:24
void multiply(large_num *num, unsigned long n)
multiply large number with another integer and store the result in the same large number
Definition: factorial_large_number.c:66
dynamically large number
Definition: factorial_large_number.c:15
◆ 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;
void add_digit(large_num *num, unsigned int value)
add a digit to the large number
Definition: factorial_large_number.c:48
◆ new_number()
create a new large number
- Returns
- pointer to a large number
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18