forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowestBasePalindrome.java
More file actions
180 lines (167 loc) · 5.93 KB
/
LowestBasePalindrome.java
File metadata and controls
180 lines (167 loc) · 5.93 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.thealgorithms.others;
import java.util.ArrayList;
import java.util.List;
/**
* Utility class for finding the lowest base in which a given integer is a
* palindrome.
* <p>
* A number is a palindrome in a given base if its representation in that base
* reads the same
* forwards and backwards. For example, 15 in base 2 is 1111, which is
* palindromic.
* This class provides methods to check palindromic properties and find the
* smallest base
* where a number becomes palindromic.
* </p>
* <p>
* Example: The number 15 in base 2 is represented as [1,1,1,1], which is
* palindromic.
* The number 10 in base 3 is represented as [1,0,1], which is also palindromic.
* </p>
*
* @see <a href="https://oeis.org/A016026">OEIS A016026 - Smallest base in which
* n is palindromic</a>
* @author TheAlgorithms Contributors
*/
public final class LowestBasePalindrome {
private LowestBasePalindrome() {
}
/**
* Validates the base, ensuring it is greater than 1.
*
* @param base the base to be checked
* @throws IllegalArgumentException if the base is less than or equal to 1
*/
private static void checkBase(int base) {
if (base <= 1) {
throw new IllegalArgumentException("Base must be greater than 1.");
}
}
/**
* Validates the number, ensuring it is non-negative.
*
* @param number the number to be checked
* @throws IllegalArgumentException if the number is negative
*/
private static void checkNumber(int number) {
if (number < 0) {
throw new IllegalArgumentException("Number must be non-negative.");
}
}
/**
* Computes the digits of a given number in a specified base.
* <p>
* The digits are returned in reverse order (least significant digit first).
* For example, the number 13 in base 2 produces [1,0,1,1] representing 1101 in
* binary.
* </p>
*
* @param number the number to be converted (must be non-negative)
* @param base the base to be used for the conversion (must be greater than 1)
* @return a list of digits representing the number in the given base, with the
* least significant digit at the beginning of the list
* @throws IllegalArgumentException if the number is negative or the base is
* less than 2
*/
public static List<Integer> computeDigitsInBase(int number, int base) {
checkNumber(number);
checkBase(base);
List<Integer> digits = new ArrayList<>();
while (number > 0) {
digits.add(number % base);
number /= base;
}
return digits;
}
/**
* Checks if a list of integers is palindromic.
* <p>
* A list is palindromic if it reads the same forwards and backwards.
* For example, [1,2,1] is palindromic, but [1,2,3] is not.
* </p>
*
* @param list the list of integers to be checked
* @return {@code true} if the list is a palindrome, {@code false} otherwise
*/
public static boolean isPalindromic(List<Integer> list) {
int size = list.size();
for (int i = 0; i < size / 2; i++) {
if (!list.get(i).equals(list.get(size - 1 - i))) {
return false;
}
}
return true;
}
/**
* Checks if the representation of a given number in a specified base is
* palindromic.
* <p>
* This method first validates the input, then applies optimization: if the
* number
* ends with 0 in the given base (i.e., divisible by the base), it cannot be
* palindromic
* as palindromes cannot start with 0.
* </p>
* <p>
* Examples:
* - 101 in base 10 is palindromic (101)
* - 15 in base 2 is palindromic (1111)
* - 10 in base 3 is palindromic (101)
* </p>
*
* @param number the number to be checked (must be non-negative)
* @param base the base in which the number will be represented (must be
* greater than 1)
* @return {@code true} if the number is palindromic in the specified base,
* {@code false} otherwise
* @throws IllegalArgumentException if the number is negative or the base is
* less than 2
*/
public static boolean isPalindromicInBase(int number, int base) {
checkNumber(number);
checkBase(base);
if (number <= 1) {
return true;
}
if (number % base == 0) {
// If the last digit of the number in the given base is 0, it can't be
// palindromic
return false;
}
return isPalindromic(computeDigitsInBase(number, base));
}
/**
* Finds the smallest base in which the representation of a given number is
* palindromic.
* <p>
* This method iteratively checks bases starting from 2 until it finds one where
* the number is palindromic. For any number n ≥ 2, the number is always
* palindromic
* in base n-1 (represented as [1, 1]), so this algorithm is guaranteed to
* terminate.
* </p>
* <p>
* Time Complexity: O(n * log(n)) in the worst case, where we check each base
* and
* convert the number to that base.
* </p>
* <p>
* Examples:
* - lowestBasePalindrome(15) returns 2 (15 in base 2 is 1111)
* - lowestBasePalindrome(10) returns 3 (10 in base 3 is 101)
* - lowestBasePalindrome(11) returns 10 (11 in base 10 is 11)
* </p>
*
* @param number the number to be checked (must be non-negative)
* @return the smallest base in which the number is a palindrome (base ≥ 2)
* @throws IllegalArgumentException if the number is negative
*/
public static int lowestBasePalindrome(int number) {
checkNumber(number);
int base = 2;
while (!isPalindromicInBase(number, base)) {
base++;
}
return base;
}
}