2020-06-05 20:53:38 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief [Problem 25](https://projecteuler.net/problem=25) solution implemented
|
|
|
|
* using arbitrarily large numbers represented as arrays
|
2020-06-06 21:51:49 +03:00
|
|
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
2020-06-05 20:53:38 +03:00
|
|
|
*/
|
2020-04-03 15:10:28 +03:00
|
|
|
#include <stdint.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdio.h>
|
2020-04-03 02:59:31 +03:00
|
|
|
#include <stdlib.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <string.h>
|
2020-04-03 02:59:31 +03:00
|
|
|
#include <time.h>
|
|
|
|
|
2020-06-05 20:53:38 +03:00
|
|
|
#define MAX_DIGITS 1000 /**< maximum number of digits */
|
2020-04-03 02:59:31 +03:00
|
|
|
|
|
|
|
/**
|
2020-06-05 20:53:38 +03:00
|
|
|
* Function to add arbitraty length decimal integers stored in an array.\n
|
2020-04-03 02:59:31 +03:00
|
|
|
* a + b = c = new b
|
2020-06-28 22:18:52 +03:00
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
|
|
|
|
int N)
|
2020-04-03 02:59:31 +03:00
|
|
|
{
|
2020-04-07 07:37:24 +03:00
|
|
|
unsigned char carry = 0;
|
2020-04-03 02:59:31 +03:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < N; i++)
|
|
|
|
{
|
|
|
|
// printf("\t%d + %d + %d ", a[i], b[i], carry);
|
|
|
|
c[i] = carry + a[i] + b[i];
|
|
|
|
if (c[i] > 9) /* check for carry */
|
|
|
|
{
|
|
|
|
carry = 1;
|
|
|
|
c[i] -= 10;
|
|
|
|
}
|
|
|
|
else
|
2020-07-13 06:49:09 +03:00
|
|
|
{
|
2020-04-03 02:59:31 +03:00
|
|
|
carry = 0;
|
2020-07-13 06:49:09 +03:00
|
|
|
}
|
2020-04-03 02:59:31 +03:00
|
|
|
// printf("= %d, %d\n", carry, c[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (carry != 0)
|
|
|
|
{
|
|
|
|
// printf("\t\t...adding new digit\n");
|
|
|
|
// printf("\t0 + %d + %d ", b[i], carry);
|
|
|
|
c[i] = carry + c[i];
|
|
|
|
if (c[i] > 9)
|
|
|
|
{
|
|
|
|
carry = 1;
|
|
|
|
c[i] -= 10;
|
|
|
|
}
|
|
|
|
else
|
2020-07-13 06:49:09 +03:00
|
|
|
{
|
2020-04-03 02:59:31 +03:00
|
|
|
carry = 0;
|
2020-07-13 06:49:09 +03:00
|
|
|
}
|
2020-04-03 02:59:31 +03:00
|
|
|
// printf("= %d, %d\n", carry, c[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:38 +03:00
|
|
|
/** Print a large number */
|
2020-04-07 07:37:24 +03:00
|
|
|
int print_number(unsigned char *number, int N)
|
2020-04-03 02:59:31 +03:00
|
|
|
{
|
|
|
|
int start_pos = N - 1;
|
|
|
|
|
|
|
|
/* skip all initial zeros */
|
2020-06-28 18:25:37 +03:00
|
|
|
while (number[start_pos] == 0) start_pos--;
|
2020-04-03 02:59:31 +03:00
|
|
|
|
2020-06-28 18:25:37 +03:00
|
|
|
for (int i = start_pos; i >= 0; i--) putchar(number[i] + 0x30);
|
2020-04-03 02:59:31 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:38 +03:00
|
|
|
/** Get number of digits in a large number */
|
2020-04-07 07:37:24 +03:00
|
|
|
unsigned int get_digits(unsigned char *number)
|
2020-04-03 02:59:31 +03:00
|
|
|
{
|
|
|
|
unsigned int digits = MAX_DIGITS;
|
2020-06-28 18:25:37 +03:00
|
|
|
while (number[digits] == 0) digits--;
|
2020-04-03 02:59:31 +03:00
|
|
|
return digits;
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:38 +03:00
|
|
|
/** Main function */
|
2020-04-03 02:59:31 +03:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
unsigned char
|
|
|
|
fn[MAX_DIGITS + 1]; /* array to store digits of a large number */
|
2020-04-07 07:37:24 +03:00
|
|
|
unsigned char fn1[MAX_DIGITS + 1];
|
|
|
|
unsigned char sum[MAX_DIGITS + 1];
|
2020-04-03 02:59:31 +03:00
|
|
|
|
|
|
|
memset(fn, 0, MAX_DIGITS);
|
|
|
|
memset(fn1, 0, MAX_DIGITS);
|
|
|
|
memset(sum, 0, MAX_DIGITS);
|
|
|
|
|
|
|
|
fn[0] = 1;
|
|
|
|
fn1[1] = 1;
|
|
|
|
|
|
|
|
unsigned int index = 1, digit_count = 1;
|
|
|
|
|
|
|
|
clock_t start_time = clock();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digit_count = add_numbers(fn, fn1, sum, digit_count);
|
|
|
|
// digit_count = get_digits(sum);
|
|
|
|
|
|
|
|
// printf("%5u (%u) (%u) ", index, digit_count, get_digits(sum));
|
|
|
|
// print_number(sum, digit_count);
|
|
|
|
// putchar('\n');
|
|
|
|
|
|
|
|
if (digit_count == MAX_DIGITS)
|
2020-07-13 06:49:09 +03:00
|
|
|
{
|
2020-04-03 02:59:31 +03:00
|
|
|
break;
|
2020-07-13 06:49:09 +03:00
|
|
|
}
|
2020-04-03 02:59:31 +03:00
|
|
|
memcpy(fn, fn1, MAX_DIGITS);
|
|
|
|
memcpy(fn1, sum, MAX_DIGITS);
|
|
|
|
index++;
|
|
|
|
} while (digit_count < MAX_DIGITS);
|
|
|
|
clock_t end_time = clock();
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Time taken: %.4g ms\n",
|
|
|
|
1e3 * (end_time - start_time) / CLOCKS_PER_SEC);
|
2020-04-03 02:59:31 +03:00
|
|
|
printf("The nth term for %d digits: %u \n", MAX_DIGITS, index--);
|
|
|
|
print_number(sum, digit_count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|