-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP34.java
38 lines (33 loc) · 788 Bytes
/
P34.java
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
package euler;
import java.util.Hashtable;
public class P34
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
// first build a lookup table
Hashtable<Integer,Integer> table = new Hashtable<Integer,Integer>();
for (int i=0; i<10; i++)
{
int factorial = 1;
for (int f=i; f>=1; f--)
factorial *= f;
table.put(i,factorial);
}
long overallSum =0;
for (int i=3; i<362880; i++) // upper limit is 9!
{
long sum=0;
int num=i;
for (int d=0; d<=(int)Math.log10(i); d++, num /= 10)
{
int rem = num%10;
sum += table.get(rem);
}
if (sum==i)
overallSum += sum;
}
long end = System.currentTimeMillis();
System.out.format ("In %dms, found overall sum is %d%n", (end-start), overallSum);
}
}