44import java .util .List ;
55
66/**
7- * @brief Class for finding the lowest base in which a given integer is a palindrome.
8- cf. https://oeis.org/A016026
7+ * Utility class for finding the lowest base in which a given integer is a
8+ * palindrome.
9+ * <p>
10+ * A number is a palindrome in a given base if its representation in that base
11+ * reads the same
12+ * forwards and backwards. For example, 15 in base 2 is 1111, which is
13+ * palindromic.
14+ * This class provides methods to check palindromic properties and find the
15+ * smallest base
16+ * where a number becomes palindromic.
17+ * </p>
18+ * <p>
19+ * Example: The number 15 in base 2 is represented as [1,1,1,1], which is
20+ * palindromic.
21+ * The number 10 in base 3 is represented as [1,0,1], which is also palindromic.
22+ * </p>
23+ *
24+ * @see <a href="https://oeis.org/A016026">OEIS A016026 - Smallest base in which
25+ * n is palindromic</a>
26+ * @author TheAlgorithms Contributors
927 */
1028public final class LowestBasePalindrome {
1129 private LowestBasePalindrome () {
@@ -37,12 +55,18 @@ private static void checkNumber(int number) {
3755
3856 /**
3957 * Computes the digits of a given number in a specified base.
58+ * <p>
59+ * The digits are returned in reverse order (least significant digit first).
60+ * For example, the number 13 in base 2 produces [1,0,1,1] representing 1101 in
61+ * binary.
62+ * </p>
4063 *
41- * @param number the number to be converted
42- * @param base the base to be used for the conversion
43- * @return a list of digits representing the number in the given base, with the most
44- * significant digit at the end of the list
45- * @throws IllegalArgumentException if the number is negative or the base is less than 2
64+ * @param number the number to be converted (must be non-negative)
65+ * @param base the base to be used for the conversion (must be greater than 1)
66+ * @return a list of digits representing the number in the given base, with the
67+ * least significant digit at the beginning of the list
68+ * @throws IllegalArgumentException if the number is negative or the base is
69+ * less than 2
4670 */
4771 public static List <Integer > computeDigitsInBase (int number , int base ) {
4872 checkNumber (number );
@@ -58,6 +82,10 @@ public static List<Integer> computeDigitsInBase(int number, int base) {
5882
5983 /**
6084 * Checks if a list of integers is palindromic.
85+ * <p>
86+ * A list is palindromic if it reads the same forwards and backwards.
87+ * For example, [1,2,1] is palindromic, but [1,2,3] is not.
88+ * </p>
6189 *
6290 * @param list the list of integers to be checked
6391 * @return {@code true} if the list is a palindrome, {@code false} otherwise
@@ -73,12 +101,29 @@ public static boolean isPalindromic(List<Integer> list) {
73101 }
74102
75103 /**
76- * Checks if the representation of a given number in a specified base is palindromic.
104+ * Checks if the representation of a given number in a specified base is
105+ * palindromic.
106+ * <p>
107+ * This method first validates the input, then applies optimization: if the
108+ * number
109+ * ends with 0 in the given base (i.e., divisible by the base), it cannot be
110+ * palindromic
111+ * as palindromes cannot start with 0.
112+ * </p>
113+ * <p>
114+ * Examples:
115+ * - 101 in base 10 is palindromic (101)
116+ * - 15 in base 2 is palindromic (1111)
117+ * - 10 in base 3 is palindromic (101)
118+ * </p>
77119 *
78- * @param number the number to be checked
79- * @param base the base in which the number will be represented
80- * @return {@code true} if the number is palindromic in the specified base, {@code false} otherwise
81- * @throws IllegalArgumentException if the number is negative or the base is less than 2
120+ * @param number the number to be checked (must be non-negative)
121+ * @param base the base in which the number will be represented (must be
122+ * greater than 1)
123+ * @return {@code true} if the number is palindromic in the specified base,
124+ * {@code false} otherwise
125+ * @throws IllegalArgumentException if the number is negative or the base is
126+ * less than 2
82127 */
83128 public static boolean isPalindromicInBase (int number , int base ) {
84129 checkNumber (number );
@@ -89,18 +134,38 @@ public static boolean isPalindromicInBase(int number, int base) {
89134 }
90135
91136 if (number % base == 0 ) {
92- // If the last digit of the number in the given base is 0, it can't be palindromic
137+ // If the last digit of the number in the given base is 0, it can't be
138+ // palindromic
93139 return false ;
94140 }
95141
96142 return isPalindromic (computeDigitsInBase (number , base ));
97143 }
98144
99145 /**
100- * Finds the smallest base in which the representation of a given number is palindromic.
146+ * Finds the smallest base in which the representation of a given number is
147+ * palindromic.
148+ * <p>
149+ * This method iteratively checks bases starting from 2 until it finds one where
150+ * the number is palindromic. For any number n ≥ 2, the number is always
151+ * palindromic
152+ * in base n-1 (represented as [1, 1]), so this algorithm is guaranteed to
153+ * terminate.
154+ * </p>
155+ * <p>
156+ * Time Complexity: O(n * log(n)) in the worst case, where we check each base
157+ * and
158+ * convert the number to that base.
159+ * </p>
160+ * <p>
161+ * Examples:
162+ * - lowestBasePalindrome(15) returns 2 (15 in base 2 is 1111)
163+ * - lowestBasePalindrome(10) returns 3 (10 in base 3 is 101)
164+ * - lowestBasePalindrome(11) returns 10 (11 in base 10 is 11)
165+ * </p>
101166 *
102- * @param number the number to be checked
103- * @return the smallest base in which the number is a palindrome
167+ * @param number the number to be checked (must be non-negative)
168+ * @return the smallest base in which the number is a palindrome (base ≥ 2)
104169 * @throws IllegalArgumentException if the number is negative
105170 */
106171 public static int lowestBasePalindrome (int number ) {
0 commit comments