# Jumping Numbers

A jumping number is defined as a positive number n which all pairs of consecutive decimal digits differ by 1. Also, all single digit numbers are considered jumping numbers. eg. 3, 45676, 212 are jumping numbers but 414 and 13 are not. The difference between 9 and 0 is not considered as 1

The challenge Create a program that output one of the following results:

• Given an input n output the first n jumping numbers.
• Given an input n output the nth term of the sequence.

Note

• Any valid I/O format is allowed
• 1-index or 0-index is allowed (please specify)

Here are some jumping numbers:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 78, 87, 89, 98, 101, 121, 123, 210, 212, 232, 234, 321, 323, 343, 345, 432, 434, 454, 456, 543, 545, 565, 567, 654, 656, 676, 678, 765, 767, 787, 789, 876, ...


This is also A033075

• Is this 0 or 1 Indexed? Mar 1, 2019 at 17:19
• @TaylorScott The sequence consists in only positive numbers. If you mean the input n then it is up to you. Mar 1, 2019 at 17:20
• I'm guessing "Any valid I/O format is allowed" includes outputting the numbers as lists of decimal digits, but just wanted to confirm - ? Mar 1, 2019 at 18:04
• Yes @JonathanAllan Mar 1, 2019 at 18:27

(l!!)
l=[1..9]++[x*10+t|x<-l,t<-[0..9],(mod x 10-t)^2==1]


Try it online!

# Jelly, 8 bytes

1DI*ƑƊ#


A full program accepting an integer, n, from STDIN which prints a list of the first n positive jumping numbers.

Try it online!

### How?

Acceptable incremental differences between digits are 1 and -1 while others from [-9,-2]+[2,9] are not. This lines up with integers which are invariant when raised to themselves. i.e. $$\x^x=x\$$ since:

$$0^0=1$$ $$1^1=1$$ $$2^2=4$$ $$\cdots$$ $$-1^{-1}=-1$$ $$-2^{-2}=-\frac{1}{4}$$ $$\cdots$$

1DI*ƑƊ# - Main Link: no arguments (accepts a line of input from STDIN)
# - count up keeping the first (input) n matches...
Ɗ  - ...match function: last three links as a monad:  e.g. 245       777      7656
D       -   convert to a list of decimal digits                 [2,4,5]   [7,7,7]  [7,6,5,6]
I      -   incremental differences                             [2,1]     [0,0]    [-1,-1,1]
Ƒ   -   invariant under?:
    -     using left argument as both inputs of:
*     -       exponentiation (vectorises)                     [4,1]     [1,1]    [-1,-1,1]
- implicitly print the list of collected values of n


# 05AB1E (legacy), 5 bytes

The input is 1-indexed.

### Code:

µN¥ÄP


Uses the 05AB1E encoding. Try it online!

### Explanation

µ          # Get the nth number, starting from 0, such that...
Ä       #   The absolute values
N¥        #   Of the delta's of N
P      #   Are all 1 (product function, basically acts as a reduce by AND)

• Right tool for the job. Mar 2, 2019 at 1:53

# Python 2, 79 75 bytes

-4 bytes by xnor

f=lambda n,i=1:n and-~f(n-g(i),i+1)
g=lambda i:i<10or i%100%11%9==g(i/10)>0


Try it online!

Derived from Chas Brown's answer. The helper function g(i) returns whether i is a jumping number. Iff the last two digits of a number n have absolute difference 1, then n%100%11 will be either 1 or 10, so n%100%11%9 will be 1.

• Nice trick with the %11. You can do f=lambda n,i=1:n and-~f(n-g(i),i+1) if you switch to one-indexing.
– xnor
Mar 2, 2019 at 5:01

# APL (Dyalog Unicode), 36 bytesSBCS

1-indexed. Thanks to dzaima for their help with golfing this.

Edit: -15 bytes from ngn.

1+⍣{∧/1=|2-/⍎¨⍕⍺}⍣⎕⊢0


Try it online!

Explanation

We have f⍣g⍣h, where, as ⍣ is an operator, APL translates this to (f⍣g)⍣h. (In contrast with functions where 2×3+1 is translated 2×(3+1))

1+⍣{...}⍣⎕⊢0  This is equivalent to
"do {check} we find the n-th integer that fulfils {check}"

(our jumping number check in {}) returns true.
⍣⎕    We take input n (⎕) and repeat (⍣) the above n times
to get the n-th jumping number.

{∧/1=|2-/⍎¨⍕⍺}  The dfn that checks for jumping numbers.

⍎¨⍕⍺   We take the base-10 digits of our left argument
by evaluating each character of the string representation of ⍺.
|2-/        Then we take the absolute value of the pairwise differences of the digits
∧/1=            and check if all of the differences are equal to 1.

• 10⊥⍣¯1⊢⍺ -> ⍎¨⍕⍺
– ngn
Mar 6, 2019 at 10:52
• it's much shorter with ⍣ instead of recursion: {1+⍣{∧/1=|2-/⍎¨⍕⍺}⍣⍵⊢0} or 1+⍣{∧/1=|2-/⍎¨⍕⍺}⍣⎕⊢0
– ngn
Mar 6, 2019 at 11:05
• "⍣ is an operand" - it's an "operator" (i this mistake in chat and corrected it, but it seems you picked up the initial version. sorry)
– ngn
Mar 6, 2019 at 18:53

# C (gcc), 90 bytes

f(n,K,b,k){for(K=0;n;b&&printf("%d,",K,n--))for(b=k=++K;k/10;)b*=abs(k%10-(k/=10)%10)==1;}


Try it online!

# Japt, 14 bytes

Outputs the first nth term, 1-indexed.

_ì äa dÉ ªU´}f


Try it

(I know, I know, I'm supposed to be taking a break but I'm in golf withdrawal!)

• Haha, maybe go learn a new golfing language to cure your withdrawal. :P Mar 1, 2019 at 20:09

# Python 2, 88 87 bytes

f=lambda n,i=2:n and f(n-g(i),i+1)or~-i
g=lambda i:i<10or abs(i/10%10-i%10)==1==g(i/10)


Try it online!

Returns the 0-indexed jumping number (i.e., f(0) => 1, etc).

• @lirtosiast : That's OK, please donate your answer to your favorite charity :). It's sufficiently different to merit a separate response (as well as being cross-language appropriate). Mar 2, 2019 at 2:57

• Thanks to Joseph Sible for enforcing the challenge rules and saving three bytes.
• Saved two bytes thanks to nimi.
(filter(all((==1).abs).(zipWith(-)<*>tail).map fromEnum.show)[1..]!!)


Try it online!

• This appears to answer the question "is this a jumping number?" for a given input number, which isn't what the challenge asked for. Mar 1, 2019 at 18:57
• @JosephSible You are correct. Thank you for noting. Mar 1, 2019 at 23:34
• Also, now that that's fixed, you can make g 3 bytes shorter by rewriting it to be pointfree, then using <*>: g=all((==1).abs).(zipWith(-)<*>tail).map(read.pure).show Mar 2, 2019 at 4:02
• @JosephSible Thank you. Mar 2, 2019 at 4:19
• @nimi Done. Thank you. Mar 2, 2019 at 7:57

# JavaScript (ES7), 60 bytes

Returns the $$\n\$$th term of the sequence (1-indexed).

f=(n,k)=>[...k+''].some(p=x=>(p-(p=x))**2-1)||n--?f(n,-~k):k


Try it online!

# Jelly, 9 bytes

-1 by Jonathan Allan

1DạƝ=1ẠƲ#


Try it online!

1-indexed.

• 9 bytes by reading STDIN Mar 1, 2019 at 18:15

# Swift, 228 bytes

func j(n:Int){
var r:[Int]=[]
for x in 0...n{
if x<=10{r.append(x)}else{
let t=String(x).compactMap{Int(String($0))} var b=true for i in 1...t.count-1{if abs(t[i-1]-t[i]) != 1{b=false}} if b{r.append(x)} } } print(r) } j(n:1000)  Try it online! # Python 3, 122 121 bytes g=lambda s:len(s)==1or 1==abs(ord(s[0])-ord(s[1]))and g(s[1:]) def f(n,i=1): while n: if g(str(i)):n-=1;yield i i+=1  Try it online! -1 byte by changing f from printing, to a generator function. g is a recursive helper function which determines if a string s is a "jumping string" (this works since the character codes for 0 to 9 are in order and contiguous). f is a generator function that takes in n and yields the first n jumping numbers. # R, 85 bytes i=scan();j=0;while(i)if((j=j+1)<10|all(abs(diff(j%/%10^(0:log10(j))%%10))==1))i=i-1;j  Try it online! Suspect this can be golfed more. Reads the number using scan() and outputs the appropriate jumping number. # Perl 5, 56 bytes map{1while++$n=~s|.(?=(.))|abs$&-$1!=1|ger>9}1..<>;say\$n


Try it online!

1-indexed, outputs the nth jumping number

# Wolfram Language (Mathematica), 85 bytes

If[#<10,#,t=n=1;While[t<=#,{-1,1}~SubsetQ~Differences@IntegerDigits@n++~If~t++];n-1]&


returns the n-th number

# Factor, 129 bytes

: f ( x -- ) 1 [ [ dup 10 >base >array differences [ abs 1 = ] all? ] [ 1 + ] until
dup . 1 + [ 1 - ] dip over 0 > ] loop 2drop ;


Try it online!

Outputs the first n jumping numbers

# Catholicon, 5 bytes

ρHṘḃǰ
`

Try it online!