- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemThree.java
More file actions
Latest commit
32 lines (30 loc) · 783 Bytes
/
Copy pathProblemThree.java
File metadata and controls
32 lines (30 loc) · 783 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
/*
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
*/
importjava.util.ArrayList;
importjava.util.List;
importjava.math.*;
publicclassProblemThree {
publicstaticList<Long> primeFactors(Longnumber) {
Longn = number;
List<Long> factors = newArrayList<Long>();
for (longi = 2; i <= n; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
returnfactors;
}
publicstaticvoidmain(String[] args) {
BigIntegerbigRange;
Longrange;
bigRange=newBigInteger("600851475143");
range=bigRange.longValue();
System.out.println("Primefactors of "+range);
for (LongLong : primeFactors((long) range)) {
System.out.println(Long);
}
}
}