-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterestingDigits.java
More file actions
64 lines (52 loc) · 1.07 KB
/
InterestingDigits.java
File metadata and controls
64 lines (52 loc) · 1.07 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
public class InterestingDigits {
public int[] digits(int base) {
int[] test;
String nums = "";
Integer digit = 2;
Integer multiple = 1;
Integer count = 1;
int index = 0;
while (digit <= base - 1) {
while (multiple <= 999) {
multiple = digit * count;
count++;
if (isInteresting(multiple, digit, base) == true) {
continue;
} else {
break;
}
}
if (isInteresting(multiple, digit, base) == true) {
nums += digit.toString();
if (digit < base - 1) {
nums += ",";
}
}
multiple = 1;
count = 1;
digit++;
}
String[] tokens = nums.split(",");
test = new int[tokens.length];
for (String t : tokens) {
test[index] = Integer.parseInt(t);
index++;
}
return test;
}
public boolean isInteresting(int num, int digit, int base) {
boolean interesting = false;
int sum = 0;
while (num > 0) {
sum = sum + num % base;
num = num / base;
}
// System.out.println("sum "+sum);
if (sum % digit == 0) {
interesting = true;
} else {
interesting = false;
}
return interesting;
}
}