OFFSET
0,4
COMMENTS
One way to calculate the Fibonacci numbers recursively is to use:
- F(0) = 0,
- F(1) = 1,
- F(2) = 1,
- F(n) = F((n + 1)/2)^2 + F((n - 1)/2)^2 for odd n,
- F(n) = F(n/2) * (F(n/2 - 1) + F(n/2 + 1)) for even n.
Proof: it is known that F(i) * F(j) + F(i + 1) * F(j + 1) = F(i + j + 1) (see formula section of A000045):
- for even n, let i = n/2 and j = n/2 - 1,
- for odd n, let i = j = (n + 1)/2.
This sequence gives the number of evaluations of F for calculating F(n). It is assumed that F needs to be evaluated more than once even if it has been evaluated before (no caching).
Conjecture: for large n, this sequence is bounded by a small constant times n^(4/3).
LINKS
Rémy Sigrist, Table of n, a(n) for n = 0..10000
GeeksforGeeks, Program for Fibonacci numbers, see method 6, but erroneously states to take O(log n) operations.
Rémy Sigrist, PARI program for A331124
FORMULA
a(0) = 1,
a(1) = 1,
a(2) = 1,
a(n) = a((n + 1)/2) + a((n - 1)/2) + 1, n odd and n > 2,
a(n) = a(n/2) + a(n/2 - 1) + a(n/2 + 1) + 1, n even.
EXAMPLE
a(5) = a(3) + a(2) + 1 = (a(2) + a(1) + 1) + a(2) + 1 = 5.
PROG
(Fortran)
program main
implicit none
integer, parameter :: pmax = 100
integer :: r, n
logical, dimension(0:pmax) :: cache
do n = 0, pmax
cache (0:2) = .true.
cache (3:pmax) = .false.
r = a(n)
write (*, fmt="(I0, ', ')", advance="no") r
end do
write (*, fmt="()")
contains
recursive function a (n) result(r)
integer, intent(in) :: n
integer :: r
if (cache(n)) then
r = 1
return
else if (mod(n, 2) == 1) then
r = a ((n + 1)/2) + a ((n - 1)/2) + 1
else
r = a (n/2) + a(n/2 - 1) + a(n/2 + 1) + 1
end if
cache (n) = .false.
end function a
end program main
(PARI) \\ See Links section.
CROSSREFS
KEYWORD
AUTHOR
Thomas König, Jan 10 2020
STATUS
approved