Home > Programming > project euler: Problem 25 1000-digit Fibonacci number

project euler: Problem 25 1000-digit Fibonacci number


The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

Solution:
This problem is straight forward. A very simple python code as follows

def func():
		f1 = 1
		f2 = 1
		term = 2
		while len(str(f2)) != 1000:
				term += 1
				f1, f2 = f2, f1 + f2
		print term

func()

time python fibo.py
Answer: 4782

real 0m0.276s
user 0m0.125s
sys 0m0.109s

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: