-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivisors.java
More file actions
29 lines (22 loc) · 748 Bytes
/
Divisors.java
File metadata and controls
29 lines (22 loc) · 748 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
package com.company;
import java.util.ArrayList;
public class Divisors {
private ArrayList<Integer> listDivisors = new ArrayList<Integer>();
// --> Opted to exclude 1 from the list of divisors for the simple reason that including it adds no real challenge.
public Divisors(int number) {
for (int i=2; i<=9; i++) {
if (number % i == 0) {
listDivisors.add(i);
}
}
}
// --> Randomly pops a divisor from the array. Returns zero if the array is empty.
int getDivisor(){
int length = listDivisors.size();
if (length == 0){
return 0;
} else {
return listDivisors.remove((int)(Math.random() * length));
}
}
}