-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactors.java
More file actions
29 lines (28 loc) · 920 Bytes
/
Copy pathFactors.java
File metadata and controls
29 lines (28 loc) · 920 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class Factors {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int[] ans = factors(a);
for (int i = 0; i < ans.length; i++) {
System.out.println(ans[i]);
}
}
public static int[] factors(int n) {
int[] fac = new int[n];
int pos = 0;
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
fac[pos++] = i;
n = n / i;
}
}
if (n > 1) fac[pos++] = n;
int[] ans = new int[pos];
for (int i = 0; i < pos; i++) ans[i] = fac[i];
return ans;
}
}