# 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
Commented 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. Commented 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
Commented Jul 1, 2017 at 20:23
• A different, shorter approach. Well done! +1 Commented Jul 2, 2017 at 3:47
• n==0n<1 saves a byte.
– lynn
Commented 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. Commented 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) Commented 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) Commented 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... Commented Jul 1, 2017 at 15:29 • ...and it can: ¥£%´JÃw :-) (works because x%y == x%-y in JS) Commented Jul 1, 2017 at 15:31 • Aha, yes, was trying a few different things to get that calculation down to 2 bytes. Commented 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 Commented 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.
• «%« is the modulus hyperoperator that gives the remainder when $_, the input argument on its left, is divided by each of the elements of the sequence on its right: $_ % 2, $_ % 3, .... • [~] concatenates those digits into a new number, which is compared with the input argument using the string equality operator eq. # PHP, 43 bytes for(;$r<$a=$argn;)$r=$a%~++$i.$r;echo$r>$a;


Try it online!

# PHP, 44 bytes

prints 1 for true and nothing for false

for(;$r<$a=$argn;)$r=$a%~++$i.$r;echo$r==$a;  Try it online! # Pyth, 13 bytes -1 bytes thanks to Okx. qsjk_m%QhdSl  Try it online! Explanation  QQ # Implicit input SlQ # Generate [1, len(str(Q))] m%Qhd # For digit d in above range, perform Q % (d + 1) sjk_ # Reverse, then convert to number q Q # Test equality with input  Alternate solution, still 13 bytes (thanks to karlkastor) qi_.e%Q+2kQT  Try it online! That's essentially the same as the first solution, excepted that it uses i to convert from array of numbers to a number, and that the range is generated differently. • You can replace ssM_ with jk_ to save 2 bytes. – Okx Commented Jul 1, 2017 at 17:13 • @Okx I need it because j outputs a string whereas I need a number to compare with the input (which is a number). – Jim Commented Jul 1, 2017 at 17:59 • Another 13 byte solution would be: qi_.e%Q+2kQT using enumerated map (.e) instead of map. And converting the remainders to a base 10 int from the list instead of using join. Commented Jul 1, 2017 at 18:13 # C++,104 bytes 1) original version: int main(){int N,T,R=1;cin>>N;T=N;for(int i=1;i<=log10(N)+1;i++){if(N%(i+1)!=T%10){R=0;}T/=10;}cout<<R;}  2) in a readable form: int main() { int N, T, R = 1; cin >> N; T = N; for (int i = 1; i <= log10(N) + 1; i++) { if (N % (i + 1) != T % 10) { R = 0; } T /= 10; } cout << R; }  Try it Online! # APL (Dyalog Extended), 21 19 bytes {⍵=10⊥⌽⍵|⍨1↓⍳1+≢⍕⍵}  Try it online! Creates two lists of digits and check if they are equal. ## Explanation {⍵=10⊥⌽⍵|⍨1↓⍳1+≢⍕⍵} ⍵ → input ≢⍕⍵ length of ⍵ as a string 1+ +1 ⍳ range from 1 to that 1↓ Drop first element ⍵|⍨ ⍵ mod each element in range ⌽ reversed 10⊥ converted from base 10 ⍵= equals the input?  # Pari/GP, 42 bytes n->Vecrev(s=digits(n))==[n%d|d<-[2..#s+1]]  Try it online! # Python 2, 66 60 58 57 bytes • Thanks to @Leaky nun for 6 bytes: remove unneeded x and (shouldnot check for 0) • Thanks to @Einkorn Enchanter for 1 byte: use of enumerate lambda x:all(a==x%(i+2)for i,a in enumerate(x[::-1]))  Try it online! • Shorter if you enumerate Commented Jul 1, 2017 at 15:09 • Thank you. And hey, I just realised you were wheat wizard.. :D Commented Jul 1, 2017 at 15:42 • This is a very late comment, but enumerate can take a second argument "number to start counting from", so x%i and enumerate(…,2) saves two bytes. – lynn Commented Nov 5, 2020 at 15:06 # Python 3: 63 Bytes lambda m:all(int(x)==m%(n+2)for n,x in enumerate(str(m)[::-1]))  If I could count the number of times I wished 'enumerate' were shorter... Try it online! • Yep, and I just realized its exactly the same as the answer @officialaimm gave... Should I remove? Commented Jul 5, 2017 at 14:41 • Theirs is in python 2 and you came up with it independently so I would leave it. Commented Jul 5, 2017 at 14:44 • Can save two bytes by starting your enumeration at 2 and rearranging the logical: lambda m:all(m%n==int(x)for n,x in enumerate(str(m)[::-1],2)) Commented Jul 5, 2017 at 15:48 # Java (OpenJDK 8), 60 bytes n->{int m=2,r=0,t=1;for(;n>=t;t*=10)r+=n%m++*t;return r==n;}  Try it online! A non-string version. ## Java 8, 156149 bytes interface B{static void main(String[]s){String f="";for(int i=1;i<=s[0].length();)f=new Long(s[0])%++i+f;System.out.print(f.equals(s[0]));}}  Ungolfed : interface B { static void main(String[] s) { String f = ""; for (int i = 1; i <= s[0].length();) f = new Long(s[0]) % ++i + f; System.out.print(f.equals(s[0])); } }  Try it Online ! UPDATE : -7 bytes : removed useless {} and replaced Integer.parseInt(...) by new Integer(...) -9 bytes : thanks to Kevin Cruijssen, removed a bunch of useless (), used Long instead of Integer and print instead of println. Thanks Kévin ! • Nice answer, +1 from me. Btw, some small things to golf: new Integer can be new Long (-3 bytes); println can be print (-2 bytes); and you can remove the parenthesis surrounding new Long(s[0])%i+f; (-4 bytes). Commented Jul 5, 2017 at 12:29 • Really nice ! Thanks, i'll update this ! Commented Jul 6, 2017 at 12:45 # Charcoal, 20 15 bytes ⌊Ｅθ⁼ιＩ﹪Ｉθ⁻⁺¹Ｌθκ  Try it online! Outputs - for a staircase number, nothing otherwise. Link is to verbose version of code. # Haskell, 53 bytes f n=foldr(\(k,_)a->10*a+mod n k)0(zip[2..]$show n)==n


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


# Arturo, 42 bytes

\$[n][1every? reverse digits n'x->x=n%<=1+]
`

Try it