# Is this number a repdigit?

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

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

# V, 13 bytes

ylÍ"/.
ñ/ä
d


Try it online!

Leaves a string of .s if truthy, nothing if falsy (empty strings are truthy in Vim/V and vice versa)

# V, 6 bytes

ø^ˆ±*$ Try it online! Hexdump: 00000000: f85e 88b1 2a24 .^..*$


This uses a brand new operator that I haven't used in any PPCG answers before. The ø operator will count the number of matches of a given regex. In this case, the (compressed) regex is:

/^$$.$$\1*$/  That is, any character at the start of the line, followed by only that character until the end of the line. Swift , 53 Bytes var s=readLine()!,r=Set(s.characters);r.count>1 ?0:1  # 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. # 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. • Please specify the version of Python. – Leaky Nun Jun 8 '17 at 3:03 • @LeakyNun It's not customary to specify a version if the code works in all recent ones. – Dennis Jun 8 '17 at 3:04 • @Dennis If it is Python 2, I can suggest a golf. – Leaky Nun Jun 8 '17 at 3:23 • Now that the input specs are more lax, you can replace str(x) with x. – Leaky Nun Jun 8 '17 at 3:23 ## 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)  # 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. • Replace @echo off with @ in front of every command. Furthermore, there is no need for "" around usebackq, which can be abbreviated to useback, which shaves another 5 bytes off. – HackingAddict1337 Mar 29 '20 at 8:29 # 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. # 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.  # 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! # ><>, 12 bytes 1l2=?n@:}=*!  Try it online! Takes input through the -s flag and checks whether every digit is the same. # Pari/GP, 21 bytes n->#Set(digits(n))==1  Try it online! # 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. ## 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.

# 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


# Pyth, 4 3 bytes

!t{


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

Takes input as a string.

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

# 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


# MathGolf, 3 bytes

▀£┴


Try it online!

Takes input as a string.

### Explanation:

▀    Get unique characters
£   Get length of list
┴  Is equal to one?


# Pyt, 2 bytes

ą≡


Try it online!

Explanation:

   Implicit input
ą  Split digits
≡ Are all elements equal?


# Stax, 4 bytes

ë@╛α


Run and debug it

## Unpacked

Eu%v!


# Prolog, 43 bytes

f(N,D):-N==0;O is N//10,D is N-O*10,f(O,D).


Run the query with f(666,D) (or whatever other number you have). If it's a repdigit, it will return a value for D, otherwise, it will return false.

Try it in SWISH (By the way, I think if you make changes directly without creating a new notebook, it may modify the original, so please don't mess with it :))