21
\$\begingroup\$

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!

\$\endgroup\$
13
  • 1
    \$\begingroup\$ Asuming base > 1 ? \$\endgroup\$
    – H.PWiz
    Aug 5, 2017 at 21:02
  • 2
    \$\begingroup\$ you can add test cases 63->6 and 1023->10 if you like \$\endgroup\$
    – ZaMoC
    Aug 5, 2017 at 21:38
  • 1
    \$\begingroup\$ @WheatWizard I think 26 does it for instance, it's 222 in base 3. \$\endgroup\$
    – xnor
    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\$ 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
    Aug 6, 2017 at 14:49

20 Answers 20

11
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ So...I'm not the only one to have figured out ZL is shorter than L€Ṁ... \$\endgroup\$ Aug 6, 2017 at 9:48
  • \$\begingroup\$ 7 bytes \$\endgroup\$ 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\$ Jun 25, 2021 at 18:46
9
\$\begingroup\$

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>

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

Haskell, 86 81 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.

\$\endgroup\$
4
  • \$\begingroup\$ Pointfree is a bit shorter: length.head.filter(all=<<(==).head).(<$>[2..]).(!). \$\endgroup\$
    – Laikoni
    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
    Aug 6, 2017 at 14:52
  • \$\begingroup\$ I can recommend pointfree.io which is based on the point free converter of lambdabot. \$\endgroup\$
    – Laikoni
    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
    Aug 7, 2017 at 7:05
5
\$\begingroup\$

Husk, 13 11 bytes

-2 bytes thanks to zgarb

L←fȯ¬tuMBtN

Try it online!

\$\endgroup\$
4
  • \$\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
    Aug 6, 2017 at 4:46
  • \$\begingroup\$ M is not even on the wiki yet :( \$\endgroup\$
    – H.PWiz
    Aug 6, 2017 at 9:39
  • \$\begingroup\$ ΓoΛ= also works as four bytes \$\endgroup\$
    – H.PWiz
    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
    Aug 6, 2017 at 11:32
4
\$\begingroup\$

05AB1E, 8 bytes

L>вʒË}нg

Try it online!

-1 thanks to kalsowerus.

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

Mathematica, 71 bytes

Max[L/@Select[Array[a~IntegerDigits~#&,a=#,2],(L=Length)@Union@#==1&]]&

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Brachylog, 12 bytes

+₁⟦₁b∋;?ḃ₍=l

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
1
2
\$\begingroup\$

Husk, 7 bytes

LḟEMBtN

Try it online!

Explanation

LḟEMBtN
     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
\$\endgroup\$
2
+100
\$\begingroup\$

APL (Dyalog Unicode), 28 26 bytes

Saved 2 bytes thanks to Adám!

{⌈/≢¨⍵/⍨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
\$\endgroup\$
2
  • \$\begingroup\$ You can remove the parenthesis with no change in functionality (though you'll gain a speed-up). \$\endgroup\$
    – Adám
    Jan 18, 2022 at 7:03
  • \$\begingroup\$ @Adám Thanks, I didn't think to take advantage of APL's arrayness \$\endgroup\$
    – user
    Jan 18, 2022 at 22:17
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

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

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.
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 24 bytes

{|/x{#{1=#?x}#y\x}'2+!x}

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
\$\endgroup\$
0
\$\begingroup\$

Pyth, 13 bytes

lhf!t{TjLQ}2h

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Lambdas were introduced in Java 8. \$\endgroup\$
    – Jakob
    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\$ Aug 7, 2017 at 12:20
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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"
\$\endgroup\$
1
  • 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
    Aug 8, 2017 at 9:52
0
\$\begingroup\$

Ruby, 60 bytes

->n{(2..n).filter_map{d=n.digits _1
!d.uniq[1]&&d.size}.max}

Attempt This Online!

\$\endgroup\$

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.