Skip to content

Commit b21444d

Browse files
Merge pull request TheAlgorithms#108 from RollandMichael7/patch-1
Create LowestBasePalindrome.java
2 parents 9fd57c7 + 658ed90 commit b21444d

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

Misc/LowestBasePalindrome.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import java.util.InputMismatchException;
2+
import java.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+
public class LowestBasePalindrome {
15+
16+
public static void main(String[] args) {
17+
Scanner in = new Scanner(System.in);
18+
int n=0;
19+
while (true) {
20+
try {
21+
System.out.print("Enter number: ");
22+
n = in.nextInt();
23+
break;
24+
} catch (InputMismatchException e) {
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+
public static int lowestBasePalindrome(int num) {
41+
int base, num2=num;
42+
int digit;
43+
char digitC;
44+
boolean foundBase=false;
45+
String newNum = "";
46+
String digits = "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+
String reverse = 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+
return base;
85+
}
86+
}
87+
}
88+
// If all else fails, n is always a palindrome in base n-1. ("11")
89+
return num-1;
90+
}
91+
92+
private static String reverse(String str) {
93+
String reverse = "";
94+
for(int i=str.length()-1; i>=0; i--) {
95+
reverse += str.charAt(i);
96+
}
97+
return reverse;
98+
}
99+
100+
private static String base2base(String n, int b1, int b2) {
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+
int decimalValue = 0, charB2;
105+
char charB1;
106+
String output="";
107+
// Go through every character of n
108+
for (int i=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+
return output;
143+
}
144+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy