36
\$\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
  • 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

86 Answers 86

1 2
3
0
\$\begingroup\$

Tcl, 53 bytes

proc r n {expr 1==1[string trim $n [string in $n 0]]}

"string trim" removes leading and trailing occurrences of all its second argument's characters from its first argument (usually used for whitespace). Second argument here is the first digit of $n. For a repdigit an empty string remains, and attaching that to 1 remains 1. For number 42424, 242 would remain after the trim, so 1 is not equal to 1242.

PS: attaching to 1 is a golfing-thing that saved two bytes versus comparing the trim with empty string.

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

Pyke, 3 bytes

}t!

Try it here!

}   -   uniquify(input)
 t  -  ^[:-1]
  ! - not ^
\$\endgroup\$
0
\$\begingroup\$

PHP, 34 bytes

<?=preg_match('#^(.)\1*$#',$argn);

Try it online!

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

Excel, 31 bytes

=MOD(A2,10)*(10^LEN(A2)-1)/9=A2
\$\endgroup\$
0
\$\begingroup\$

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)

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

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.

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

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\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 29 '20 at 8:29
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\$
3
  • 1
    \$\begingroup\$ You don't need l. \$\endgroup\$ 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\$ 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\$
0
\$\begingroup\$

Stax, 4 bytes

ë@╛α

Run and debug it

Unpacked

Eu%v!
\$\endgroup\$
0
\$\begingroup\$

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

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

Zsh --extendedglob, 14 bytes

>$1
: ^{1..9}#

Try it online!

Outputs via exit code: 0 is not a repdigit, 1 is a repdigit.

  • >$1 - create the file according to the input
  • ^{1..9}# - construct the string ^1# ^2# ... ^9#
  • then find a file matching all of the patterns ^1# ^2# ... ^9#
    • # means zero or more of the preceding digit (but since a file must be at least 1 character, it actually means 1 or more)
    • ^ means logical NOT
      • by DeMorgan's law, this creates a logical OR of the patterns ^1# ^2# ... ^9# (just with inverted output, but we can cope with that)
    • if no file matching all of these patterns exists - that is, the file is not not a repeated digit - there will be an error and the exit code will be 1.
  • : - and do nothing with it (otherwise it would try to execute the file as a command, which would also exit with an error)

--extendedglob is required to enable the ^ and # glob operators.

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

Vyxal, 1 byte

Try it Online!

Simply checks if all the digits are equal to each other

\$\endgroup\$
1 2
3

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.