38
\$\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, 2017 at 3:02
  • \$\begingroup\$ @AidanF.Pierce What's the biggest number the input will be? \$\endgroup\$
    – stevefestl
    Jun 14, 2017 at 9:00

91 Answers 91

2
\$\begingroup\$

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?

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ 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 \$\endgroup\$
    – Luis Mendo
    May 27, 2019 at 9:47
  • \$\begingroup\$ 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. \$\endgroup\$
    – DrQuarius
    Jul 17, 2019 at 11:37
  • \$\begingroup\$ 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) \$\endgroup\$
    – Luis Mendo
    Jul 17, 2019 at 16:06
  • 1
    \$\begingroup\$ Wow, ok. That's a very simple solution then. Will add. \$\endgroup\$
    – DrQuarius
    Aug 2, 2019 at 4:42
2
\$\begingroup\$

brainfuck, 21 bytes

,<,[[>->+<<-]>[>],]>.

With a little formatting:

,<,
[
  [>->+<<-]
  >[>]
  ,
]
>.

Try it online

Prints the repeated digit for true, and NUL for false.

Compares adjacent digits in a loop, and in the case of inequality, shifts pointer so that all subsequent comparisons will also result in inequality, and the cell to the right will be zero upon exiting the loop.

Variant for stricter output rules, 25 bytes

,<,[[>->+<<-]>[>>->],]<+.

Formatted:

,<,
[
  [>->+<<-]
  >[>>->]
  ,
]
<+.

Try it online

This version prints \x01 for true.

\$\endgroup\$
2
\$\begingroup\$

Excel, 24 bytes (assuming Tab is 1 byte)

=LEFT(B1,1);=A1&B1=B1&A1

Excel, 28 27 bytes

=A1=MID(A1,2,A1)&LEFT(A1,1)
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

p~/^(.)\1*$/

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ I'm no C# developer, but do you really require using System.Linq? I've seen plenty of C# lambda answers without that. \$\endgroup\$ Jun 8, 2017 at 12:37
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Jun 8, 2017 at 12:40
1
\$\begingroup\$

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 %))
\$\endgroup\$
1
\$\begingroup\$

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

><>, 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
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 12 bytes

{[==] .comb}

Reduces the list of characters in the input number with the numeric equality operator.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\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\$
5
  • \$\begingroup\$ Please specify the version of Python. \$\endgroup\$
    – Leaky Nun
    Jun 8, 2017 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, 2017 at 3:04
  • \$\begingroup\$ @Dennis If it is Python 2, I can suggest a golf. \$\endgroup\$
    – Leaky Nun
    Jun 8, 2017 at 3:23
  • \$\begingroup\$ Now that the input specs are more lax, you can replace str(x) with x. \$\endgroup\$
    – Leaky Nun
    Jun 8, 2017 at 3:23
  • \$\begingroup\$ In Python 3.6+ this can be len({*x})<2 \$\endgroup\$
    – pxeger
    Feb 24, 2021 at 14:31
1
\$\begingroup\$

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.

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

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Are the spaces around the < necessary? I haven't used small talk very much, but most languages wont require that sort of thing. \$\endgroup\$
    – Wheat Wizard
    Jun 14, 2017 at 3:51
  • 1
    \$\begingroup\$ 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 |. \$\endgroup\$ Jun 14, 2017 at 3:56
  • 1
    \$\begingroup\$ I stand corrected. I tried removing the spaces before and after the < and it seems to work. \$\endgroup\$ Jun 14, 2017 at 3:58
1
\$\begingroup\$

Check, 135 134 132 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.
\$\endgroup\$
1
\$\begingroup\$

Julia, 30 bytes

f(n)=length(Set(string(n)))==1
\$\endgroup\$
1
\$\begingroup\$

F#, 38 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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$) \$\endgroup\$
    – steenbergh
    Jun 20, 2018 at 13:34
  • \$\begingroup\$ Confirmed on QB4.5: if we input n$ as 123, then STRING$(3, n$) is 111. \$\endgroup\$
    – steenbergh
    Jun 20, 2018 at 16:22
  • 1
    \$\begingroup\$ @steenbergh [facepalm] I just read that in the help file, too. Thanks! \$\endgroup\$
    – DLosc
    Jun 20, 2018 at 20:36
1
\$\begingroup\$

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

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, 602 582 541 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...).

\$\endgroup\$
5
  • \$\begingroup\$ For the second program? Sure, I could exit another way. \$\endgroup\$ Oct 9, 2018 at 0:45
  • \$\begingroup\$ I know, I made the modification. \$\endgroup\$ Oct 9, 2018 at 0:49
  • \$\begingroup\$ 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 \$\endgroup\$
    – Jo King
    Oct 9, 2018 at 0:55
  • \$\begingroup\$ Could it crash in two different ways depending on result? \$\endgroup\$ Oct 9, 2018 at 0:57
  • \$\begingroup\$ 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 \$\endgroup\$
    – Jo King
    Oct 9, 2018 at 1:05
1
\$\begingroup\$

Pascal (FPC), 81 75 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.

\$\endgroup\$
2
  • \$\begingroup\$ I think you can shave off a bit with the mod 100 mod 11 trick some others are using. \$\endgroup\$ Oct 8, 2018 at 20:08
  • \$\begingroup\$ @ØrjanJohansen Thanks, a good one! \$\endgroup\$
    – AlexRacer
    Oct 8, 2018 at 21:19
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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

naz, 50 bytes

2a2x1v1r2x2v1x1f1r3x1v2e3x2v1e0m1o0x1x2f0m1a1o0x1f

Works for any input integer, provided it's passed as a file terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v                 # Set variable 1 equal to 2
1r2x2v                 # Read the first byte of input and store it in variable 2
1x1f                   # Function 1
    1r                 # Read a byte of input
      3x1v2e           # Jump to function 2 if it equals variable 1
            3x2v1e     # Jump back to the start of the function if it equals variable 2
                  0m1o # Otherwise, output 0
1x2f                   # Function 2
    0m1a1o             # Output 1
1f                     # Call function 1
\$\endgroup\$
1
\$\begingroup\$

Pip, 3 bytes

$=a

fold by equality.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Swift (24 / 17 bytes)

Console App (24 bytes):

Set(readLine()!).count<2

Function (17 bytes):

{Set($0).count<2}

Usage example:

let isRepdigit: (String) -> Void = {Set($0).count<2}
print(isRepdigit("234")) // prits false
print(isRepdigit("66666")) // prints true
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 1 byte

Try it Online!

Simply checks if all the digits are equal to each other

\$\endgroup\$
1
\$\begingroup\$

Scratch 3.0, 20 blocks/148 bytes

enter image description here

define(z)
set[o v]to(1
set[i v]to(1
repeat(length of(z)
if <not<(letter(i)of(z))=(letter(1)of(z))>>then
set[o v]to(0
end
set[i v]to((i)+(1
end
say(o

Try it on Scratch

I did the most logical thing and answer this with scratch. Essentially, we loop through every character and see if it equals the first character. If any character doesn't equal the first, the output is set to falsey

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.