|
1 | 1 | package com.thealgorithms.others; |
2 | 2 |
|
3 | | -import java.util.InputMismatchException; |
4 | | -import java.util.Scanner; |
| 3 | +import java.util.ArrayList; |
5 | 4 |
|
6 | 5 | /** |
7 | | - * Class for finding the lowest base in which a given integer is a palindrome. |
8 | | - * Includes auxiliary methods for converting between bases and reversing |
9 | | - * strings. |
10 | | - * |
11 | | - * <p> |
12 | | - * NOTE: There is potential for error, see note at line 63. |
13 | | - * |
14 | | - * @author RollandMichael |
15 | | - * @version 2017.09.28 |
| 6 | + * @brief Class for finding the lowest base in which a given integer is a palindrome. |
| 7 | + cf. https://oeis.org/A016026 |
16 | 8 | */ |
17 | | -public class LowestBasePalindrome { |
| 9 | +final public class LowestBasePalindrome { |
| 10 | + private LowestBasePalindrome() { |
| 11 | + } |
18 | 12 |
|
19 | | - public static void main(String[] args) { |
20 | | - Scanner in = new Scanner(System.in); |
21 | | - int n = 0; |
22 | | - while (true) { |
23 | | - try { |
24 | | - System.out.print("Enter number: "); |
25 | | - n = in.nextInt(); |
26 | | - break; |
27 | | - } catch (InputMismatchException e) { |
28 | | - System.out.println("Invalid input!"); |
29 | | - in.next(); |
30 | | - } |
| 13 | + private static void checkBase(int base) { |
| 14 | + if (base <= 1) { |
| 15 | + throw new IllegalArgumentException("base must be greater than 1."); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + private static void checkNumber(int number) { |
| 20 | + if (number < 0) { |
| 21 | + throw new IllegalArgumentException("number must be nonnegative."); |
31 | 22 | } |
32 | | - System.out.println( |
33 | | - n + " is a palindrome in base " + lowestBasePalindrome(n) |
34 | | - ); |
35 | | - System.out.println( |
36 | | - base2base(Integer.toString(n), 10, lowestBasePalindrome(n)) |
37 | | - ); |
38 | | - in.close(); |
39 | 23 | } |
40 | 24 |
|
41 | 25 | /** |
42 | | - * Given a number in base 10, returns the lowest base in which the number is |
43 | | - * represented by a palindrome (read the same left-to-right and |
44 | | - * right-to-left). |
45 | | - * |
46 | | - * @param num A number in base 10. |
47 | | - * @return The lowest base in which num is a palindrome. |
| 26 | + * @brief computes the representation of the input number in given base |
| 27 | + * @param number the input number |
| 28 | + * @param base the given base |
| 29 | + * @exception IllegalArgumentException number is negative or base is less than 2 |
| 30 | + * @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 |
48 | 31 | */ |
49 | | - public static int lowestBasePalindrome(int num) { |
50 | | - int base, num2 = num; |
51 | | - int digit; |
52 | | - char digitC; |
53 | | - boolean foundBase = false; |
54 | | - String newNum = ""; |
55 | | - String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
56 | | - |
57 | | - while (!foundBase) { |
58 | | - // Try from bases 2 to num-1 |
59 | | - for (base = 2; base < num2; base++) { |
60 | | - newNum = ""; |
61 | | - while (num > 0) { |
62 | | - // Obtain the first digit of n in the current base, |
63 | | - // which is equivalent to the integer remainder of (n/base). |
64 | | - // The next digit is obtained by dividing n by the base and |
65 | | - // continuing the process of getting the remainder. This is done |
66 | | - // until n is <=0 and the number in the new base is obtained. |
67 | | - digit = (num % base); |
68 | | - num /= base; |
69 | | - // If the digit isn't in the set of [0-9][A-Z] (beyond base 36), its character |
70 | | - // form is just its value in ASCII. |
| 32 | + public static ArrayList<Integer> computeDigitsInBase(int number, int base) { |
| 33 | + checkNumber(number); |
| 34 | + checkBase(base); |
| 35 | + var result = new ArrayList<Integer>(); |
| 36 | + while (number > 0) { |
| 37 | + result.add(number % base); |
| 38 | + number /= base; |
| 39 | + } |
| 40 | + return result; |
| 41 | + } |
71 | 42 |
|
72 | | - // NOTE: This may cause problems, as the capital letters are ASCII values |
73 | | - // 65-90. It may cause false positives when one digit is, for instance 10 and assigned |
74 | | - // 'A' from the character array and the other is 65 and also assigned 'A'. |
75 | | - // Regardless, the character is added to the representation of n |
76 | | - // in the current base. |
77 | | - if (digit >= digits.length()) { |
78 | | - digitC = (char) (digit); |
79 | | - newNum += digitC; |
80 | | - continue; |
81 | | - } |
82 | | - newNum += digits.charAt(digit); |
83 | | - } |
84 | | - // Num is assigned back its original value for the next iteration. |
85 | | - num = num2; |
86 | | - // Auxiliary method reverses the number. |
87 | | - String reverse = reverse(newNum); |
88 | | - // If the number is read the same as its reverse, then it is a palindrome. |
89 | | - // The current base is returned. |
90 | | - if (reverse.equals(newNum)) { |
91 | | - foundBase = true; |
92 | | - return base; |
93 | | - } |
| 43 | + /** |
| 44 | + * @brief checks if the input array is a palindrome |
| 45 | + * @brief list the input array |
| 46 | + * @return true, if the input array is a palindrome, false otherwise |
| 47 | + */ |
| 48 | + public static boolean isPalindromic(ArrayList<Integer> list) { |
| 49 | + for (int pos = 0; pos < list.size()/2; ++pos) { |
| 50 | + if(list.get(pos) != list.get(list.size()-1-pos)) { |
| 51 | + return false; |
94 | 52 | } |
95 | 53 | } |
96 | | - // If all else fails, n is always a palindrome in base n-1. ("11") |
97 | | - return num - 1; |
| 54 | + return true; |
98 | 55 | } |
99 | 56 |
|
100 | | - private static String reverse(String str) { |
101 | | - String reverse = ""; |
102 | | - for (int i = str.length() - 1; i >= 0; i--) { |
103 | | - reverse += str.charAt(i); |
| 57 | + /** |
| 58 | + * @brief checks if representation of the input number in given base is a palindrome |
| 59 | + * @param number the input number |
| 60 | + * @param base the given base |
| 61 | + * @exception IllegalArgumentException number is negative or base is less than 2 |
| 62 | + * @return true, if the input number represented in the given base is a palindrome, false otherwise |
| 63 | + */ |
| 64 | + public static boolean isPalindromicInBase(int number, int base) { |
| 65 | + checkNumber(number); |
| 66 | + checkBase(base); |
| 67 | + |
| 68 | + if (number <= 1) { |
| 69 | + return true; |
104 | 70 | } |
105 | | - return reverse; |
106 | | - } |
107 | 71 |
|
108 | | - private static String base2base(String n, int b1, int b2) { |
109 | | - // Declare variables: decimal value of n, |
110 | | - // character of base b1, character of base b2, |
111 | | - // and the string that will be returned. |
112 | | - int decimalValue = 0, charB2; |
113 | | - char charB1; |
114 | | - String output = ""; |
115 | | - // Go through every character of n |
116 | | - for (int i = 0; i < n.length(); i++) { |
117 | | - // store the character in charB1 |
118 | | - charB1 = n.charAt(i); |
119 | | - // if it is a non-number, convert it to a decimal value >9 and store it in charB2 |
120 | | - if (charB1 >= 'A' && charB1 <= 'Z') { |
121 | | - charB2 = 10 + (charB1 - 'A'); |
122 | | - } // Else, store the integer value in charB2 |
123 | | - else { |
124 | | - charB2 = charB1 - '0'; |
125 | | - } |
126 | | - // Convert the digit to decimal and add it to the |
127 | | - // decimalValue of n |
128 | | - decimalValue = decimalValue * b1 + charB2; |
| 72 | + if (number % base == 0) { |
| 73 | + // the last digit of number written in base is 0 |
| 74 | + return false; |
129 | 75 | } |
130 | 76 |
|
131 | | - // Converting the decimal value to base b2: |
132 | | - // A number is converted from decimal to another base |
133 | | - // by continuously dividing by the base and recording |
134 | | - // the remainder until the quotient is zero. The number in the |
135 | | - // new base is the remainders, with the last remainder |
136 | | - // being the left-most digit. |
137 | | - // While the quotient is NOT zero: |
138 | | - while (decimalValue != 0) { |
139 | | - // If the remainder is a digit < 10, simply add it to |
140 | | - // the left side of the new number. |
141 | | - if (decimalValue % b2 < 10) { |
142 | | - output = decimalValue % b2 + output; |
143 | | - } // If the remainder is >= 10, add a character with the |
144 | | - // corresponding value to the new number. (A = 10, B = 11, C = 12, ...) |
145 | | - else { |
146 | | - output = (char) ((decimalValue % b2) + 55) + output; |
147 | | - } |
148 | | - // Divide by the new base again |
149 | | - decimalValue /= b2; |
| 77 | + return isPalindromic(computeDigitsInBase(number, base)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @brief finds the smallest base for which the representation of the input number is a palindrome |
| 82 | + * @param number the input number |
| 83 | + * @exception IllegalArgumentException number is negative |
| 84 | + * @return the smallest base for which the representation of the input number is a palindrome |
| 85 | + */ |
| 86 | + public static int lowestBasePalindrome(int number) { |
| 87 | + int base = 2; |
| 88 | + while(!isPalindromicInBase(number, base)) { |
| 89 | + ++base; |
150 | 90 | } |
151 | | - return output; |
| 91 | + return base; |
152 | 92 | } |
153 | 93 | } |
0 commit comments