|
| 1 | +importjava.util.InputMismatchException; |
| 2 | +importjava.util.Scanner; |
| 3 | + |
| 4 | +/** |
| 5 | + * Class for finding the lowest base in which a given integer is a palindrome. |
| 6 | + * Includes auxiliary methods for converting between bases and reversing strings. |
| 7 | + * |
| 8 | + * NOTE: There is potential for error, see note at line 63. |
| 9 | + * |
| 10 | + * @author RollandMichael |
| 11 | + * @version 2017.09.28 |
| 12 | + * |
| 13 | + */ |
| 14 | +publicclassLowestBasePalindrome { |
| 15 | + |
| 16 | +publicstaticvoidmain(String[]args) { |
| 17 | +Scannerin =newScanner(System.in); |
| 18 | +intn=0; |
| 19 | +while (true) { |
| 20 | +try { |
| 21 | +System.out.print("Enter number: "); |
| 22 | +n =in.nextInt(); |
| 23 | +break; |
| 24 | +}catch (InputMismatchExceptione) { |
| 25 | +System.out.println("Invalid input!"); |
| 26 | +in.next(); |
| 27 | +} |
| 28 | +} |
| 29 | +System.out.println(n+" is a palindrome in base "+lowestBasePalindrome(n)); |
| 30 | +System.out.println(base2base(Integer.toString(n),10,lowestBasePalindrome(n))); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Given a number in base 10, returns the lowest base in which the |
| 35 | + * number is represented by a palindrome (read the same left-to-right |
| 36 | + * and right-to-left). |
| 37 | + * @param num A number in base 10. |
| 38 | + * @return The lowest base in which num is a palindrome. |
| 39 | + */ |
| 40 | +publicstaticintlowestBasePalindrome(intnum) { |
| 41 | +intbase,num2=num; |
| 42 | +intdigit; |
| 43 | +chardigitC; |
| 44 | +booleanfoundBase=false; |
| 45 | +StringnewNum =""; |
| 46 | +Stringdigits ="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 47 | + |
| 48 | +while (!foundBase) { |
| 49 | +// Try from bases 2 to num (any number n in base n is 1) |
| 50 | +for (base=2;base<num2;base++) { |
| 51 | +newNum=""; |
| 52 | +while(num>0) { |
| 53 | +// Obtain the first digit of n in the current base, |
| 54 | +// which is equivalent to the integer remainder of (n/base). |
| 55 | +// The next digit is obtained by dividing n by the base and |
| 56 | +// continuing the process of getting the remainder. This is done |
| 57 | +// until n is <=0 and the number in the new base is obtained. |
| 58 | +digit = (num %base); |
| 59 | +num/=base; |
| 60 | +// If the digit isn't in the set of [0-9][A-Z] (beyond base 36), its character |
| 61 | +// form is just its value in ASCII. |
| 62 | + |
| 63 | +// NOTE: This may cause problems, as the capital letters are ASCII values |
| 64 | +// 65-90. It may cause false positives when one digit is, for instance 10 and assigned |
| 65 | +// 'A' from the character array and the other is 65 and also assigned 'A'. |
| 66 | + |
| 67 | +// Regardless, the character is added to the representation of n |
| 68 | +// in the current base. |
| 69 | +if (digit>=digits.length()) { |
| 70 | +digitC=(char)(digit); |
| 71 | +newNum+=digitC; |
| 72 | +continue; |
| 73 | +} |
| 74 | +newNum+=digits.charAt(digit); |
| 75 | +} |
| 76 | +// Num is assigned back its original value for the next iteration. |
| 77 | +num=num2; |
| 78 | +// Auxiliary method reverses the number. |
| 79 | +Stringreverse =reverse(newNum); |
| 80 | +// If the number is read the same as its reverse, then it is a palindrome. |
| 81 | +// The current base is returned. |
| 82 | +if (reverse.equals(newNum)) { |
| 83 | +foundBase=true; |
| 84 | +returnbase; |
| 85 | +} |
| 86 | +} |
| 87 | +} |
| 88 | +// If all else fails, n is always a palindrome in base n-1. ("11") |
| 89 | +returnnum-1; |
| 90 | +} |
| 91 | + |
| 92 | +privatestaticStringreverse(Stringstr) { |
| 93 | +Stringreverse =""; |
| 94 | +for(inti=str.length()-1;i>=0;i--) { |
| 95 | +reverse +=str.charAt(i); |
| 96 | + } |
| 97 | +returnreverse; |
| 98 | +} |
| 99 | + |
| 100 | +privatestaticStringbase2base(Stringn,intb1,intb2) { |
| 101 | +// Declare variables: decimal value of n, |
| 102 | +// character of base b1, character of base b2, |
| 103 | +// and the string that will be returned. |
| 104 | +intdecimalValue =0,charB2; |
| 105 | +charcharB1; |
| 106 | +Stringoutput=""; |
| 107 | +// Go through every character of n |
| 108 | +for (inti=0;i<n.length();i++) { |
| 109 | +// store the character in charB1 |
| 110 | +charB1 =n.charAt(i); |
| 111 | +// if it is a non-number, convert it to a decimal value >9 and store it in charB2 |
| 112 | +if (charB1 >='A' &&charB1 <='Z') |
| 113 | +charB2 =10 + (charB1 -'A'); |
| 114 | +// Else, store the integer value in charB2 |
| 115 | +else |
| 116 | +charB2 =charB1 -'0'; |
| 117 | +// Convert the digit to decimal and add it to the |
| 118 | +// decimalValue of n |
| 119 | +decimalValue =decimalValue *b1 +charB2; |
| 120 | +} |
| 121 | + |
| 122 | +// Converting the decimal value to base b2: |
| 123 | +// A number is converted from decimal to another base |
| 124 | +// by continuously dividing by the base and recording |
| 125 | +// the remainder until the quotient is zero. The number in the |
| 126 | +// new base is the remainders, with the last remainder |
| 127 | +// being the left-most digit. |
| 128 | + |
| 129 | +// While the quotient is NOT zero: |
| 130 | +while (decimalValue !=0) { |
| 131 | +// If the remainder is a digit < 10, simply add it to |
| 132 | +// the left side of the new number. |
| 133 | +if (decimalValue %b2 <10) |
| 134 | +output =Integer.toString(decimalValue %b2) +output; |
| 135 | +// If the remainder is >= 10, add a character with the |
| 136 | +// corresponding value to the new number. (A = 10, B = 11, C = 12, ...) |
| 137 | +else |
| 138 | +output = (char)((decimalValue %b2)+55) +output; |
| 139 | +// Divide by the new base again |
| 140 | +decimalValue /=b2; |
| 141 | +} |
| 142 | +returnoutput; |
| 143 | +} |
| 144 | +} |