-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler3.java
More file actions
61 lines (58 loc) · 1.71 KB
/
projectEuler3.java
File metadata and controls
61 lines (58 loc) · 1.71 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
/*
* Author: Sreenath T V
* https://www.hackerrank.com/contests/projecteuler/challenges/euler003
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int T;
Scanner kbd = new Scanner(System.in);
T = kbd.nextInt();
long arr[] = new long[T];
long fromBottom = 0;
for (int i = 0; i < T; i++) {
arr[i] = kbd.nextLong();
}
boolean result;
long num;
for (int i = 0; i < T; i++) {
result = false;
num = 0;
fromBottom = 0;
if(checkPrime(arr[i])) {
System.out.println(arr[i]);
continue;
}
for (int j = 2; j <= Math.sqrt(arr[i]); j++) {
if (arr[i] % j == 0) {
num = arr[i] / j;
result = checkPrime(num);
if (result) {
System.out.println(num);
break;
} else {
if (checkPrime(j)) {
fromBottom = j;
}
}
}
}
if (!result && fromBottom != 0) {
System.out.println(fromBottom);
}
}
}
private static boolean checkPrime(long num) {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}