# Find the longest rep digit

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

111_2
21_3
13_4
12_5
11_6
10_7
7_8


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!

• Asuming base > 1 ? Aug 5 '17 at 21:02
• you can add test cases 63->6 and 1023->10 if you like Aug 5 '17 at 21:38
• @WheatWizard I think 26 does it for instance, it's 222 in base 3.
– xnor
Aug 6 '17 at 2:15
• Can bases go above 10? If so, for bases > 10, should we include characters a-z? What about bases > 36? Aug 6 '17 at 11:20
• @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. Aug 6 '17 at 14:49

# Jelly,  9  7 bytes

-2 thanks to caird coinheringaahing (‘Ḋ$ -> ‘€ and use of the newer alias for Ðf, Ƈ.) b‘€EƇZL  A monadic link accepting and returning numbers Try it online! or see a test suite (inputs one to 32 inclusive). ### How? 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: b‘€EƇZLo1  ...just for the Lo1z. • So...I'm not the only one to have figured out ZL is shorter than L€Ṁ... Aug 6 '17 at 9:48 • 7 bytes Jun 25 at 3:51 • @cairdcoinheringaahing Good spot on the ‘€, this was about a year into my use of Jelly, I should probably have known better by then! Jun 25 at 18:46 ## JavaScript (ES6), 62 bytes f=(n,b=2,l=0,d=n)=>d?n%b<1|n%b-d%b?f(n,b+1):f(n,b,l+1,d/b|0):l <input oninput=o.textContent=f(this.value)><pre id=o> • I love the unnecessarily golfed test HTML Aug 6 '17 at 5:54 # Haskell, 8681 79 bytes 2 bytes saved thanks to Laikoni 0!y=[] x!y=mod x y:div x y!y length.head.filter(all=<<(==).head).(<$>[2..]).(!)


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.

• Pointfree is a bit shorter: length.head.filter(all=<<(==).head).(<$>[2..]).(!). Aug 6 '17 at 9:15 • @Laikoni Thanks! For some reason I wasn't able to figure out how to get it into point-free notation. Aug 6 '17 at 14:52 • I can recommend pointfree.io which is based on the point free converter of lambdabot. Aug 7 '17 at 7:04 • @Laikoni I use pointfree.io quite a bit. I must have not tried it here. I usually get pretty good results though. Aug 7 '17 at 7:05 # Husk, 13 11 bytes -2 bytes thanks to zgarb L←fȯ¬tuMBtN  Try it online! • 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... Aug 6 '17 at 4:46 • M is not even on the wiki yet :( Aug 6 '17 at 9:39 • ΓoΛ= also works as four bytes Aug 6 '17 at 9:45 • 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 Ṁ. Aug 6 '17 at 11:32 # Mathematica, 71 bytes Max[L/@Select[Array[a~IntegerDigits~#&,a=#,2],(L=Length)@Union@#==1&]]&  Try it online! # 05AB1E, 8 bytes L>вʒË}нg  Try it online! -1 thanks to kalsowerus. • L>вʒË}нg for 8 bytes Aug 7 '17 at 9:01 • @kalsowerus Didn't know you can use lists like that...thanks! Aug 7 '17 at 9:04 # Brachylog, 12 bytes +₁⟦₁b∋;?ḃ₍=l  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! • -5 bytes Aug 7 '17 at 15:42 # 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) {_,2>3+fb{)-!}=,}  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! ### Explanation { } # 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.

• You can remove the [] around polymod by changing \$_ to @_
– Jo King
Nov 6 '18 at 0:16

## TI-BASIC, 37 bytes

Input N
For(B,2,2N
int(log(NB)/log(B
If fPart(N(B-1)/(B^Ans-1
End


Prompts for N, returns output in Ans.

# Explanation

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.


# APL (Dyalog Unicode), 28 bytes

{⌈/≢¨⍵/⍨(1=≢∘∪)¨⍵}⊢⊥⍣¯1⍨¨1+⍳


Try it online!

Pretty straightforward implementation.

{⌈/≢¨⍵/⍨(1=≢∘∪)¨⍵}⊢⊥⍣¯1⍨¨1+⍳
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


# Pyth, 13 bytes

lhf!t{TjLQ}2h


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. ;)

Explanation:

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

• Lambdas were introduced in Java 8. Aug 7 '17 at 12:19
• @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... >.> Aug 7 '17 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)

J2jr@jbcz[{dgL[}m^>]


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...

J2jr@jbcz[{dg}m^:sm)L[>]

:sm -- filter for "all elements are the same"

• Base-2 representation will always be longest, try for example with input 26 and you'll see that your first solution is incorrect
– Leo
Aug 8 '17 at 9:52