Your task is to take a positive number as input, n, and output the length of the longest rep-digit representation of n in any base. For example 7 can be represented as any of the following


The rep-digits are 111_2 and 11_6, 111_2 is longer so our answer is 3.

This is a question so answers will be scored in bytes, with fewer bytes being better.

Test Cases

1   -> 1
2   -> 1
3   -> 2
4   -> 2
5   -> 2
6   -> 2
7   -> 3
8   -> 2
9   -> 2
10  -> 2
11  -> 2
26 -> 3
63  -> 6
1023-> 10

Sample implementation

Here is an implementation in Haskell that can be used to generate more test cases.

f 0 y=[]
f x y=f(div x y)y++[mod x y]
s x=all(==x!!0)x
g x=maximum$map(length.f x)$filter(s.f x)[2..x+1]

Try it online!

  • 1
    \$\begingroup\$ Asuming base > 1 ? \$\endgroup\$
    – H.PWiz
    Commented Aug 5, 2017 at 21:02
  • 2
    \$\begingroup\$ you can add test cases 63->6 and 1023->10 if you like \$\endgroup\$
    – ZaMoC
    Commented Aug 5, 2017 at 21:38
  • 1
    \$\begingroup\$ @WheatWizard I think 26 does it for instance, it's 222 in base 3. \$\endgroup\$
    – xnor
    Commented Aug 6, 2017 at 2:15
  • 1
    \$\begingroup\$ Can bases go above 10? If so, for bases > 10, should we include characters a-z? What about bases > 36? \$\endgroup\$ Commented Aug 6, 2017 at 11:20
  • 6
    \$\begingroup\$ @RickHitchcock Bases can go arbitrarily high. Since you don't have to output any numbers in any base other than 10, I don't care how you represent other bases, but they should work for bases larger than 36. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 6, 2017 at 14:49

20 Answers 20


Jelly,  9  7 bytes

-2 thanks to caird coinheringaahing (‘Ḋ$ -> ‘€ and use of the newer alias for Ðf, Ƈ.)


A monadic link accepting and returning numbers

Try it online! or see a test suite (inputs one to 32 inclusive).


b‘€EƇZL - Link: number, n
  €     - for each (i in [1..n]):
 ‘      -   increment = i+1
        - -> [2,3,4,...,n+1]
b       - convert (n) to those bases
    Ƈ   - filter keep if:
   E    -   all elements are equal
     Z  - transpose
      L - length (note:  length of the transpose of a list of lists is the length of the
        -                longest item in the original list, but shorter than L€Ṁ)

Or maybe I should have done:


...just for the Lo1z.

  • \$\begingroup\$ So...I'm not the only one to have figured out ZL is shorter than L€Ṁ... \$\endgroup\$ Commented Aug 6, 2017 at 9:48
  • \$\begingroup\$ 7 bytes \$\endgroup\$ Commented Jun 25, 2021 at 3:51
  • \$\begingroup\$ @cairdcoinheringaahing Good spot on the ‘€, this was about a year into my use of Jelly, I should probably have known better by then! \$\endgroup\$ Commented Jun 25, 2021 at 18:46

JavaScript (ES6), 62 bytes

<input oninput=o.textContent=f(this.value)><pre id=o>

  • 2
    \$\begingroup\$ I love the unnecessarily golfed test HTML \$\endgroup\$
    – Jakob
    Commented Aug 6, 2017 at 5:54

Haskell, 86 81 79 bytes

2 bytes saved thanks to Laikoni

x!y=mod x y:div x y!y

Try it online!

Since this has died down a bit, here's my approach. It's a golfed version of the sample code I made for the question. I think it can definitely be shorter. I just thought I'd put it out there.

  • \$\begingroup\$ Pointfree is a bit shorter: length.head.filter(all=<<(==).head).(<$>[2..]).(!). \$\endgroup\$
    – Laikoni
    Commented Aug 6, 2017 at 9:15
  • \$\begingroup\$ @Laikoni Thanks! For some reason I wasn't able to figure out how to get it into point-free notation. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 6, 2017 at 14:52
  • \$\begingroup\$ I can recommend pointfree.io which is based on the point free converter of lambdabot. \$\endgroup\$
    – Laikoni
    Commented Aug 7, 2017 at 7:04
  • \$\begingroup\$ @Laikoni I use pointfree.io quite a bit. I must have not tried it here. I usually get pretty good results though. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 7, 2017 at 7:05

Husk, 13 11 bytes

-2 bytes thanks to zgarb


Try it online!

  • \$\begingroup\$ mm can be M, and ṠoΛ=← can be ȯ¬tu. There's not yet a built-in for checking that all elements of a list are equal... \$\endgroup\$
    – Zgarb
    Commented Aug 6, 2017 at 4:46
  • \$\begingroup\$ M is not even on the wiki yet :( \$\endgroup\$
    – H.PWiz
    Commented Aug 6, 2017 at 9:39
  • \$\begingroup\$ ΓoΛ= also works as four bytes \$\endgroup\$
    – H.PWiz
    Commented Aug 6, 2017 at 9:45
  • 1
    \$\begingroup\$ Oops, M should be in the docs, since we've had it for a while. I should fix that. But it's basically the dual of . \$\endgroup\$
    – Zgarb
    Commented Aug 6, 2017 at 11:32

05AB1E, 8 bytes


Try it online!

-1 thanks to kalsowerus.

  • \$\begingroup\$ L>вʒË}нg for 8 bytes \$\endgroup\$
    – kalsowerus
    Commented Aug 7, 2017 at 9:01
  • \$\begingroup\$ @kalsowerus Didn't know you can use lists like that...thanks! \$\endgroup\$ Commented Aug 7, 2017 at 9:04

Mathematica, 71 bytes


Try it online!


Brachylog, 12 bytes


Try it online!


Python 3, 92 87 bytes

5 bytes thanks to Halvard Hummel.

g=lambda n,b,s=1:s*(n<b)or(n%b**2%-~b<1)*g(n//b,b,s+1)
f=lambda n,b=2:g(n,b)or f(n,b+1)

Try it online!


Husk, 7 bytes


Try it online!


     tN  on numbers 2,3,4...
   MB    map input to digits in base 2,3,4...
 ḟE      find first list that only has one unique element
L        length of that

APL (Dyalog Unicode), 28 26 bytes

Saved 2 bytes thanks to Adám!


Try it online!

Pretty straightforward implementation.

                           1+⍳ ⍝ Make a range [2, n + 1]
                          ¨   ⍝ For each b in that range
                   ⊢⊥⍣¯1⍨    ⍝ Interpret n in base b (gives a vector of integers)
     ⍵/⍨                      ⍝ Keep the representations where
         1=≢∘∪¨⍵             ⍝ it's a repdigit
              ¨⍵             ⍝ For every representation,
          ≢∘∪                 ⍝ is the length of it, without duplicates
        1=                    ⍝ equal to 1?
   ≢¨                         ⍝ Get the length of each of the remaining representations
 ⌈/                           ⍝ Get the biggest length
  • \$\begingroup\$ You can remove the parenthesis with no change in functionality (though you'll gain a speed-up). \$\endgroup\$
    – Adám
    Commented Jan 18, 2022 at 7:03
  • \$\begingroup\$ @Adám Thanks, I didn't think to take advantage of APL's arrayness \$\endgroup\$
    – user
    Commented Jan 18, 2022 at 22:17

Mathematica, 58 bytes

FirstCase[#~IntegerDigits~Range[#+1],l:{a_ ..}:>Tr[1^l]]&

Throws an error (because base-1 is not a valid base), but it is safe to ignore.

Of course, it is okay to take the length of the first repdigit (FirstCase), since numbers in lower bases cannot be shorter than in higher bases.


CJam (17 bytes)


Online test suite. This is an anonymous block (function) which takes an integer on the stack and leaves an integer on the stack.

Works with brute force, using 3 as a fallback base to handle the special cases (input 1 or 2).


Perl 6, 49 bytes

{+first {[==] $_},map {[.polymod($^b xx*)]},2..*}

Try it online!


{                                               }  # A lambda.
                  map {                   },2..*   # For each base from 2 to infinity...
                        .polymod($^b xx*)          #   represent the input in that base,
                       [                 ]         #   and store it as an array.
  first {[==] $_},                                 # Get the first array whose elements
                                                   # are all the same number.
 +                                                 # Return the length of that array.

The polymod method is a generalization of Python's divmod: It performs repeated integer division using a given list of divisors, and returns the intermediate remainders.
It can be used to decompose a quantity into multiple units:

my ($sec, $min, $hrs, $days, $weeks) = $seconds.polymod(60, 60, 24, 7);

When passing a lazy sequence as the list of divisors, polymod stops when the quotient reaches zero. Thus, giving it an infinite repetition of the same number, decomposes the input into digits of that base:

my @digits-in-base-37 = $number.polymod(37 xx *);

I use this here because it allows arbitrarily high bases, in contrast to the string-based .base method which only supports up to base 36.

  • \$\begingroup\$ You can remove the [] around polymod by changing $_ to @_ \$\endgroup\$
    – Jo King
    Commented Nov 6, 2018 at 0:16

TI-BASIC, 37 bytes

Input N
If fPart(N(B-1)/(B^Ans-1

Prompts for N, returns output in Ans.


As an overview, for each possible base B in sequence it first calculates the number of digits of N when represented in base B, then checks whether N is divisible by the value represented by that same number of 1-digits in base B.

Input N            Ask the user for the value of N.
For(B,2,2N         Loop from base 2 to 2N. We are guaranteed a solution
                   at base N+1, and this suffices since N is at least 1.
int(log(NB)/log(B  Calculate the number of digits of N in base B,
                   placing the result in Ans.
                   This is equivalent to floor(log_B(N))+1.
          (B-1)/(B^Ans-1   The value represented by Ans consecutive
                           1-digits in base B, inverted.
If fpart(N         Check whether N is divisible by the value with Ans
                   consecutive 1-digits, by multiplying it by the inverse
                   and checking its fractional part.
                   Skips over the End if it was divisible.
End                Continue the For loop, only if it was not divisible.
                   The number of digits of N in base B is still in Ans.

K (ngn/k), 24 bytes


Try it online!

  • x{...}'2+!x call the inner {...} lambda on the input n, paired up with each number from 2..n+1
    • y\x convert the input to the current base
    • {1=#?x}# filter down to representations containing only a single unique digit
    • # return the length of this representation (0 if it contained multiple digits)
  • {|/...} return the maximum across all potential bases

Pyth, 13 bytes


Try it online!


Java 8, 111 bytes

n->{int r=0,i=1,l;for(String t;++i<n+2;r=(l=t.length())>r&t.matches("(.)\\1*")?l:r)t=n.toString(n,i);return r;}

Byte-count of 111 is also a rep-digit. ;)


Try it here.

n->{                            // Method with Integer as parameter return-type
  int r=0,                      //  Result-integer
      i=1,                      //  Index-integer
      l;                        //  Length-integer
  for(String t;                 //  Temp-String
      ++i<n+2;                  //  Loop from 2 to `n+2` (exclusive)
      r=                        //    After every iteration, change `r` to:
        (l=t.length())>r        //     If the length of `t` is larger than the current `r`
        &t.matches("(.)\\1*")?  //     and the current `t` is a rep-digit:
         l                      //      Change `r` to `l` (the length of the rep-digit)
        :                       //     Else:
         r)                     //      Leave `r` as is
    t=n.toString(n,i);          //   Set String representation of `n` in base-`i` to `t`
                                //  End of loop (implicit / single-line body)
  return r;                     //  Return the result-integer
}                               // End of method
  • \$\begingroup\$ Lambdas were introduced in Java 8. \$\endgroup\$
    – Jakob
    Commented Aug 7, 2017 at 12:19
  • 1
    \$\begingroup\$ @Jakob Woops.. Not sure why I typed 7.. Either because I recently looked back at a Java 7 answer of mine, or just a typo.. Thanks for the correction either way, should of course have been 8... >.> \$\endgroup\$ Commented Aug 7, 2017 at 12:20

Java 8, 79 bytes

A lambda from Integer to Integer.

n->{int m,b=2,l;for(;;b++){for(m=n,l=0;m>0&m%b==n%b;l++)m/=b;if(m<1)return l;}}

Ungolfed lambda

n -> {
    int m, b = 2, l;
    for (; ; b++) {
        for (m = n, l = 0; m > 0 & m % b == n % b; l++)
            m /= b;
        if (m < 1)
            return l;

Checks radices in increasing order from 2 until a rep-digit radix is found. Relies on the fact that the smallest such radix will correspond to a representation with the most digits.

m is a copy of the input, b is the radix, and l is the number of digits checked (and ultimately the length of the radix-b representation).


Burlesque, 24 bytes

(see correct solution below)


See in action.

J2jr@ -- boiler plate to build a list from 2..N
jbcz[ -- zip in N
{dgL[}m^ -- calculate base n of everything and compute length
>]    -- find the maximum.

At least if my intuition is right that a rep-digit representation will always be longest? Otherwise uhm...


:sm -- filter for "all elements are the same"
  • 1
    \$\begingroup\$ Base-2 representation will always be longest, try for example with input 26 and you'll see that your first solution is incorrect \$\endgroup\$
    – Leo
    Commented Aug 8, 2017 at 9:52

Ruby, 60 bytes

->n{(2..n).filter_map{d=n.digits _1

Attempt This Online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.