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

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

◆ main()

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

Main function

83 {
84  unsigned char
85  fn[MAX_DIGITS + 1]; /* array to store digits of a large number */
86  unsigned char fn1[MAX_DIGITS + 1];
87  unsigned char sum[MAX_DIGITS + 1];
88 
89  memset(fn, 0, MAX_DIGITS);
90  memset(fn1, 0, MAX_DIGITS);
91  memset(sum, 0, MAX_DIGITS);
92 
93  fn[0] = 1;
94  fn1[1] = 1;
95 
96  unsigned int index = 1, digit_count = 1;
97 
98  clock_t start_time = clock();
99  do
100  {
101  digit_count = add_numbers(fn, fn1, sum, digit_count);
102  // digit_count = get_digits(sum);
103 
104  // printf("%5u (%u) (%u) ", index, digit_count, get_digits(sum));
105  // print_number(sum, digit_count);
106  // putchar('\n');
107 
108  if (digit_count == MAX_DIGITS)
109  break;
110  memcpy(fn, fn1, MAX_DIGITS);
111  memcpy(fn1, sum, MAX_DIGITS);
112  index++;
113  } while (digit_count < MAX_DIGITS);
114  clock_t end_time = clock();
115 
116  printf("Time taken: %.4g ms\n",
117  1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
118  printf("The nth term for %d digits: %u \n", MAX_DIGITS, index--);
119  print_number(sum, digit_count);
120 
121  return 0;
122 }
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)
64  start_pos--;
65 
66  for (int i = start_pos; i >= 0; i--)
67  putchar(number[i] + 0x30);
68 
69  return 0;
70 }
N
#define N
Definition: sol1.c:111
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