# 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

# MATL (6 5 bytes or 2 bytes for Luis Mendo solution)

Vun1=


Try it online!

## Explanation

V     % convert to string
un    % find unique characters and count them
1=    % if there is only one unique character, then we pass.


Luis Mendo solution (see comments):

&=


Outputs a truthy array (all 1's) if repdigit, or a falsy array (some 0 in the array) if not a perfect repdigit.

Try it online!

• Since any truthy/falsy outputs are valid, you can use V&=; or j&= to avoid floating-point limitations. Or even &=, since inputting a string is allowed – Luis Mendo May 27 at 9:47
• Hmm, I'm afraid I can't follow. I tried replacing "1=" with "&=" and it didn't work. Also tried "V&=" as the whole code, in case that's what you meant. – DrQuarius Jul 17 at 11:37
• I mean just &=. A non-empty array containing only ones is truthy, and an array containing some zero is falsy (see link in my previous comment for explanation about truthy/falsy) – Luis Mendo Jul 17 at 16:06
• Wow, ok. That's a very simple solution then. Will add. – DrQuarius Aug 2 at 4:42

# Python 3, 23 bytes

lambda s:s==s[0]*len(s)


Try it online!

Not shorter than @shadow's answer, but I thought it was interesting. Should work in Python 2 as well.

# Ruby, 13 bytes (12 + '-n' flag)

p~/^(.)\1*$/  Try it online! # C#, 38 bytes using System.Linq;s=>s.All(c=>c==s[0])  Or alternatively for 44 bytes: using System.Linq;s=>s.Distinct().Count()==1  • I'm no C# developer, but do you really require using System.Linq? I've seen plenty of C# lambda answers without that. – Olivier Grégoire Jun 8 '17 at 12:37 • @OlivierGrégoire Linq is required for All, it's like needing an external package in C and requiring to import it. If I am using Linq I must include the using. – TheLethalCoder Jun 8 '17 at 12:40 ## Clojure, 17 12 bytes You may take and use input as a string representation in base 10 with impunity. Oh in that case: #(apply = %)  Original: #(apply =(str %))  # QBIC, 36 23 bytes [_l!:$||p=p*z+1]?b%p=0


## Explanation

    :     Read a number from the cmd line
! $cast it to string _l | Take its length [ | And run a FOR-loop from 1 to that length p= p starts out as 0. set it to p*z itself multiplied by 10 (z=10 in QBIC) (still 0 on the first run +1 then add 1. On consecutive FOR-loops yields 1, 11, 111, .... ] Close the FOR loop ?b%p=0 PRINT -1 if b mod p is 0 (ie 444 % 111 = 0), or 0 otherwise  ## ><>, 26 24 bytes !vi:0(?v::&r&=? >0n;n1<  Reads the input as a string and reads every character in a loop  i // Take the next character and pushes it onto the stack :0(?v // If the end of the input has been reached, goto the successful termination code :: // Duplicate the top of the stack twice !v &r&=? // Pop the top of the stack, pop the bottom of the stack, and compare them. If they are not equal, goto the failure termination code >0n; // Failure termination code. Prints a 0 and terminates ;n1< // Success termination code. Prints a 1 and terminates  # Perl 6, 12 bytes {[==] .comb}  Reduces the list of characters in the input number with the numeric equality operator. ## PowerShell, 23 bytes "$args"-match'^(.)\1*$'  Save as repdigit.ps1 and run with PS C:\wherever\repdigit.ps1 444, outputs True or False. If you want the Python len(set(input))==1 style, it costs more at 32 bytes: @($args-split''|group).count-eq2


(Noting that the split on the space between chars also outputs an empty start and end string as well as the characters).

# PHP, 28 bytes

<?=!count_chars($argn,3)[1];  count_chars with mode=3 creates a string with all different characters in $argn.
If there is only one, the second character will be empty == falsy.

Run as pipe with -F.

# Smalltalk, 21 19 bytes

[:s|s asSet size<2]


Try it online!

You can execute the block above by sending the message value: to the block with a String. A String in Smalltalk is written with single quotes. A longer version of the block could accept an Integer. It would take 30 28 bytes.

[:i|i asString asSet size<2]


I tried both solutions in the current online version of Amber Smalltalk and Pharo Smalltalk version 6.0.

Thanks to the suggestion in the comments I was able to remove the spaces before and after the <. It's been many years since I wrote Smalltalk but I doubt I ever tried that in practice. The shorter version worked in both versions of Smalltalk I tested.

• Are the spaces around the < necessary? I haven't used small talk very much, but most languages wont require that sort of thing. – Sriotchilism O'Zaic Jun 14 '17 at 3:51
• Yes, the spaces are necessary. The syntax of Smalltalk is Object<space>message. I was able to remove two spaces before and after the block separator |. – Donald Raab Jun 14 '17 at 3:56
• I stand corrected. I tried removing the spaces before and after the < and it seems to work. – Donald Raab Jun 14 '17 at 3:58

## Check, 135134132 124 bytes

 [r            #v
#:>10%:]R+r->\#v#?
#v
# >10-\)\#     # ?
d #             ^
#R:
:>=r,#v
#ddd[=#(:@:@=R-?
d\:!:R *
o>]=d#^


Input should be passed as a command-line argument. Outputs some unprintables for truthy and only zero bytes for falsey. Always terminates with an IndexError due to the fact that it abuses an interpreter bug.

### How does it work?

This code is divided into two segments. The first half turns the input integer into a list of digits, and the second half checks that all of its digits are equal to the first digit (i.e. they are all equal).

It roughly corresponds to the following pseudocode:

1. Read input and call it i.
2. Create an empty array and store it in the register. ([r)
3. If i is 0, go to step 13.
4. Take the number modulo 10. Call it x.
5. Prepend x to the register and store it back in the register.
6. Subtract x from i.
7. Create a counter, starting at 0.
8. If i is 0, go to step 12.
9. Increment the counter.
10. Decrement i by 10.
11. Go back to step 8.
12. Set i to whatever value is now in the counter.
13. Go back to step 3.
14. Load the register, which is now an array containing the digits of the input. Call this d.
15. Get the first element in d and store it in the register.
16. Create a counter called j, initialized to the length of d.
17. Decrement j.
18. If the jth element of d is not equal to the value of the register, crash the program. (We found a digit that is not correct.)
19. Otherwise, print some random junk !j times. This only prints something if j == 0.
20. Create an array of length 1 and get the !jth element. This will crash the program if j == 0.
21. If the program has not yet crashed, j must not yet be zero, so go back to step 17.

# Julia, 30 bytes

f(n)=length(Set(string(n)))==1


## F#, 38 bytes

let f n=Seq.length(Seq.countBy id n)=1


Try it online!

# QBasic, 37 32 bytes

INPUT n$?n$=STRING$(LEN(n$),n$)  -5 bytes thanks to steenbergh The STRING$ function takes two arguments, a number n and a string s$, and constructs a string consisting of n copies of the first character of s$.* So this code reads our number as a string n$, generates a string consisting of LEN(n$) copies of n$'s first character, and checks to see if it is equal to n$.

* The second argument can also be an integer codepoint, in which case it repeats the character corresponding to that codepoint.

• Are you sure you need the ASC? Often QBasic simply takes the 1st char of a string when it expects only 1 char but the string itself is longer. I think, but can't test atm, that this will work fine too: INPUT n$:?n$=STRING$(LEN(n$),n$) – steenbergh Jun 20 '18 at 13:34 • Confirmed on QB4.5: if we input n$ as 123, then STRING$(3, n$) is 111. – steenbergh Jun 20 '18 at 16:22
• @steenbergh [facepalm] I just read that in the help file, too. Thanks! – DLosc Jun 20 '18 at 20:36

# Whitespace, 96 93 bytes

[S S S N
_Push_0][S N
S _Duplicate][T N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][N
S S N
_Create_Label_LOOP][S N
S _Duplicate][S S S T   T   S S T   S S N
_Push_100][T    S T T   _Modulo][S S S T    S T T   N
_Push_11][T S T T   _Modulo][N
T   S S N
_If_0_Jump_to_label_NEXT][S S S T   S S T   N
_Push_9][S N
T   _Swap_top_two][T    S T S _Integer_division][T  N
S T _Print_as_integer][N
S S S N
_Create_Label_NEXT][S S S T S T S N
_Push_10][T S T S _Integer_division][N
S N
N
_Jump_to_Label_LOOP]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Port of @Dennis♦' C answer, so also outputs a positive digit ([1,9]) as truthy and 0 as falsey.

Try it online (with raw spaces, tabs and new-lines only).

Pseudo-code:

Integer i = STDIN as integer
Start LOOP:
If(i modulo-100 modulo-11 == 0):
i = i integer-divided by 10
Go to next iteration of LOOP
i = 9 / i
Print i to STDOUT as integer
Stop the program with an error


# Taxi, 730 706 bytes

-24 bytes by eliminating linebreaks.

Clever answer in a verbose language? Check and check.

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Pickup a passenger going to Cyclone.[B]Switch to plan D if no one is waiting.Pickup a passenger going to Crime Lab.Go to Cyclone:n 1 l 3 l.Pickup a passenger going to Crime Lab.Pickup a passenger going to Crime Lab.Go to Crime Lab:n 2 r 2 r.Switch to plan C if no one is waiting.Pickup a passenger going to Cyclone.Go to Fueler Up:n.Go to Chop Suey:n 3 r 1 l.Switch to plan B.[C]0 is waiting at Writer's Depot.Go to Writer's Depot:n 4 l 2 l.Pickup a passenger going to Cyclone.Go to Chop Suey:n 3 r 3 r.[D]Go to Cyclone:n 1 l 3 l.Pickup a passenger going to Post Office.Go to Post Office:s 1 l 2 r 1 l.


Try it online!

Try it online with linebreaks!

Doesn't return to the Taxi Garage after the program ends, so the boss fires me, and it exits with an error.

What this program actually does is checks whether or not the input consists of any repeating character, and outputs that character if it does, or 0 if it doesn't.

# Alternative solution, 602582541 445 bytes

-20 bytes by eliminating linebreaks.

-41 bytes by having the program error out upon a false result.

-96 bytes by having the program output via error message.

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Pickup a passenger going to Cyclone.[B]Switch to plan C if no one is waiting.Pickup a passenger going to Crime Lab.Go to Cyclone:n 1 l 3 l.Pickup a passenger going to Crime Lab.Pickup a passenger going to Crime Lab.Go to Crime Lab:n 2 r 2 r.Pickup a passenger going to Cyclone.Go to Fueler Up:n.Go to Chop Suey:n 3 r 1 l.Switch to plan B.[C]


Try it online!

Try it online with linebreaks!

This alternative solution errors out with The boss couldn't find your taxi in the garage. You're fired! if the input is a repdigit, and no outgoing passengers found if the input is not a repdigit (meaning this also works with 0...or 00, or 000...).

• For the second program? Sure, I could exit another way. – JosiahRyanW Oct 9 '18 at 0:45
• I know, I made the modification. – JosiahRyanW Oct 9 '18 at 0:49
• Note that a program can output via exit code, so theoretically you could have it crash if not a repdigit and exit normally otherwise instead of outputting – Jo King Oct 9 '18 at 0:55
• Could it crash in two different ways depending on result? – JosiahRyanW Oct 9 '18 at 0:57
• I think so? Relevant meta, but I don't see any reason not to allow it, since it is easily observable. Edit: ah, I realise it would be two different outputs to STDERR, so you're good – Jo King Oct 9 '18 at 1:05

# J, 6 bytes

1=#@~.


Try it online!

Take unique elements; does its length equal 1?

/:-:\:


Try it online!

Does ascending sorting order equal descending sorting order?

-:##{.


Try it online!

Does the first element, duplicated to the length, equal the original?

-:1&|.


Try it online!

Does the input, rotated once, equal itself?

# Pascal (FPC), 8175 68 bytes

var n:string;begin read(n);write(n=StringOfChar(n[1],length(n)))end.


Try it online!

It seems that taking a number as string is shorter.

### 75 bytes - taking the number as integer:

Thanks to @Ørjan Johansen for -6 bytes - mod 100 mod 11 trick

var n:word;begin read(n);while n mod$64mod$B=0do n:=n div$A;write(n<=9)end.  Try it online! $64, $B and $A are hexadecimal constants, they eliminate some whitespace that would be needed for their decimal counterparts.

• I think you can shave off a bit with the mod 100 mod 11 trick some others are using. – Ørjan Johansen Oct 8 '18 at 20:08
• @ØrjanJohansen Thanks, a good one! – AlexRacer Oct 8 '18 at 21:19

# Pepe, 48 bytes

rrEEreeeEEeeeErEeREEeREEREEEEEEErEEEEreErEEEeReE


Outputs 1 if number is a repdigit, nothing otherwise.

Try it online!

## Explanation

(for input 45. 52 and 53 are charcodes of digits)

rrEE          # Create label 0, implicitly push 0 to the second stack [] [0]
reeeEEeeeE    # Print "1"
rEe           # Return

REEe          # Start: Take input as charcodes to the first stack [52,52,53] [0]
REE           # Create label I (1)
REEEEEEE      # Move first item in first stack to the other [52,53] [0, 52]
rEEEE         # Reset second stack pointer position [>52, 53] [>0, 52]
reE           # If item in first stack equals the first item in the second stack (0),
#   call label 0. It will only be true if first stack is empty
#   because it will implicitly give 0.
rEEEe           # Move second stack pointer to the end [>52, 52] [0, >52]
ReE           # Repeat if items are the same (52 == 52), end program otherwise
reeeEEeeee    # Optional: Print 0.


# Pepe, 69 60 bytes

REeErEEEEEREEEeeEREEEEeEeEREEEEEEeREEEeREEEEEerrEEreEErEereE


Try it online!

How it works?

It takes the greatest digit and subtracts it from the least digit. Repdigit numbers always evaluate to 0.

REeE             # Takes a number (stack r)
rEEEEEREEEeeE    # Splits by digits
REEEEeEeE        # Sorts them
REEEEEEe         # Copies first digit to other stack
REEEe            # Move pointer to last in stack r
REEEEEe          # Subtract stack R to stack r
rrEErEEEEErEereE # Print if 0, else none


# Excel, 28 27 bytes

=A1=MID(A1,2,A1)&LEFT(A1,1)


# Cheddar, 16 bytes

s->s.len*s[0]==s


Try it online!

## Integer input, 29 bytes

n->(s->s.len*s[0]==s)("%d"%n)


Try it online!

# 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. ## Pyke, 3 bytes }t!  Try it here! } - uniquify(input) t - ^[:-1] ! - not ^  # PHP, 34 bytes <?=preg_match('#^(.)\1*$#',$argn);  Try it online! # Excel, 31 bytes =MOD(A2,10)*(10^LEN(A2)-1)/9=A2  # 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.

# AWK, 33 bytes

BEGIN{FS=""}{$0=NF==gsub($1,"")}1


Try it online!

Replaces all characters in the input with the first character and compares the changed count to the total number of characters.