Skip to main content
added 1393 characters in body
Source Link
Dennis
  • 212k
  • 41
  • 381
  • 834

Julia, 27 26 18 bytes

!n=log(3n+.7)÷.48

This uses the inverse of Binet's formulaBinet's formula, with just enough precision for 32-bit integers; it actually works up to F(153) = 42,230,279,526,998,466,217,810,220,532,898 > 2105.

Try it online!

How it works

Binet's formula states the following.

Binet's formula

Restricting F to the set of Fibonacci, the map n → Fn has a right inverse F → nF.

We have that

right inverse of Binet's formula

and all that is left to do is dealing with the edge case 0.

Since the input is restricted to 32-bit integers, we can use short decimal literals instead of the constants in the formula.

  • log φ = 0.481211825059603447… ≈ 0.48

    Unfortunately, 0.5 isn't precise enough.

  • √5 = 2.2360679774997896964… ≈ 3

    That might seem like an awful approximation at first glance, but we're taking logarithms and since log 3 - log √5 = 0.29389333245105…, the result before rounding will be off by a small constant factor.

  • 0.5 ≈ 0.7

    Because of the excess from the previous approximation, we could actually omit this term altogether and still get correct results for F > 0. However, if F = 0, the logarithm will be undefined. 0.7 turned out to be the shortest value that extends our formula to F = 0.

Julia, 27 26 18 bytes

!n=log(3n+.7)÷.48

This uses the inverse of Binet's formula, with just enough precision for 32-bit integers; it actually works up to F(153) = 42,230,279,526,998,466,217,810,220,532,898 > 2105.

Try it online!

Julia, 27 26 18 bytes

!n=log(3n+.7)÷.48

This uses the inverse of Binet's formula, with just enough precision for 32-bit integers; it actually works up to F(153) = 42,230,279,526,998,466,217,810,220,532,898 > 2105.

Try it online!

How it works

Binet's formula states the following.

Binet's formula

Restricting F to the set of Fibonacci, the map n → Fn has a right inverse F → nF.

We have that

right inverse of Binet's formula

and all that is left to do is dealing with the edge case 0.

Since the input is restricted to 32-bit integers, we can use short decimal literals instead of the constants in the formula.

  • log φ = 0.481211825059603447… ≈ 0.48

    Unfortunately, 0.5 isn't precise enough.

  • √5 = 2.2360679774997896964… ≈ 3

    That might seem like an awful approximation at first glance, but we're taking logarithms and since log 3 - log √5 = 0.29389333245105…, the result before rounding will be off by a small constant factor.

  • 0.5 ≈ 0.7

    Because of the excess from the previous approximation, we could actually omit this term altogether and still get correct results for F > 0. However, if F = 0, the logarithm will be undefined. 0.7 turned out to be the shortest value that extends our formula to F = 0.

deleted 354 characters in body
Source Link
Dennis
  • 212k
  • 41
  • 381
  • 834

Julia, 2727 26 18 bytes

!n=int(n>0&&logn=log(φ,n*√5)3n+.7)÷.48

This uses the inverse of Binet's formula.

Try it online!


Julia, 26 bytes

!n=endof("$(66n^6)")÷1.24

This uses the clever string length trick fromBinet's formula, with just enough precision for 32-bit integers; it actually works up to @Hurkyl's Python answerF(153) = 42,230,279,526,998,466,217,810,220,532,898 > 2105.

The function expects a BigInt as argument. Try it onlineTry it online!

Julia, 27 bytes

!n=int(n>0&&log(φ,n*√5))

This uses the inverse of Binet's formula.

Try it online!


Julia, 26 bytes

!n=endof("$(66n^6)")÷1.24

This uses the clever string length trick from @Hurkyl's Python answer.

The function expects a BigInt as argument. Try it online

Julia, 27 26 18 bytes

!n=log(3n+.7)÷.48

This uses the inverse of Binet's formula, with just enough precision for 32-bit integers; it actually works up to F(153) = 42,230,279,526,998,466,217,810,220,532,898 > 2105.

Try it online!

added 644 characters in body
Source Link
Dennis
  • 212k
  • 41
  • 381
  • 834

Julia, 27 bytes

!n=int(n>0&&log(φ,n*√5))

This uses the inverse of Binet's formula.

Try it online!


Julia, 26 bytes

!n=endof("$(66n^6)")÷1.24

This uses the clever string length trick from @Hurkyl's Python answer.

The function expects a BigInt as argument. Try it online

Julia, 27 bytes

!n=int(n>0&&log(φ,n*√5))

Try it online!

Julia, 27 bytes

!n=int(n>0&&log(φ,n*√5))

This uses the inverse of Binet's formula.

Try it online!


Julia, 26 bytes

!n=endof("$(66n^6)")÷1.24

This uses the clever string length trick from @Hurkyl's Python answer.

The function expects a BigInt as argument. Try it online

Source Link
Dennis
  • 212k
  • 41
  • 381
  • 834
Loading