A fast algorithm for computing large Fibonacci numbers

Daisuke Takahashi

Department of Information and Computer Sciences, Saitama University, 255 Shimo-Okubo, Urawa-shi, Saitama 338-8570, Japan Received 13 March 2000; received in revised form 19 June 2000 Communicated by K. Iwama

Abstract We present a fast algorithm for computing large Fibonacci numbers. It is known that the product of Lucas numbers algorithm uses the fewest bit operations to compute the Fibonacci number Fn . We show that the number of bit operations in the conventional product of Lucas numbers algorithm can be reduced by replacing multiplication with the square operation. © 2000 Elsevier Science B.V. All rights reserved. Keywords: Program derivation; Fibonacci numbers

1. Introduction

1.1. Fibonacci and Lucas numbers We deﬁne the Fibonacci numbers as F0 = 0, F1 = 1, n 0. (1)

Many algorithms for computing Fibonacci numbers have been well studied [11,12,5,8,4,3,7,9,2]. It is known that the product of Lucas numbers algorithm uses the fewest bit operations to compute Fn [2]. In this paper, we present a fast algorithm for computing large Fibonacci numbers. This algorithm is based on the product of Lucas numbers algorithm [9, 2]. The conventional product of Lucas numbers algorithm uses multiplication and square operation. In general, it is known that the number of bit operations used to square an n-bit number is less than that used to multiply two n-bit numbers. Thus, the number of bit operations in the conventional product of Lucas numbers algorithm can be reduced by replacing multiplication with the square operation.

Fn+2 = Fn+1 + Fn ,

The Lucas numbers are deﬁned as L0 = 2, L1 = 1, n 0. (2)

Ln+2 = Ln+1 + Ln ,

We also have the formulas αn − β n , α−β Ln = α n + β n , Fn = (3)

E-mail address: daisuke@ics.saitama-u.ac.jp (D. Takahashi).

(4) √ √ where α = (1 + 5)/2 and β = (1 − 5)/2. We will use γ n to represent the number of bits in Fn where γ = log2 α ≈ 0.69424 since Fn is asymptotic to α n /5 [2].

0020-0190/00/$ – see front matter © 2000 Elsevier Science B.V. All rights reserved. PII: S 0 0 2 0 - 0 1 9 0 ( 0 0 ) 0 0 1 1 2 - 5

244

D. Takahashi / Information Processing Letters 75 (2000) 243–246

1.2. Multiplication and square operations It is known that multiplication of n-bit numbers can be performed in O(n log n log log n) bit operations by using the Schönhage–Strassen algorithm [10] which is based on the fast Fourier transform (FFT) [1]. In multiplication of many thousand bits or more, FFTbased multiplication is the fastest. In this paper, we use FFT-based multiplication for computing large Fibonacci numbers. We denote the number of bit operations used to multiply two nbit numbers as M(n), and the number of bit operations used to square an n-bit number as S(n). Also, we assume M(n) = O(n log n log log n) and S(n) = O(n log n log log n). In general, we have S(n) M(n). From (a + b)2 − (a − b)2 ab = 4 we have M(n) 2S(n). Then, we have 1 2 M(n)

fib(n) f ←1 l←3 for i = 2 to log2 n − 1 f ←f ∗l l ←l∗l−2 f ←f ∗l return f Fig. 1. Conventional product of Lucas numbers algorithm [2].

L2k = α 2k + β 2k = (α k + β k )2 − 2(αβ)k = L2 − 2 · (−1)k . k We can compute F2i [2] by i−1

(6)

F2i =

j =0

L2j .

(7)

S(n)

M(n).

In FFT-based multiplication and square operations, computation of FFTs consumes the most computation time. Although three FFTs are needed to multiply two n-bit numbers, only two FFTs are needed to square an n-bit number. Therefore we assume S(n) ≈ 2 3 M(n) in FFT-based multiplication and square operations. Since the number of bit operations used to add/subtract two n-bit numbers is O(n) and the number of bit operations used to multiply/divide an n-bit number by an O(1)-bit number is also O(n), no consideration of these is made in this paper.

The conventional product of Lucas numbers algorithm to compute Fn (n = 2i , i 2) is shown in Fig....