Skip to main content
added 49 characters in body
Source Link
user16488
user16488

Python, 36 3434 32 bytes

f=lambdalambda n:len(str(66*n**6))//1.24

Previous versionversions:

f=lambda n:len(str(66*n**6))//1.24
f=lambda n:(n*n*7).bit_length()//1.4

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

  • Use len(str(n)) to compute log base 10 without importing log (old version used .bit_length() to compute log base 2)
  • Raise n to a power, so that the approximation of the logarithm can distinguish between successive Fibonacci numbers
  • Multiplying by a constant scales up the values to get them in the correct range

Then the divisor was truncated to as little precision as I could manage and the multiplier chosen to give the correct results for all 32-bit fibonacci numbers.

Python, 36 34 bytes

f=lambda n:len(str(66*n**6))//1.24

Previous version:

f=lambda n:(n*n*7).bit_length()//1.4

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

  • Use len(str(n)) to compute log base 10 without importing log (old version used .bit_length() to compute log base 2)
  • Raise n to a power, so that the approximation of the logarithm can distinguish between successive Fibonacci numbers
  • Multiplying by a constant scales up the values to get them in the correct range

Then the divisor was truncated to as little precision as I could manage and the multiplier chosen to give the correct results for all 32-bit fibonacci numbers.

Python, 36 34 32 bytes

lambda n:len(str(66*n**6))//1.24

Previous versions:

f=lambda n:len(str(66*n**6))//1.24
f=lambda n:(n*n*7).bit_length()//1.4

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

  • Use len(str(n)) to compute log base 10 without importing log (old version used .bit_length() to compute log base 2)
  • Raise n to a power, so that the approximation of the logarithm can distinguish between successive Fibonacci numbers
  • Multiplying by a constant scales up the values to get them in the correct range

Then the divisor was truncated to as little precision as I could manage and the multiplier chosen to give the correct results for all 32-bit fibonacci numbers.

added 182 characters in body
Source Link
user16488
user16488

Python, 3636 34 bytes

f=lambda n:len(str(66*n**6))//1.24

Previous version:

f=lambda n:(n*n*7).bit_length()//1.4

Explanation

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

  • Use bit_lengthlen(str(n)) to compute log base 210 without importing log (old version used .bit_length() to compute log base 2)
  • SquareRaise n to a power, so that bit_lengththe approximation of the logarithm can distinguish between successive Fibonacci numbers
  • Multiplying by the constant 7 is a cheaper wayconstant scales up the values to add a valueget them in the correct range

Then the divisor was truncated to as little precision as I could manage and the multiplier 7 chosen to give the correct results for all 32-bit fibonacci numbers.

Python, 36 bytes

f=lambda n:(n*n*7).bit_length()//1.4

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

  • Use bit_length to compute log base 2 without importing log
  • Square n, so that bit_length can distinguish between successive Fibonacci numbers
  • Multiplying by the constant 7 is a cheaper way to add a value

Then the divisor was truncated to as little precision as I could manage and the multiplier 7 chosen to give the correct results for all 32-bit fibonacci numbers.

Python, 36 34 bytes

f=lambda n:len(str(66*n**6))//1.24

Previous version:

f=lambda n:(n*n*7).bit_length()//1.4

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

  • Use len(str(n)) to compute log base 10 without importing log (old version used .bit_length() to compute log base 2)
  • Raise n to a power, so that the approximation of the logarithm can distinguish between successive Fibonacci numbers
  • Multiplying by a constant scales up the values to get them in the correct range

Then the divisor was truncated to as little precision as I could manage and the multiplier chosen to give the correct results for all 32-bit fibonacci numbers.

Source Link
user16488
user16488

Python, 36 bytes

f=lambda n:(n*n*7).bit_length()//1.4

Explanation

The core idea is to invert the formula

fibonacci(n) ~ ( (1 + sqrt(5)) / 2)**n / sqrt(5)

which tells us that

log fibonacci(n) ~ n log((1 + sqrt(5)) / 2) - log(sqrt(5))

to get

f(n) ~ (log(n) + log(sqrt(5))) / log((1 + sqrt(5))/2)

The golfing optimizations are:

  • Use bit_length to compute log base 2 without importing log
  • Square n, so that bit_length can distinguish between successive Fibonacci numbers
  • Multiplying by the constant 7 is a cheaper way to add a value

Then the divisor was truncated to as little precision as I could manage and the multiplier 7 chosen to give the correct results for all 32-bit fibonacci numbers.