- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
41 lines (33 loc) · 1.25 KB
/
Copy pathMain.java
File metadata and controls
41 lines (33 loc) · 1.25 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
/*
@author: mc-es
Problem 36
The decimal number, 585 = (1001001001)2 (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include leading zeros.)
Answer: 872187
*/
publicclassMain {
publicstaticvoidmain(String[] args) {
intlimit = 1000000;
intresult = doubleBasePalindromes(limit);
System.out.println(result);
}
// Function to check if a string is a palindrome
publicstaticbooleanisPalindrome(Strings) {
returns.equals(newStringBuilder(s).reverse().toString());
}
// Function to find the sum of double-base palindromes below the given limit
publicstaticintdoubleBasePalindromes(intlimit) {
inttotal = 0;
for (intnumber = 1; number < limit; number++) {
// Convert the number to its decimal and binary string representations
StringdecimalRepresentation = Integer.toString(number);
StringbinaryRepresentation = Integer.toBinaryString(number);
// Check if both representations are palindromes
if (isPalindrome(decimalRepresentation) && isPalindrome(binaryRepresentation)) {
total += number;
}
}
returntotal;
}
}