forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowestBasePalindrome.java
96 lines (85 loc) · 3 KB
/
LowestBasePalindrome.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.thealgorithms.others;
import java.util.ArrayList;
/**
* @brief Class for finding the lowest base in which a given integer is a palindrome.
cf. https://oeis.org/A016026
*/
public final class LowestBasePalindrome {
private LowestBasePalindrome() {
}
private static void checkBase(int base) {
if (base <= 1) {
throw new IllegalArgumentException("base must be greater than 1.");
}
}
private static void checkNumber(int number) {
if (number < 0) {
throw new IllegalArgumentException("number must be nonnegative.");
}
}
/**
* @brief computes the representation of the input number in given base
* @param number the input number
* @param base the given base
* @exception IllegalArgumentException number is negative or base is less than 2
* @return the list containing the digits of the input number in the given base, the most
* significant digit is at the end of the array
*/
public static ArrayList<Integer> computeDigitsInBase(int number, int base) {
checkNumber(number);
checkBase(base);
var result = new ArrayList<Integer>();
while (number > 0) {
result.add(number % base);
number /= base;
}
return result;
}
/**
* @brief checks if the input array is a palindrome
* @brief list the input array
* @return true, if the input array is a palindrome, false otherwise
*/
public static boolean isPalindromic(ArrayList<Integer> list) {
for (int pos = 0; pos < list.size() / 2; ++pos) {
if (list.get(pos) != list.get(list.size() - 1 - pos)) {
return false;
}
}
return true;
}
/**
* @brief checks if representation of the input number in given base is a palindrome
* @param number the input number
* @param base the given base
* @exception IllegalArgumentException number is negative or base is less than 2
* @return true, if the input number represented in the given base is a palindrome, false
* otherwise
*/
public static boolean isPalindromicInBase(int number, int base) {
checkNumber(number);
checkBase(base);
if (number <= 1) {
return true;
}
if (number % base == 0) {
// the last digit of number written in base is 0
return false;
}
return isPalindromic(computeDigitsInBase(number, base));
}
/**
* @brief finds the smallest base for which the representation of the input number is a
* palindrome
* @param number the input number
* @exception IllegalArgumentException number is negative
* @return the smallest base for which the representation of the input number is a palindrome
*/
public static int lowestBasePalindrome(int number) {
int base = 2;
while (!isPalindromicInBase(number, base)) {
++base;
}
return base;
}
}