# Staircase numbers

A staircase number is a positive integer x such that its nth digit (one indexed starting with the least significant digit) is equal to x % (n + 1). Thats a bit of a mouthful so lets look at an example. Take 7211311, if we take the modular residues of 7211311 on the range 2-8 we get the following:

7211311 % 2 = 1
7211311 % 3 = 1
7211311 % 4 = 3
7211311 % 5 = 1
7211311 % 6 = 1
7211311 % 7 = 2
7211311 % 8 = 7

These are the digits of 7211311! Thus 7211311 is a staircase number.

Write code that takes when given a positive number as input, will output two distinct values one if the number is a staircase number and the other if it is not.

This is a competition so your aim should be to minimize the number of bytes in your source code.

## Test Cases

Here are the first 13 staircase numbers:

1, 10, 20, 1101, 1121, 11311, 31101, 40210, 340210, 4620020, 5431101, 7211311, 12040210
• Isn't 0 a staircase numbers? A lot of answers think so.
– Okx
Jul 2, 2017 at 10:40
• @Okx the task is just to distinguish positive staircase numbers from positive non-staircase numbers, so the behavior is undefined for 0 and negative numbers. Jul 2, 2017 at 12:40

f m|let n#x=n==0||nmod10==mmodx&&div n 10#(x+1)=m#2

A different approach than the other Haskell solution.

Thanks xnor for saving 2 bytes.

• You can use this tip to shorten the let expression.
– xnor
Jul 1, 2017 at 20:23
• A different, shorter approach. Well done! +1 Jul 2, 2017 at 3:47
• n==0n<1 saves a byte.
– Lynn
Nov 5, 2020 at 15:10

# Brachylog, 252116 14 bytes

{it+₂;?↔%}ᶠ↔c?

Try it online!

First Brachylog submission :D probably very ungolfed...many thanks to Leaky Nun and Fatalize for encouragement and help to golf this from 25 all the way down to just 14. :) :)

# 05AB1E, 6 bytes

### Code:

ā>%JRQ

Uses the 05AB1E encoding. Try it online!

### Explanation:

ā        # Get the range [1 .. len(input)]
>       # Increment by 1
%      # Vectorized modulo
J     # Join the array into a single number
RQ   # Reverse that number and check if it's equal to the original input

# Javascript, 424139 38 bytes

-4 bytes thanks to @Shaggy and @ETHProductions

s=>[...s].some(d=>s%i++^d,i=~s.length)

This takes the number as a string and returns false if the number is a staircase number and true otherwise.

Example code snippet:

f=
s=>[...s].some(d=>s%i++^d,i=~s.length)

function update() {
o.innerText = f(document.getElementById("i").value)
}
<input id="i" type="number">
<button onclick="update()">Test</button>
<p id="o">

• You should be able to drop the ! as the challenge doesn't explicitly specify that you must return true for true and false for false, simply that you must return 2 distinct values. Jul 1, 2017 at 16:01
• This is very well golfed, well done. I think you should be able to squeeze out two more bytes if you calculate i yourself: s=>[...s].some(d=>s%i--^d,i=s.length+1) Jul 1, 2017 at 16:05
• Actually, by exploiting the fact that ~x == -(x+1) on integers and x%-y == x%y, I think you can get one more: s=>[...s].some(d=>s%i++^d,i=~s.length) Jul 1, 2017 at 16:10

# Python 2, 54 bytes

f=lambda n,x=0:10**x>n or(n%(x+2)==n[~x])*f(n,x+1)

Try it online!

Takes the number as an int

x n|s<-show n=reverse s==(rem n.(+1)<$>[1..length s]>>=show) # Mathematica, 60 bytes FromDigits@Reverse@Mod[#,Range@Length@IntegerDigits@#+1]==#& Try it online! @alephalpha golfed it to 48 # Mathematica, 48 bytes FromDigits@Reverse@Mod[#,Range[2,Log10@#+2]]==#& next one is 24120020 # Japt, 97 6 bytes Takes input as a string. Ô¶¡%´J 2 bytes saved with help from ETHproductions. Try it Ô¶¡%´J :Implicit input of string U Ô :Reverse ¶ :Test for equality with ¡ :Map U % : Modulo ´J : J (initially -1) prefix decremented • Gosh darn (Y+2, I feel like that could be at least 1 byte shorter... Jul 1, 2017 at 15:29 • ...and it can: ¥£%´JÃw :-) (works because x%y == x%-y in JS) Jul 1, 2017 at 15:31 • Aha, yes, was trying a few different things to get that calculation down to 2 bytes. Jul 1, 2017 at 15:47 # Neim, 6 bytes 𝐧ᛖ𝕄𝐫𝐣𝔼 Explanation: 𝐧 Get the length of the input, then create an exclusive range ᛖ Add 2 to each element 𝕄 Modulus 𝐫 Reverse 𝐣 Join 𝔼 Check for equality Try it online! • @Thehx Regarding your edit, Neim uses a custom encoding: this one – Okx Jul 5, 2017 at 15:24 # Jelly, 7 bytes DJ‘⁸%⁼Ṛ Try it online! # Python 2, 56 bytes lambda x:all(x%(i+2)==x[~i]for i in range(len(x))) Try it online! # Perl 6, 32 bytes {$_ eq[~] $_ «%«(1+.comb...2)} Try it online! • .comb is the number of characters in the string representation of the input argument$_ (that is, the number of digits).
• 1 + .comb ... 2 is the sequence of numbers from one greater than the number of digits down to 2.

Try it online!

# PHP, 44 bytes

prints 1 for true and nothing for false

Try it online!

## Python 2, 61 bytes

lambda x:[x%(n+2)for n in range(len(x))][::-1]==list(x`)
• Nope, your new golf is a byte shorter. :) Jul 1, 2017 at 15:15

# q/kdb+, 34 bytes

Solution:

{s~raze($)x mod'2+(|)(!)(#)s:($)x}

Example:

q){s~raze($)x mod'2+(|)(!)(#)s:($)x}7211311 / this is a staircase number (true)
1b
q){s~raze($)x mod'2+(|)(!)(#)s:($)x}7211312 / this is not (false)
0b
q)t(&){s~raze($)x mod'2+(|)(!)(#)s:($)x}each t:1 + til 1000000 / up to a million
1 10 20 1101 1121 11311 31101 40210 340210

Explanation:

Cast the input number to a string, count from 0..length of string, add 2 to all, reverse it and feed each number into mod along with the original input. Cast the result of the mod to a string and reduce the list, check if it is equal to the string of the input number:

{s~raze string x mod'2 + reverse til count s:string x} / ungolfed solution
{                                                    } / lambda function
s:string x  / convert input to string, save as s
count             / return length of this string
til                   / like python's range() function
reverse                       / reverses the list
2 +                               / adds two to each element in the list
x mod'                                  / ' is each both, so feeds x, and each element of the list to modulo function
string                                         / converts output list to string list ("7";"2";"1"..etc)
raze                                                / reduce list ("721...")
s~                                                    / is s equal to this reduced list, returns boolean

Notes:

Most of the solution is for generating the 2,3,4.. list, I have another solution that does less stuff, but winds up being 37 bytes after golfing:

{s~x mod'reverse 2 + til count s:("J"$) each string x} / ungolfed {s~x mod'(|)2+til(#)s:("J"$)each($)x} / golfed ## Clojure, 75 bytes #(=(sort %)(sort(map(fn[i c](char(+(mod(Integer. %)(+ i 2))48)))(range)%))) Input is a string, using map and the trailing % ended up being shorter than for[i(range(count %))] approach. # Haskell, 62 bytes f x=and$zipWith(==)(reverse$show x)$map(head.show.mod x)[2..]

Instead of reversing the (infinite) list of moduli, it truncates the list by zipping it with the reversed string-respresentation of the integral x, which it then ensures is equal element-wise.

# Perl 5, 41 bytes

39 bytes of code + 2 flags -pa

map{$\||=$_!=$F[0]%++$n}0,reverse/./g}{

Try it online!

Outputs nothing (undef) for staircase numbers, 1 for anything else

# MathGolf, 7 bytes

mækî)%Ñ

Input as a string.

Try it online.

Explanation:

m        # Map over each character of the (implicit) input-string,
æ       # using the following four characters as inner code-block:
#  (implicitly push the current character of the map)
k      #  Push the input as an integer
î     #  Push the 1-based map-index
)    #  Increase it by 1
%   #  Take the input-integer modulo (1-based) index+1
# After the map, the digits are implicitly joined together to a string
Ñ  # Check that this string is a palindrome
# (after which the entire stack joined together is output implicitly as result)