mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 14:59:36 +03:00
Merge branch 'project_euler/problem_16' into project_euler/master
# Conflicts: # DIRECTORY.md
This commit is contained in:
commit
6ff4ac36d8
55
project_euler/Problem 16/sol1.c
Normal file
55
project_euler/Problem 16/sol1.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const double tmp = log(10) / log(2); /* required to get number of digits */
|
||||
unsigned long MAX_NUM_DIGITS;
|
||||
uint8_t *digits = NULL; /* array to store individual digits. index 0 = units place */
|
||||
int N = 1000, sum = 0;
|
||||
|
||||
if (argc == 2)
|
||||
N = atoi(argv[1]);
|
||||
|
||||
MAX_NUM_DIGITS = (N + tmp) / tmp;
|
||||
|
||||
digits = calloc(MAX_NUM_DIGITS, sizeof(uint8_t));
|
||||
digits[0] = 1;
|
||||
|
||||
if (!digits)
|
||||
{
|
||||
perror("Unable to allocate memory!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int carry = 0;
|
||||
for (int j = 0; j < MAX_NUM_DIGITS; j++)
|
||||
{
|
||||
digits[j] = (digits[j] << 1) + carry; /* digit * 2 + carry */
|
||||
// printf("\t value: %d\t", digits[j]);
|
||||
if (digits[j] > 9)
|
||||
{
|
||||
carry = 1;
|
||||
digits[j] -= 10;
|
||||
} else
|
||||
carry = 0;
|
||||
// printf("carry: %d\t value: %d\n", carry, digits[j]);
|
||||
|
||||
/* accumulate sum for last multiplication */
|
||||
if (i == N - 1)
|
||||
sum += digits[j];
|
||||
}
|
||||
}
|
||||
|
||||
printf("2^%d = ", N);
|
||||
for(int i = MAX_NUM_DIGITS - 1; i >= 0; i--)
|
||||
putchar(digits[i] + 0x30);
|
||||
printf("\n\t Sum: %d\t Num. digits: %lu\n", sum, MAX_NUM_DIGITS);
|
||||
|
||||
free(digits);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user