- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
66 lines (53 loc) · 1.48 KB
/
Copy pathMain.java
File metadata and controls
66 lines (53 loc) · 1.48 KB
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
59
60
61
62
63
64
65
66
importjava.util.HashSet;
importjava.util.Set;
/*
@author: mc-es
Problem 47
The first two consecutive numbers to have two distinct prime factors are:
14 = 2 x 7
15 = 3 x 5
The first three consecutive numbers to have three distinct prime factors are
644 = 2^2 x 7 x 23
645 = 3 x 5 x 43
646 = 2 x 17 x 19
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
Answer: 134043
*/
publicclassMain {
publicstaticvoidmain(String[] args) {
intresult = findConsecutiveNumbers(4, 4);
System.out.println("Result: " + result);
}
publicstaticintprimeFactorsCount(intn) {
Set<Integer> factors = newHashSet<>();
while (n % 2 == 0) {
factors.add(2);
n /= 2;
}
for (inti = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
if (n > 2) {
factors.add(n);
}
returnfactors.size();
}
publicstaticintfindConsecutiveNumbers(inttargetFactors, intconsecutiveCount) {
intconsecutive = 0;
intnum = 2;
while (true) {
if (primeFactorsCount(num) == targetFactors) {
consecutive++;
if (consecutive == consecutiveCount) {
returnnum - consecutiveCount + 1;
}
} else {
consecutive = 0;
}
num++;
}
}
}