# New Order #5: where Fibonacci and Beatty meet at Wythoff

## R, ~~143~~ ~~130~~ ~~124~~ 123 bytes

```
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]}
```

Try it online!

Uses the formula \$T(n,-1)=n-1; T(n,0)=\lfloor n\cdot\phi\rfloor;T(n,k)=T(n,k-1)+T(n,k-2)\$ to construct the array (transposed), then `splits`

the array along antidiagonals. `k`

merely exists to prevent forcing a `drop=F`

argument in `m[-1:-2,]`

for the case `n=1`

.

*Thanks to Neil for pointing out a 1 byte golf.*

## R, ~~150~~ ~~138~~ 132 bytes

```
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]}
```

Try it online!

Implements the formula \$T(n,k)=Fib(k+1)\cdot\lfloor n\cdot\phi\rfloor+Fib(k)\cdot(n-1)\$ 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) `double`

s, 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 `double`

s be worrisome, and then the language would be `R + gmp`

.

## Jelly, ~~27~~ 24 bytes

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

Try it online!

Monadic link using 1-based indexing.
Thanks to @JonathanAllan for a better way of getting the row and columns from `n`

and saving 3 bytes. In its shortest form it’s too slow for larger n on TIO, so the following Try it online! reduces the size of the initial list of rows and columns at the cost of three bytes.

### Explanation

```
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
```

Note this is based on the description of the Python code on the OEIS page.

## Wolfram Language (Mathematica), 90 bytes

```
Flatten[Table[(F=Fibonacci)[a+1]⌊(b-a+1)GoldenRatio⌋+(b-a)F@a,{b,#},{a,b,1,-1}]][[#]]&
```

Try it online!