-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorialSum.java
More file actions
43 lines (39 loc) · 804 Bytes
/
FactorialSum.java
File metadata and controls
43 lines (39 loc) · 804 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
import java.util.Arrays;
public class FactorialSum {
public static void main(String[] args){
long[] f = new long[10];
int total = 0;
for (int k = 0; k < 10; k++){
long value = k;
f[k] = fact(value);
}
f[0]=0;
System.out.println(Arrays.toString(f));
for (int k = 3; k < 1000000; k++){
long value = k;
String s = "" + k;
int l = 0;
while (l<s.length()){
long fact = f[digitAt(k, l)];
value = value-fact;
//System.out.println(k+ " " +value);
if (value<0)
break;
l++;
}
if (value==0){
total += k;
System.out.println(total);}
}
System.out.println(total);
}
public static long fact(long n)
{
return n*n*n*n*n;
}
public static int digitAt(int n, int d){
String s = "" + n;
s = s.substring(d, d+1);
return Integer.parseInt(s);
}
}