-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem03.java
More file actions
38 lines (33 loc) · 1008 Bytes
/
problem03.java
File metadata and controls
38 lines (33 loc) · 1008 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
/*
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143 ?
*/
public class problem03 {
public static void main(String args[]) {
int test = 300;
long num = 600851475143L; // L designates it as a long
int root = (int)Math.floor(Math.sqrt(num)); // casting
// System.out.println(root);
for (long i = 1; i < num/2; i++) {
if (num%i == 0) {// if it's a factor of num
System.out.print(i + " is a factor of " + num + ". ");
if (isPrime(i) == true) {// if it's prime
System.out.println("And it's prime!");
}
}
}
}
public static boolean isPrime(long num) {
boolean prime = true;
int numroot =(int)Math.floor(Math.sqrt(num));
System.out.println("Testing " + num +" for primeness up to sqrt(" + num + ")");
for (int j=2; j<numroot; j++) {
if (num%j == 0) {
prime = false;
//System.out.println(num + " isn't prime");
}
}
return prime;
}
}