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

Problem 25 solution implemented using arbitrarily large numbers represented as arrays More...

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Include dependency graph for sol1.c:

Macros

#define MAX_DIGITS   1000
 

Functions

unsigned int add_numbers (unsigned char *a, unsigned char *b, unsigned char *c, int N)
 
int print_number (unsigned char *number, int N)
 
unsigned int get_digits (unsigned char *number)
 
int main (int argc, char *argv[])
 

Detailed Description

Problem 25 solution implemented using arbitrarily large numbers represented as arrays

Author
Krishna Vedala

Macro Definition Documentation

◆ MAX_DIGITS

#define MAX_DIGITS   1000

maximum number of digits

Function Documentation

◆ add_numbers()

unsigned int add_numbers ( unsigned char *  a,
unsigned char *  b,
unsigned char *  c,
int  N 
)

Function to add arbitraty length decimal integers stored in an array.
a + b = c = new b

21 {
22  unsigned char carry = 0;
23  unsigned int i;
24 
25  for (i = 0; i < N; i++)
26  {
27  // printf("\t%d + %d + %d ", a[i], b[i], carry);
28  c[i] = carry + a[i] + b[i];
29  if (c[i] > 9) /* check for carry */
30  {
31  carry = 1;
32  c[i] -= 10;
33  }
34  else
35  carry = 0;
36  // printf("= %d, %d\n", carry, c[i]);
37  }
38 
39  while (carry != 0)
40  {
41  // printf("\t\t...adding new digit\n");
42  // printf("\t0 + %d + %d ", b[i], carry);
43  c[i] = carry + c[i];
44  if (c[i] > 9)
45  {
46  carry = 1;
47  c[i] -= 10;
48  }
49  else
50  carry = 0;
51  // printf("= %d, %d\n", carry, c[i]);
52  i++;
53  }
54  return i;
55 }

◆ get_digits()

unsigned int get_digits ( unsigned char *  number)

Get number of digits in a large number

72 {
73  unsigned int digits = MAX_DIGITS;
74  while (number[digits] == 0) digits--;
75  return digits;
76 }

◆ main()

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

Main function

80 {
81  unsigned char
82  fn[MAX_DIGITS + 1]; /* array to store digits of a large number */
83  unsigned char fn1[MAX_DIGITS + 1];
84  unsigned char sum[MAX_DIGITS + 1];
85 
86  memset(fn, 0, MAX_DIGITS);
87  memset(fn1, 0, MAX_DIGITS);
88  memset(sum, 0, MAX_DIGITS);
89 
90  fn[0] = 1;
91  fn1[1] = 1;
92 
93  unsigned int index = 1, digit_count = 1;
94 
95  clock_t start_time = clock();
96  do
97  {
98  digit_count = add_numbers(fn, fn1, sum, digit_count);
99  // digit_count = get_digits(sum);
100 
101  // printf("%5u (%u) (%u) ", index, digit_count, get_digits(sum));
102  // print_number(sum, digit_count);
103  // putchar('\n');
104 
105  if (digit_count == MAX_DIGITS)
106  break;
107  memcpy(fn, fn1, MAX_DIGITS);
108  memcpy(fn1, sum, MAX_DIGITS);
109  index++;
110  } while (digit_count < MAX_DIGITS);
111  clock_t end_time = clock();
112 
113  printf("Time taken: %.4g ms\n",
114  1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
115  printf("The nth term for %d digits: %u \n", MAX_DIGITS, index--);
116  print_number(sum, digit_count);
117 
118  return 0;
119 }
Here is the call graph for this function:

◆ print_number()

int print_number ( unsigned char *  number,
int  N 
)

Print a large number

59 {
60  int start_pos = N - 1;
61 
62  /* skip all initial zeros */
63  while (number[start_pos] == 0) start_pos--;
64 
65  for (int i = start_pos; i >= 0; i--) putchar(number[i] + 0x30);
66 
67  return 0;
68 }
N
#define N
Definition: sol1.c:109
add_numbers
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
Definition: sol1.c:44
MAX_DIGITS
#define MAX_DIGITS
Definition: sol1.c:13
print_number
int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
Definition: sol1.c:82