New Order # 5: Fibonacci และ Beatty พบกันที่ Wythoff


16

บทนำ (อาจถูกละเว้น)

การใส่ตัวเลขบวกทั้งหมดในลำดับปกติ (1, 2, 3, ... ) นั้นน่าเบื่อนิดหน่อยใช่ไหม? ดังนั้นนี่คือชุดของความท้าทายรอบพีชคณิต (reshuffelings) ของจำนวนบวกทั้งหมด นี่คือความท้าทายที่ห้าในชุดนี้ (เชื่อมโยงไปครั้งแรก , สอง , สามและสี่ความท้าทาย)

ในการท้าทายนี้เราจะได้พบกับอาร์เรย์ Wythoff ซึ่งเป็นหิมะถล่มที่ผสมผสานระหว่างFibonacci และ Beatty sequences!

ตัวเลข Fibonacciอาจจะเป็นส่วนใหญ่ของคุณลำดับที่รู้จักกันดี ได้รับตัวเลขสองเริ่มต้นF0และF1ต่อไปนี้Fnจะได้รับโดย: Fn=F(n1)+F(n2)สำหรับn>2 2

ลำดับเบ็ตตี้ได้รับพารามิเตอร์rคือBnr=rnสำหรับn1 1 หนึ่งในคุณสมบัติของลำดับเบ็ตตี้คือสำหรับพารามิเตอร์rทุกตัวมีพารามิเตอร์หนึ่งตัวคือs=r/(r1)เช่นนั้นลำดับเบ็ตตี้สำหรับพารามิเตอร์เหล่านั้นจะแยกกันและรวมเข้าด้วยกัน 0 (เช่น: BrBr/(r1)=N{0} )

ตอนนี้ที่นี่มาพร้อมส่วนจิตใจที่ลื่นไหล: คุณสามารถสร้างอาร์เรย์ที่แต่ละแถวเป็นลำดับฟีโบนักชีและแต่ละคอลัมน์เป็นลำดับเบ็ตตี้ อาร์เรย์นี้เป็นอาร์เรย์ Wythoff ส่วนที่ดีที่สุดคือ: ทุกจำนวนบวกจะปรากฏขึ้นหนึ่งครั้งในอาเรย์นี้! อาร์เรย์มีลักษณะดังนี้:

   1    2    3    5    8   13   21   34   55   89  144 ...
   4    7   11   18   29   47   76  123  199  322  521 ...
   6   10   16   26   42   68  110  178  288  466  754 ...
   9   15   24   39   63  102  165  267  432  699 1131 ...
  12   20   32   52   84  136  220  356  576  932 1508 ...
  14   23   37   60   97  157  254  411  665 1076 1741 ...
  17   28   45   73  118  191  309  500  809 1309 2118 ...
  19   31   50   81  131  212  343  555  898 1453 2351 ...
  22   36   58   94  152  246  398  644 1042 1686 2728 ...
  25   41   66  107  173  280  453  733 1186 1919 3105 ...
  27   44   71  115  186  301  487  788 1275 2063 3338 ...
  ...

องค์ประกอบที่แถวmและคอลัมน์nถูกกำหนดเป็น:

Am,n={mφφ if n=1mφφ2 if n=2Am,n2+Am,n1 if n>2

โดยที่φคืออัตราส่วนทองคำ: φ=1+52 .

ถ้าเราทำตามการต่อต้านแนวทแยงมุมของอาเรย์นี้เราจะได้A035513ซึ่งเป็นลำดับเป้าหมายสำหรับความท้าทายนี้ (โปรดสังเกตว่าลำดับนี้จะถูกเพิ่มเข้าไปใน OEIS โดยNeil Sloaneด้วยตัวเอง!) ตั้งแต่นี้เป็น "ลำดับบริสุทธิ์" ความท้าทายของงานคือการส่งออก( n )สำหรับให้nเป็น input ที่( n )เป็นA035513a(n)na(n)

มีกลยุทธ์ที่แตกต่างกันคุณสามารถปฏิบัติตามเพื่อไปa(n)ซึ่งทำให้ความท้าทายนี้ (ในความคิดของฉัน) น่าสนใจจริงๆ

งาน

ได้รับการป้อนข้อมูลจำนวนเต็มnผลผลิต( n )ในรูปแบบจำนวนเต็มที่( n )เป็นA035513a(n)a(n) A035513

หมายเหตุ: การจัดทำดัชนีแบบ 1 จะถือว่าที่นี่; คุณอาจใช้การจัดทำดัชนีa(0)=1;a(1)=2ดังนั้นa ( 0 ) = 1 ; a ( 1 ) = 2ฯลฯ โปรดพูดถึงสิ่งนี้ในคำตอบของคุณหากคุณเลือกที่จะใช้

กรณีทดสอบ

Input | Output
---------------
1     |  1
5     |  7
20    |  20
50    |  136
78    |  30
123   |  3194
1234  |  8212236486
3000  |  814
9999  |  108240
29890 |  637

มันอาจจะเป็นความสนุกสนานที่จะรู้ว่าที่ใหญ่ที่สุด( n )สำหรับ1 n 32767เป็น( 32,642 ) = 512653048485188394162163283930413917147479973138989971 = F ( 256 ) 2 ไว+ F ( 255 )a(n)1n32767a(32642)=512653048485188394162163283930413917147479973138989971=F(256)2φ+F(255).

กฎระเบียบ

  • อินพุตและเอาต์พุตเป็นจำนวนเต็ม
  • a(n)ไปถึง 30 หมายเลขหลักในช่วงนี้ ...
  • อินพุตไม่ถูกต้อง (0, ลอย, สตริง, ค่าลบ, ฯลฯ ) อาจนำไปสู่ผลลัพธ์ที่ไม่ได้คาดการณ์ไว้, ข้อผิดพลาดหรือ (un) พฤติกรรมที่กำหนดไว้
  • ใช้กฎ I / Oเริ่มต้น
  • ช่องโหว่เริ่มต้นเป็นสิ่งต้องห้าม
  • นี่คือดังนั้นคำตอบที่สั้นที่สุดในหน่วยไบต์ชนะ

2
ดังนั้นการอ้างอิงคำสั่งซื้อใหม่ที่นี่คืออะไร
Luis Mendo

2
@ LuisMendo: การพังทลายของลำดับฟีโบนักชีและเบ็ตตี้ซึ่งประกอบไปด้วยอาร์เรย์ของ Wythoff ...
ตลอด 24

อ่าฉันพลาดไปหมดแล้ว! ตอนนี้ฉันรู้สึกเสียใจ ...
Luis Mendo

1
การเป็นตัวแทนจุดลอยตัวของ phi (หรือ rt (5)) และการประยุกต์ใช้การเกิดซ้ำจะเป็นไปตามข้อกำหนดของช่วงหรือไม่
Jonathan Allan

1
โปรดแก้ไขกรณีทดสอบที่ 9: 999ไม่ใช่9999
J42161217

คำตอบ:


4

เยลลี่ , 27 24 ไบต์

p`SÞ⁸ịð’;רpḞ¥×⁹r‘ÆḞ¤Sð/

ลองออนไลน์!

ลิงค์ Monadic ใช้การจัดทำดัชนีแบบ 1 ฐาน ขอบคุณ @JonathanAllan สำหรับวิธีที่ดีกว่าในการรับแถวและคอลัมน์จากnและบันทึก 3 ไบต์ ในรูปแบบที่สั้นที่สุดมันช้าเกินไปสำหรับ n ที่ใหญ่กว่าบน TIO ดังนั้นลองใช้งานออนไลน์ต่อไปนี้! ลดขนาดของรายการแถวและคอลัมน์เริ่มต้นด้วยราคาสามไบต์

คำอธิบาย

p`                       | Cartesian product of the range from 1..input with itself   
  SÞ                     | Sort by sum
    ⁸ị                   | Find the tuple at the position indicated by the input - this is the row and column
      ð               ð/ | Start a new dyadic chain using the row as the left and column as the right argument
       ’                 | Increase the row by 1
        ;    ¥           | Concatenate to:
         רp             |   row × φ
            Ḟ            |   rounded down
              ×     ¤    | Multiply this pair by
                  ÆḞ     |   the Fibonacci numbers at positions
               ⁹         |   column index and
                r‘       |   column index plus one
                     S   | sum

หมายเหตุนี่เป็นไปตามคำอธิบายของรหัส Python ในหน้า OEIS


1
...×⁹r‘ÆḞ¤Sð/ประหยัดหนึ่งในรุ่นการควบรวมกิจการของคุณ ( TIO )
Jonathan Allan

6

R , 143 130 124 123 ไบต์

function(n){k=0:n+1
`~`=rbind
m=k-1~(k*(1+5^.5)/2)%/%1
for(i in k)m=m~m[i,]+m[i+1,]
m=m[-1:-2,]
m[order(row(m)+col(m))][n]}

ลองออนไลน์!

ใช้สูตร T(n,-1)=n-1;T(n,0)=nφ;T(n,k)=T(n,k-1)+T(n,k-2)เพื่อสร้างอาเรย์ (transposed) จากนั้นsplitsอาเรย์ตาม antidiagonals kเพียงมีอยู่เพื่อป้องกันไม่ให้บังคับให้drop=Fโต้แย้งในกรณีm[-1:-2,]n=1

ขอบคุณNeil ที่ชี้ให้เห็นถึง 1 ไบต์กอล์ฟ

R , 150 138 132 ไบต์

function(n){T[2]=1
for(j in 2:n-1)T=c(T,T[j]+T[j+1])
m=T[-1]%o%((1:n*(.5+5^.5/2))%/%1)+T[-1-n]%o%(1:n-1)
m[order(row(m)+col(m))][n]}

ลองออนไลน์!

ใช้สูตร T(n,k)=Fib(k+1)nϕ+Fib(k)(n1) to get generate the array, then splits along the antidiagonals and extracts the nth element.

Thanks to Robin Ryder for the T[2]=1 trick for generating the Fibonacci sequence.


Both solutions are highly inefficient, creating an nxn matrix of (most likely) doubles, as R promotes integer (32-bit signed) to double automatically when overflowing, but the second one should be quite a lot faster. Taking n as a bignum should work automatically, using the call gmp::as.bigz(n), should loss of precision under doubles be worrisome, and then the language would be R + gmp.


Can you use (1+5^.5)/2 instead of (.5+5^.5/2)?
Neil

@Neil ...yes, I can. Thank you! Only going to edit it into the top one unless I can find a way to golf down the second one quite a lot more.
Giuseppe


2

Jelly, 30 bytes

p`SÞ⁸ịð;Øp,²;\¤×Ḟ¥/;+ƝQƊ⁹¡ị@ð/

Try it online!
This is a little slow, but a huge improvement is made with a prefix of Ḥ½Ċ (double, square-root, ceiling) like in this test-suite.


2
you are right! 740496902 is the result for 999
J42161217

Combining the first part of yours and second part of mine gives 25 bytes. Not sure which of us should have the combined version!
Nick Kennedy

@NickKennedy - nice, go for it!
Jonathan Allan

2

Charcoal, 54 bytes

Nθ≔⁰ηW‹ηθ«≦⊕η≧⁻ηθ»⊞υ¹Fθ⊞υ⁻⁺³ι§υ⊖§υι⊞υθF⁺²⁻θη⊞υΣ…⮌υ²I⊟υ

Try it online! Link is to verbose version of code. 0-indexed. Uses only integer arithmetic so works for arbitrary large values. Explanation:

Nθ

Input q.

≔⁰ηW‹ηθ«≦⊕η≧⁻ηθ»

Calculate the antidiagonal by subtracting ever increasing numbers from q, which ends up with the target row number m.

⊞υ¹Fθ⊞υ⁻⁺³ι§υ⊖§υι

Calculate the first m+1 terms of A019446, although we're only interested in the mth.

⊞υθF⁺²⁻θη⊞υΣ…⮌υ²

Calculate the first n+4 terms of the generalised Fibonacci series that starts with [a(m), m]. The terms of this sequence are the mth terms of A019446, A001477, A000201, A003622, A035336; these last two are the first two columns of the Wythoff array, and so this sequence continues with the rest of the mth row of the array.

I⊟υ

Output the desired term.

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.