Find the longest rep digit

17

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!

Post Rock Garf Hunter

Posted 2017-08-05T20:56:15.370

Reputation: 55 382

1Asuming base > 1 ? – H.PWiz – 2017-08-05T21:02:23.970

@H.PWiz Yes. base 1 doesn't really make any sense. – Post Rock Garf Hunter – 2017-08-05T21:02:46.467

...and input >= 3 I guess :) – Jonathan Allan – 2017-08-05T21:05:23.863

@JonathanAllan No, input can be as small as 1. Is there a problem with that? – Post Rock Garf Hunter – 2017-08-05T21:05:54.897

Oh OK, just makes 2 an edge case (the base being higher than the input) – Jonathan Allan – 2017-08-05T21:07:10.527

@JonathanAllan Yes 1 and 2 are edge cases, but to be clear you are not outputting the base with the longest rep digit just the length. – Post Rock Garf Hunter – 2017-08-05T21:08:07.080

2you can add test cases 63->6 and 1023->10 if you like – J42161217 – 2017-08-05T21:38:38.157

Rep-digits can use any digit, not just 1, right? I think it would be good to have more test cases to cover those. – xnor – 2017-08-06T02:11:26.880

1@WheatWizard I think 26 does it for instance, it's 222 in base 3. – xnor – 2017-08-06T02:15:10.267

222,333,444,555,666,777,888,999 all work here are the first 69 if my code is correct (including the edge case of 2) – Jonathan Allan – 2017-08-06T02:31:29.690

@xnor I must have made a mistake. I'll add that right away. :) – Post Rock Garf Hunter – 2017-08-06T02:37:37.640

1Can bases go above 10? If so, for bases > 10, should we include characters a-z? What about bases > 36? – Rick Hitchcock – 2017-08-06T11:20:43.483

6@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. – Post Rock Garf Hunter – 2017-08-06T14:49:47.280

Answers

9

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.

Jonathan Allan

Posted 2017-08-05T20:56:15.370

Reputation: 67 804

So...I'm not the only one to have figured out ZL is shorter than L€Ṁ... – Erik the Outgolfer – 2017-08-06T09:48:24.860

8

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>

Neil

Posted 2017-08-05T20:56:15.370

Reputation: 95 035

2I love the unnecessarily golfed test HTML – Jakob – 2017-08-06T05:54:04.977

6

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.

Post Rock Garf Hunter

Posted 2017-08-05T20:56:15.370

Reputation: 55 382

Pointfree is a bit shorter: length.head.filter(all=<<(==).head).(<$>[2..]).(!). – Laikoni – 2017-08-06T09:15:58.523

@Laikoni Thanks! For some reason I wasn't able to figure out how to get it into point-free notation. – Post Rock Garf Hunter – 2017-08-06T14:52:46.130

I can recommend http://pointfree.io/ which is based on the point free converter of lambdabot.

– Laikoni – 2017-08-07T07:04:57.987

@Laikoni I use pointfree.io quite a bit. I must have not tried it here. I usually get pretty good results though. – Post Rock Garf Hunter – 2017-08-07T07:05:58.007

5

Husk, 13 11 bytes

-2 bytes thanks to zgarb

L←fȯ¬tuMBtN

Try it online!

H.PWiz

Posted 2017-08-05T20:56:15.370

Reputation: 10 962

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... – Zgarb – 2017-08-06T04:46:31.800

M is not even on the wiki yet :( – H.PWiz – 2017-08-06T09:39:35.750

ΓoΛ= also works as four bytes – H.PWiz – 2017-08-06T09:45:34.920

1Oops, 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 . – Zgarb – 2017-08-06T11:32:44.567

3

Mathematica, 71 bytes

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

Try it online!

J42161217

Posted 2017-08-05T20:56:15.370

Reputation: 15 931

3

05AB1E, 8 bytes

L>вʒË}нg

Try it online!

-1 thanks to kalsowerus.

Erik the Outgolfer

Posted 2017-08-05T20:56:15.370

Reputation: 38 134

L>вʒË}нg for 8 bytes – kalsowerus – 2017-08-07T09:01:15.470

@kalsowerus Didn't know you can use lists like that...thanks! – Erik the Outgolfer – 2017-08-07T09:04:35.267

2

Brachylog, 12 bytes

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

Try it online!

Erik the Outgolfer

Posted 2017-08-05T20:56:15.370

Reputation: 38 134

2

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!

Leaky Nun

Posted 2017-08-05T20:56:15.370

Reputation: 45 011

-5 bytes – Halvard Hummel – 2017-08-07T15:42:42.197

1

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.

JungHwan Min

Posted 2017-08-05T20:56:15.370

Reputation: 13 290

1

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

Peter Taylor

Posted 2017-08-05T20:56:15.370

Reputation: 41 901

1

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.

smls

Posted 2017-08-05T20:56:15.370

Reputation: 4 352

You can remove the [] around polymod by changing $_ to @_ – Jo King – 2018-11-06T00:16:44.323

1

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.

calc84maniac

Posted 2017-08-05T20:56:15.370

Reputation: 165

0

Pyth, 13 bytes

lhf!t{TjLQ}2h

Try it online!

Erik the Outgolfer

Posted 2017-08-05T20:56:15.370

Reputation: 38 134

0

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

Kevin Cruijssen

Posted 2017-08-05T20:56:15.370

Reputation: 67 575

Lambdas were introduced in Java 8. – Jakob – 2017-08-07T12:19:19.627

1@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... >.> – Kevin Cruijssen – 2017-08-07T12:20:04.860

0

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

Jakob

Posted 2017-08-05T20:56:15.370

Reputation: 2 428

0

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"

mroman

Posted 2017-08-05T20:56:15.370

Reputation: 1 382

1Base-2 representation will always be longest, try for example with input 26 and you'll see that your first solution is incorrect – Leo – 2017-08-08T09:52:16.543