-
Notifications
You must be signed in to change notification settings - Fork 0
/
030.c
58 lines (44 loc) · 1.01 KB
/
030.c
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
55
56
57
58
// Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int main() {
bool isValid(int n, int *pow5);
void populatePowers(int exp, int arr[]);
int pows[10];
long int sum = 0;
populatePowers(5, pows);
for (int i = 2; i <= 999999; i++) {
if (isValid(i, pows)) {
printf("%i\n", i);
sum += i;
}
}
printf("Sum: %li\n", sum);
return 0;
}
// Populate power array
void populatePowers(int exp, int arr[]) {
for (int i = 0; i < 10; i++) {
arr[i] = pow(i, exp);
}
}
// Returns whether n can be written as the sum of fifth powers of their digiits
bool isValid(int n, int *pows) {
char seq[9];
int digit;
long int sum = 0;
sprintf(seq, "%i", n);
for (int i = 0; seq[i] != '\0'; i++) {
digit = seq[i] - '0';
sum += pows[digit];
if (sum > n) {
return false;
}
}
if (sum == n) {
return true;
} else {
return false;
}
}