-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_euler_prob20.c
More file actions
54 lines (42 loc) · 840 Bytes
/
project_euler_prob20.c
File metadata and controls
54 lines (42 loc) · 840 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
52
53
54
/*
* project_euler_prob20.c
*
* Created on: April 27, 2014
* Author: ssimmons
*
* Problem 20: Find sum of digits
* for 100!
*
* Copy sum function from problem 16
*/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
int sum_array(char *);
int main(){
char *digits = NULL;
unsigned long int i = 1;
mpz_t output;
mpz_init(output);
// Could do this with builtin function
// mpz_fac_ui
mpz_set_ui(output,1);
for (i = 1; i<101; i++){
mpz_mul_ui(output,output,i);
}
digits = mpz_get_str( (char * ) NULL, 10 , output);
mpz_clear(output);
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;
}