ジャンプ番号


12

ジャンプ番号は、連続する10進数のすべてのペアが1異なる正の数nとして定義されます。また、すべての1桁の番号はジャンプ番号と見なされます。例えば。3、45676、212はジャンプ数ですが、414と13はそうではありません。9と0の差は1とは見なされません

課題 次のいずれかの結果を出力するプログラムを作成します。

  • 入力が与えられたn場合、最初のnジャンプ番号。
  • 入力が与えられnた出力をnシーケンスの用語。

注意

  • 有効なI / O形式はすべて許可されます
  • 1インデックスまたは0インデックスが許可されます(指定してください)

ここにいくつかのジャンプ番号があります:

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

これもA033075です


これは0または1にインデックス付けされていますか?
テイラースコット

1
@TaylorScottシーケンスは正数のみで構成されます。入力を意味する場合n、それはあなた次第です。
ルイスフェリペデイエスムニョス

「任意の有効なI / O形式が許可されている」とは、数字を10進数のリストとして出力することを含むと推測していますが、確認したいだけです。
ジョナサンアラン

回答:



6

ゼリー、8バイト

1DI*`ƑƊ#

n最初のn正のジャンプ番号のリストを出力する、STDINからの整数を受け入れる完全なプログラム。

オンラインでお試しください!

どうやって?

数字の間で許容される増分の違いは1あり-1、他のもの[-9,-2]+[2,9]は違います。これは、それ自体に上げられたときに不変の整数と並んでいます。すなわち、xx=x以降:

00=1
11=1
22=4
11=1
22=14

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


5

パイソン279の 75バイト

xnorによる-4バイト

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

オンラインでお試しください!

Chas Brown答えに由来します。ヘルパー関数は、ジャンプ番号g(i)かどうかを返しますi。数字の最後の2桁にn絶対差1 n%100%11がある場合、1または10になるため、n%100%11%91になります。


で素敵なトリック%11。あなたは行うことができf=lambda n,i=1:n and-~f(n-g(i),i+1)ますが、1-インデックスに切り替えた場合。
xnor

4

APL(Dyalog Unicode)、36 バイトSBCS

1インデックス付き。ゴルフを手伝ってくれたdzaimaに感謝します。

編集:NGNから-15バイト。

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

オンラインでお試しください!

説明

我々は持っているf⍣g⍣h通りであり、オペレータは、APLはこれを翻訳します(f⍣g)⍣h。(2×3+1変換される関数とは対照的に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.

10⊥⍣¯1⊢⍺ -> ⍎¨⍕⍺
ngn

it's much shorter with instead of recursion: {1+⍣{∧/1=|2-/⍎¨⍕⍺}⍣⍵⊢0} or 1+⍣{∧/1=|2-/⍎¨⍕⍺}⍣⎕⊢0
ngn

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



3

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

3

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!


1
This appears to answer the question "is this a jumping number?" for a given input number, which isn't what the challenge asked for.
Joseph Sible-Reinstate Monica

@JosephSible You are correct. Thank you for noting.
Jonathan Frech

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
Joseph Sible-Reinstate Monica

@JosephSible Thank you.
Jonathan Frech

@nimi Done. Thank you.
Jonathan Frech

2

JavaScript (ES7), 60 bytes

Returns the nth 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!



1

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!


1

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.


1

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.




1

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


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.