-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_euler_prob16.c
More file actions
51 lines (42 loc) · 853 Bytes
/
project_euler_prob16.c
File metadata and controls
51 lines (42 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* project_euler_prob16.c
*
* Created on: Aug 23, 2012
* Author: ssimmons
*
* Problem 16: Find sum of digits for
* 2^1000.
*
* Problem 15 done analytically (combinatorics!)
* so no source code.
*
* Learning how to use gmp lib here (as 2^1000 *very* big)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gmp.h>
#define SIZE 150
int sum_array(char *);
int main(){
char *digits = NULL;
mpz_t answer;
mpz_init(answer);
mpz_ui_pow_ui(answer,2,1000);
digits = mpz_get_str((char *) NULL,10,answer);
printf("%s\n", digits); // debugging
mpz_clear(answer);
printf("The answer is: %d\n", sum_array(digits));
free(digits);
return 0;
}
int sum_array(char *input){
int i = 0, sum = 0;
if (input == NULL)
return sum;
while(*(input+i) != '\0'){
sum += *(input+i) - '0';
i++;
}
return sum;
}