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
 dynamically large number More...
 

Typedefs

typedef struct _large_num large_num
 dynamically large number
 

Functions

large_numnew_number (void)
 create a new large number More...
 
void delete_number (large_num *num)
 delete all memory allocated for large number More...
 
void add_digit (large_num *num, unsigned int value)
 add a digit to the large number More...
 
void multiply (large_num *num, unsigned long n)
 multiply large number with another integer and store the result in the same large number
 
int main (int argc, char *argv[])
 main function
 

Detailed Description

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

Author
Krishna Vedala

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 }

◆ 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
dynamically large number
Definition: factorial_large_number.c:15
_large_num::num_digits
unsigned int num_digits
number of digits in the number
Definition: factorial_large_number.c:17
delete_number
void delete_number(large_num *num)
delete all memory allocated for large number
Definition: factorial_large_number.c:37
_large_num::digits
char * digits
array to store individual digits
Definition: factorial_large_number.c:16