Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
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
 maximum number of digits
 

Functions

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.
 
int print_number (unsigned char *number, int N)
 Print a large number.
 
unsigned int get_digits (unsigned char *number)
 Get number of digits in a large number.
 
int main (int argc, char *argv[])
 Main function.
 

Detailed Description

Problem 25 solution implemented using arbitrarily large numbers represented as arrays

Author
Krishna Vedala

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 {
36 carry = 0;
37 }
38 // printf("= %d, %d\n", carry, c[i]);
39 }
40
41 while (carry != 0)
42 {
43 // printf("\t\t...adding new digit\n");
44 // printf("\t0 + %d + %d ", b[i], carry);
45 c[i] = carry + c[i];
46 if (c[i] > 9)
47 {
48 carry = 1;
49 c[i] -= 10;
50 }
51 else
52 {
53 carry = 0;
54 }
55 // printf("= %d, %d\n", carry, c[i]);
56 i++;
57 }
58 return i;
59}

◆ get_digits()

unsigned int get_digits ( unsigned char *  number)

Get number of digits in a large number.

76{
77 unsigned int digits = MAX_DIGITS;
78 while (number[digits] == 0) digits--;
79 return digits;
80}
#define MAX_DIGITS
maximum number of digits
Definition sol1.c:13

◆ main()

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

Main function.

84{
85 unsigned char
86 fn[MAX_DIGITS + 1]; /* array to store digits of a large number */
87 unsigned char fn1[MAX_DIGITS + 1];
88 unsigned char sum[MAX_DIGITS + 1];
89
90 memset(fn, 0, MAX_DIGITS);
91 memset(fn1, 0, MAX_DIGITS);
92 memset(sum, 0, MAX_DIGITS);
93
94 fn[0] = 1;
95 fn1[1] = 1;
96
97 unsigned int index = 1, digit_count = 1;
98
99 clock_t start_time = clock();
100 do
101 {
102 digit_count = add_numbers(fn, fn1, sum, digit_count);
103 // digit_count = get_digits(sum);
104
105 // printf("%5u (%u) (%u) ", index, digit_count, get_digits(sum));
106 // print_number(sum, digit_count);
107 // putchar('\n');
108
109 if (digit_count == MAX_DIGITS)
110 {
111 break;
112 }
113 memcpy(fn, fn1, MAX_DIGITS);
114 memcpy(fn1, sum, MAX_DIGITS);
115 index++;
116 } while (digit_count < MAX_DIGITS);
117 clock_t end_time = clock();
118
119 printf("Time taken: %.4g ms\n",
120 1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
121 printf("The nth term for %d digits: %u \n", MAX_DIGITS, index--);
122 print_number(sum, digit_count);
123
124 return 0;
125}
int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
Function to print a long number.
Definition sol1.c:92
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
Function to add arbitrary length decimal integers stored in an array.
Definition sol1.c:48
Here is the call graph for this function:

◆ print_number()

int print_number ( unsigned char *  number,
int  N 
)

Print a large number.

63{
64 int start_pos = N - 1;
65
66 /* skip all initial zeros */
67 while (number[start_pos] == 0) start_pos--;
68
69 for (int i = start_pos; i >= 0; i--) putchar(number[i] + 0x30);
70
71 return 0;
72}