Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
factorial_large_number.c File Reference

Compute factorial of arbitrarily large numbers by storing individual digits in a byte. More...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for factorial_large_number.c:

Data Structures

struct  _large_num
 

Typedefs

typedef struct _large_num large_num
 

Functions

large_numnew_number (void)
 
void delete_number (large_num *num)
 
void add_digit (large_num *num, unsigned int value)
 
void multiply (large_num *num, unsigned long n)
 
int main (int argc, char *argv[])
 

Detailed Description

Compute factorial of arbitrarily large numbers by storing individual digits in a byte.

Author
Krishna Vedala

Typedef Documentation

◆ large_num

typedef struct _large_num large_num

dynamically large number

Function Documentation

◆ add_digit()

void add_digit ( large_num num,
unsigned int  value 
)

add a digit to the large number

Parameters
[in,out]num
[in]valuevalue of the digit to insert
49 {
50  if (value > 9)
51  {
52  fprintf(stderr, "digit > 9!!\n");
53  delete_number(num);
54  exit(EXIT_FAILURE);
55  }
56 
57  num->num_digits++;
58  num->digits = (char *)realloc(num->digits, num->num_digits * sizeof(char));
59  num->digits[num->num_digits - 1] = value;
60 }
Here is the call graph for this function:

◆ delete_number()

void delete_number ( large_num num)

delete all memory allocated for large number

Parameters
[in]numpointer to large_num to delete
38 {
39  free(num->digits);
40  free(num);
41 }

◆ main()

int main ( int  argc,
char *  argv[] 
)

main function

95 {
96  int number, i;
97 
98  // Asks for the number/position of term in Fibonnacci sequence
99  if (argc == 2)
100  number = atoi(argv[1]);
101  else
102  {
103  printf("Enter the value of n(n starts from 0 ): ");
104  scanf("%d", &number);
105  }
106 
107  large_num *result = new_number();
108 
109  clock_t start_time = clock();
110  for (i = 2; i <= number; i++) /* Multiply every number from 2 thru N */
111  multiply(result, i);
112  double time_taken = (clock() - start_time) * (double)1e3 / CLOCKS_PER_SEC;
113  // time_taken = (clock() - start_time) / (double) CLOCKS_PER_SEC;
114 
115  printf("%d! = ", number);
116  for (i = result->num_digits; i > 0; i--)
117  putchar(result->digits[i - 1] + '0');
118  printf("\nTime taken: %.4g ms\n", time_taken);
119 
120  delete_number(result);
121  return 0;
122 }
Here is the call graph for this function:

◆ multiply()

void multiply ( large_num num,
unsigned long  n 
)

multiply large number with another integer and store the result in the same large number

67 {
68  int i;
69  unsigned long carry = 0, temp;
70  for (i = 0; i < num->num_digits; i++)
71  {
72  temp = num->digits[i] * n;
73  temp += carry;
74  if (temp < 10)
75  carry = 0;
76  else
77  {
78  carry = temp / 10;
79  temp = temp % 10;
80  }
81  num->digits[i] = temp;
82  }
83 
84  while (carry != 0)
85  {
86  add_digit(num, carry % 10);
87  carry /= 10;
88  }
89 }
Here is the call graph for this function:

◆ new_number()

large_num* new_number ( void  )

create a new large number

Returns
pointer to a large number
25 {
26  large_num *new_num = (large_num *)malloc(sizeof(large_num));
27  new_num->num_digits = 1;
28  new_num->digits = (char *)malloc(1 * sizeof(char));
29  new_num->digits[0] = 1;
30  return new_num;
31 }
_large_num
Definition: factorial_large_number.c:15
_large_num::num_digits
unsigned int num_digits
Definition: factorial_large_number.c:17
new_number
large_num * new_number(void)
Definition: factorial_large_number.c:24
multiply
void multiply(large_num *num, unsigned long n)
Definition: factorial_large_number.c:66
delete_number
void delete_number(large_num *num)
Definition: factorial_large_number.c:37
_large_num::digits
char * digits
Definition: factorial_large_number.c:16
add_digit
void add_digit(large_num *num, unsigned int value)
Definition: factorial_large_number.c:48