Ctrl K

Palindromes in Two Bases

Hard
Call a positive integer a **twin palindrome** for base base if it reads the same forwards and backwards both in decimal and when written in base base (both forms without leading zeros). For example, 5 is a twin palindrome for base 2, since in binary it is 101. But 6 is not: in binary it is 110. Return the sum of the count smallest twin palindromes for base base.
Example 1
base=2, count=4 → 16

The first such numbers are 1 (1), 3 (11), 5 (101) and 7 (111), with the binary form in brackets. Their sum is 16.

Example 2
base=4, count=5 → 66

The numbers are 1, 2, 3, 5 (11 in base 4) and 55 (313 in base 4); 4 = 10₄ and 6 = 12₄ do not qualify. The sum is 66.

Example 3
base=9, count=30 → 18627530
Constraints
  • 2 ≤ base ≤ 9
  • 1 ≤ count ≤ 30
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPalindrome From Two Factors HardSums of Consecutive Numbers HardMirror Squares in a Range HardWhere Moss Species Meet HardSplit a Tree Into Equal Parts Hard
loading Python…
Input
base=2, count=4
Expected
16

Run checks the first 3 tests in your browser. Submit checks all 60 tests on the server, hidden ones included.