-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler047.java
More file actions
executable file
·92 lines (76 loc) · 2.24 KB
/
Copy pathEuler047.java
File metadata and controls
executable file
·92 lines (76 loc) · 2.24 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
/*
Project Euler Problem 47
========================
The first two consecutive numbers to have two distinct prime factors are:
14 = 2 * 7
15 = 3 * 5
The first three consecutive numbers to have three distinct prime factors
are:
644 = 2^2 * 7 * 23
645 = 3 * 5 * 43
646 = 2 * 17 * 19.
Find the first four consecutive integers to have four distinct primes
factors. What is the first of these numbers?
*/
import java.util.Scanner;
public class Euler047 {
public static void main(String[] args) throws FileNotFoundException {
Date start, end;
start = new Date();
end = new Date();
System.out.println(getFactors(93845346));
System.out.println("Execution Time: "
+ (end.getTime() - start.getTime()));
}
private static ArrayList<Integer> getFactors(long n)
throws FileNotFoundException {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> primeList = new ArrayList<Integer>();
File file = new File("./src/listOfPrimes.txt");
Scanner scanner = null;
scanner = new Scanner(file);
while (scanner.hasNextInt()) {
primeList.add(scanner.nextInt());
}
scanner.close();
int i = 0;
while (n != 1) {
if (n % primeList.get(i) == 0) {
list.add(primeList.get(i));
n = n / primeList.get(i);
i = 0;
} else {
i++;
}
}
return list;
}
public static boolean isPrime(long num) {
if (num == 0 || num == 1)
return false;
if (num < 4)
return true;
if (num % 2 == 0)
return false;
if (num < 9)
return true;
if (num % 3 == 0)
return false;
else {
int sqrt = (int) Math.floor(Math.sqrt(num));
int f = 5;
while (f <= sqrt) {
if (num % f == 0)
return false;
if (num % (f + 2) == 0)
return false;
f += 6;
}
return true;
}
}
}