33
\$\begingroup\$

Challenge

A repdigit is a non-negative integer whose digits are all equal.

Create a function or complete program that takes a single integer as input and outputs a truthy value if the input number is a repdigit in base 10 and falsy value otherwise.

The input is guaranteed to be a positive integer.

You may take and use input as a string representation in base 10 with impunity.

Test cases

These are all repdigits below 1000.

1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
111
222
333
444
555
666
777
888
999

A larger list can be found on OEIS.

Winning

The shortest code in bytes wins. That is not to say that clever answers in verbose languages will not be welcome.

\$\endgroup\$
  • 2
    \$\begingroup\$ Related. \$\endgroup\$ – Leaky Nun Jun 8 '17 at 3:02
  • \$\begingroup\$ @AidanF.Pierce What's the biggest number the input will be? \$\endgroup\$ – stevefestl Jun 14 '17 at 9:00

77 Answers 77

0
\$\begingroup\$

Swift , 53 Bytes

var s=readLine()!,r=Set(s.characters);r.count>1 ?0:1
\$\endgroup\$
0
\$\begingroup\$

TI-BASIC, 17 16 bytes

log(1+.9X/fPart(.1X:Ans=int(Ans

Takes input on X. TI-BASIC is token based, all tokens are one byte.

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

Python, 27 22 bytes:

Just to get things started.

lambda x:len(set(x))<2

Converts to a string, converts the string into a set of all distinct characters in that string, and checks whether there aren't multiple elements in the set.

5 bytes saved thanks to @LeakyNun's reminder.

\$\endgroup\$
  • \$\begingroup\$ Please specify the version of Python. \$\endgroup\$ – Leaky Nun Jun 8 '17 at 3:03
  • 3
    \$\begingroup\$ @LeakyNun It's not customary to specify a version if the code works in all recent ones. \$\endgroup\$ – Dennis Jun 8 '17 at 3:04
  • \$\begingroup\$ @Dennis If it is Python 2, I can suggest a golf. \$\endgroup\$ – Leaky Nun Jun 8 '17 at 3:23
  • \$\begingroup\$ Now that the input specs are more lax, you can replace str(x) with x. \$\endgroup\$ – Leaky Nun Jun 8 '17 at 3:23
0
\$\begingroup\$

CJam, 6 bytes

qL|,1=

Explanation:

q  e# Get the input: "1311211"
L| e# Get unique elements: "132"
,  e# Length: 3
1= e# Equal to 1: 0 (false)
\$\endgroup\$
0
\$\begingroup\$

Batch, 177 bytes

@echo off
set s=
set z=
<nul set/p=.%1>t
for /f "usebackq" %%G in ('t')do set z=%%~zG
for /l %%G in (2,1,%z%)do call set s=1%%s%%
set/ar=%1%%%s%
if %r%==0 echo %r%&exit/b
echo 1

Not properly golfed, yet.

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

Noether, 39 bytes

""~bI~aL(ai/~c{bc/}{}{bc+~b}i1+~i)bL1=P

Try it here!

It takes input as a number enclosed in quotation marks.

This works by looping through the input string, and, at the start, adding the first character to the string, B. As it loops, if the current character in the input isn't in B, it is appended to B. At the end, if the number is a repdigit, B will only be one character long.

Outputs 1 for true, 0 for false.

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

Pushy, 5 bytes

sK=P#

Try it online!

                                              Falsy Example:      Truthy Example:
       \ Implicit: Input on stack             [22121]             [5555]
s      \ Split into digits                    [2, 2, 1, 2, 2]     [5, 5, 5, 5]
 K=    \ Pop last, compare with all others    [1, 1, 0, 1]        [1, 1, 1]
   P#  \ Print the stack's product            PRINT: 0            PRINT: 1

6 bytes

I prefer this method as it's sweet and simple, but unfortunately it's one byte longer:

suLtn#

Try it online!

        \ Implicit: Input on stack
s       \ Split into digits
 u      \ Make stack into a set of itself
  Lt    \ Get the stack length - 1
        \ This will be 0 for a valid repdigit, a positive value otherwise
    n   \ Boolean negate: this maps 0 -> 1, and everything else to 0.
     #  \ Print result.
\$\endgroup\$
0
\$\begingroup\$

Tcl, 48 bytes

proc R n {expr 1[regsub -all ^(.)\\1* $n ""]==1}

Try it online!


Tcl, 52 bytes

proc R n {expr [llength [lsort -u [split $n ""]]]<2}

Try it online!

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

><>, 12 bytes

1l2=?n@:}=*!

Try it online!

Takes input through the -s flag and checks whether every digit is the same.

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

Pari/GP, 21 bytes

n->#Set(digits(n))==1

Try it online!

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

SmileBASIC 3, 26 bytes

INPUT N$?N$[0]*LEN(N$)==N$

Takes input as a string, prints 0 for false and 1 for true. If we repeat the first character of the input length(input) times and that is equal to the input, then it's a repdigit.

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

PHP, 42 Bytes

Try it online

(42 Bytes because i forgot Towel day on may 25)

Code

<?=!(($a=$argv)%str_repeat(1,strlen($a)));

Explanation

Every number like 11, 22222,5555555555 always will be dvisible by 1 times the number length it just checks that, if the % between the numbers is 0 outputs 1 else empty.

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

Forth (gforth), 87 bytes

: m 10 /mod swap ; : f 1 swap m >r begin ?dup 0> while m r@ = rot * swap repeat rdrop ;

Try it online!

Explanation

The algorithm is approximately:

  1. Divide by 10, take remainder and store on return stack
  2. Repeatedly divide by 10 and compare remainder to stored value
  3. When quotient equals 0, stop loop and clean return stack

Code Explanation

: m 10 /mod swap ;        \ helper to grab the quotient and remainder after dividing by 10

1 swap                    \ place 1 as a result value on the stack and move to the bottom
m >r                      \ divide by 10 and place remainder on the return stack
begin                     \ start an indefinite loop
  ?dup 0>                 \ duplicate if not equal to 0 and check if greater than 0
while                     \ if greater than 0 execute loop body, else go the end of the loop
  m r@ =                  \ divide by 10, check if remainder equals our check value
  rot *                   \ move the result value to the top and multiply by result
  swap                    \ move the result value back to the boottom
repeat                    \ end the loop body
rdrop                     \ clean the return stack
\$\endgroup\$
0
\$\begingroup\$

Pyth, 4 3 bytes

!t{

Try it online!
-1 thanks to Erik the Outgolfer!

Takes input as a string.

\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need l. \$\endgroup\$ – Erik the Outgolfer Jun 7 '18 at 20:32
  • \$\begingroup\$ @EriktheOutgolfer huh. Not how i expected t to behave, but certainly useful. Thanks! \$\endgroup\$ – hakr14 Jun 7 '18 at 20:36
  • \$\begingroup\$ Some of Pyth's built-ins are type-overloaded. \$\endgroup\$ – Erik the Outgolfer Jun 7 '18 at 21:35
0
\$\begingroup\$

APL(NARS), 16 chars, 32 bytes

{(⍴w)=+/w=↑w←⍕⍵}

test:

  {(⍴w)=+/w=↑w←⍕⍵}¨1 2 23 33 93 9999999
 1  1  0  1  0  1 
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 3 bytes

▀£┴

Try it online!

Takes input as a string.

Explanation:

▀    Get unique characters
 £   Get length of list
  ┴  Is equal to one?
\$\endgroup\$
0
\$\begingroup\$

Pyt, 2 bytes

ą≡

Try it online!

Explanation:

   Implicit input
ą  Split digits
 ≡ Are all elements equal?
\$\endgroup\$

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.