12
\$\begingroup\$

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

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

18 Answers 18

8
\$\begingroup\$

Haskell, 57 bytes

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

Try it online!

\$\endgroup\$
6
\$\begingroup\$

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...
1        - ...start with n equal to: 1
      Ɗ  - ...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]
         -                                            --so we:   discard   discard  keep
         - implicitly print the list of collected values of n
\$\endgroup\$
6
\$\begingroup\$

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)
\$\endgroup\$
1
  • \$\begingroup\$ Right tool for the job. \$\endgroup\$
    – lirtosiast
    Commented Mar 2, 2019 at 1:53
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – xnor
    Commented Mar 2, 2019 at 5:01
4
\$\begingroup\$

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}"

1+⍣{...}   ⊢0  Start with 0 and keep adding 1s until the dfn 
               (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.
\$\endgroup\$
3
  • \$\begingroup\$ 10⊥⍣¯1⊢⍺ -> ⍎¨⍕⍺ \$\endgroup\$
    – ngn
    Commented Mar 6, 2019 at 10:52
  • \$\begingroup\$ it's much shorter with instead of recursion: {1+⍣{∧/1=|2-/⍎¨⍕⍺}⍣⍵⊢0} or 1+⍣{∧/1=|2-/⍎¨⍕⍺}⍣⎕⊢0 \$\endgroup\$
    – ngn
    Commented Mar 6, 2019 at 11:05
  • \$\begingroup\$ "⍣ 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) \$\endgroup\$
    – ngn
    Commented Mar 6, 2019 at 18:53
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ Haha, maybe go learn a new golfing language to cure your withdrawal. :P \$\endgroup\$
    – Quintec
    Commented Mar 1, 2019 at 20:09
3
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ @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). \$\endgroup\$
    – Chas Brown
    Commented Mar 2, 2019 at 2:57
3
\$\begingroup\$

Haskell, 69 bytes

  • 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!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ This appears to answer the question "is this a jumping number?" for a given input number, which isn't what the challenge asked for. \$\endgroup\$ Commented Mar 1, 2019 at 18:57
  • \$\begingroup\$ @JosephSible You are correct. Thank you for noting. \$\endgroup\$ Commented Mar 1, 2019 at 23:34
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Mar 2, 2019 at 4:02
  • \$\begingroup\$ @JosephSible Thank you. \$\endgroup\$ Commented Mar 2, 2019 at 4:19
  • \$\begingroup\$ @nimi Done. Thank you. \$\endgroup\$ Commented Mar 2, 2019 at 7:57
2
\$\begingroup\$

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!

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

Jelly, 9 bytes

-1 by Jonathan Allan

1DạƝ=1ẠƲ#

Try it online!

1-indexed.

\$\endgroup\$
1
  • \$\begingroup\$ 9 bytes by reading STDIN \$\endgroup\$ Commented Mar 1, 2019 at 18:15
1
\$\begingroup\$

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!

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

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.

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

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.

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

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

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

Wolfram Language (Mathematica), 85 bytes

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

Try it online!

returns the n-th number

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

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

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

Catholicon, 5 bytes

ρHṘḃǰ

Try it online!

\$\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.