Code RoomSmallest repunit base
HardPrep Room Coding #757

Smallest repunit base

CodingAlgorithms & data structuresSenior–Staff~35 min

Given an integer n (3 <= n <= 10^18) provided as a Python int, find the smallest base k >= 2 such that the representation of n in base k consists entirely of 1s, i.e. n = 1 + k + k^2 + ... + k^m for some m >= 1. Return k. For example n = 13 = 111 in base 3, so the answer is 3. Every n has the trivial representation 11 in base n-1, so an answer always exists.

Implement
smallest_good_base(n: int) → int
Examples
in[13]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[13]3not run yetsample