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

16 Answers 16

9
\$\begingroup\$

Jelly, 9 bytes

b‘Ḋ$EÐfZL

A monadic link accepting and returning numbers

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

How?

b‘Ḋ$EÐfZL - Link: number, n
   $      - last two links as a monad:
 ‘        -   increment = n+1
  Ḋ       -   dequeue (with implicit range build) = [2,3,4,...,n+1]
b         - convert to those bases
     Ðf   - 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ÐfZLo1

For the Lo1z.

\$\endgroup\$
  • \$\begingroup\$ So...I'm not the only one to have figured out ZL is shorter than L€Ṁ... \$\endgroup\$ – Erik the Outgolfer Aug 6 '17 at 9:48
8
\$\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\$
  • 2
    \$\begingroup\$ I love the unnecessarily golfed test HTML \$\endgroup\$ – Jakob Aug 6 '17 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\$
  • \$\begingroup\$ Pointfree is a bit shorter: length.head.filter(all=<<(==).head).(<$>[2..]).(!). \$\endgroup\$ – Laikoni Aug 6 '17 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 '17 at 14:52
  • \$\begingroup\$ I can recommend pointfree.io which is based on the point free converter of lambdabot. \$\endgroup\$ – Laikoni Aug 7 '17 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 '17 at 7:05
5
\$\begingroup\$

Husk, 13 11 bytes

-2 bytes thanks to zgarb

L←fȯ¬tuMBtN

Try it online!

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

Mathematica, 71 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 8 bytes

L>вʒË}нg

Try it online!

-1 thanks to kalsowerus.

\$\endgroup\$
  • \$\begingroup\$ L>вʒË}нg for 8 bytes \$\endgroup\$ – kalsowerus Aug 7 '17 at 9:01
  • \$\begingroup\$ @kalsowerus Didn't know you can use lists like that...thanks! \$\endgroup\$ – Erik the Outgolfer Aug 7 '17 at 9:04
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
\$\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\$
  • \$\begingroup\$ You can remove the [] around polymod by changing $_ to @_ \$\endgroup\$ – Jo King Nov 6 '18 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\$
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\$
  • \$\begingroup\$ Lambdas were introduced in Java 8. \$\endgroup\$ – Jakob Aug 7 '17 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\$ – Kevin Cruijssen Aug 7 '17 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
    \$\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 '17 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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