0 views

Uploaded by Lucky1

save

- 1. Introduction to Algorithms
- 451 Sample Questions 2
- TCS
- chapter 6 review
- Nota Bengkel 2015 SKAS(2) Bengkel Matematik 2015
- AoPS Post Test Prealgebra
- 451 Sample Questions 2
- (Www.entrance Exam.net) 451 Sample Questions(TCS)
- 63800499 TCS Sample Questions
- Basic Mathematics
- sample raft-1
- Mth 10 Rev Sheets
- L5Number
- topic 12 kud
- 1.1adding Dissimilar Fractions (1)
- Math Foundations Session 1 Arithmetic
- Score a Notes3
- CAT Quant - Factorials by Ashank Dubey (CAT 16 100%iler)
- Math Knowing Our Numbers Integers
- midterm study guide
- ch16 (1)
- el-cambio-educativo-desde-la-investigacion-accion
- Hoja de Trabajo PDTF.pdf
- 49-687-1-PB
- --رؤى الرؤساء التنفيذيين في المملكة العربية السعودية
- Biodiesel Production with Green Technologies - Islam e Ravindra (2017).pdf
- Ejercicios Ley de Ohm
- 464 Sop Capping Pulpa
- SK JENIS PELY OK.docx
- LP-CKD-ETC-HT-HD-docx.docx
- propuesta-Artística-sexto-grado.docx
- SSSS.docx
- 16305-64822-1-PB
- World%20Steel%20in%20Figures%202018.pdf
- Contenido Unidad I Oblig II
- 242474280-Kebijakan-Penolakan-BHD-Resusitasi-Tindakan-Atau-Pengobatan.doc
- Ivanov
- 01_Putri Regina(13116090)_EL3102_M Daniel Firdaus.pdf
- Modelo de Cargo
- BODEGON (2da. Parte)
- ACUT CORONARY.pdf
- IMSLP541542-PMLP874928-Krebs-organ-works-vol1.pdf
- Scaffolding Independent Design(Senipah).doc
- KELANTAN PERCUBAAN 2018 SAINS K1.pdf
- akses-ke-rumah-sakit-dan-kontinuitas-pelayanan-ark-45.pdf
- Brosur Ppk
- Manual de Calidad de Simarc Final (Reparado)
- Pravilnik_o_zastiti_na_radu_prilikom_izvodjenja_gradjevinskih_radova.pdf
- Teologia Salmos
- Pregunta de Investigación
- GTWA.pdf
- Bag in Voice
- 246616207-Understanding-Machine-Learning.pdf
- Chandamama-2003-6
- Nodejs 0.8.15 API Documentation
- Data Structures and the Java Collections Framework, 3rd Edition.pdf
- Chandamama-1953-6.pdf
- Data Structures and the Java Collections Framework, 3rd Edition.pdf
- VBScript Tutorial
- Tutorial Guide v Writing Test
- 01_Matrices_and_Vectors_9_min.pdf
- Generics, Inheritance, and Subtypes (The Java™ Tutorials _ Learning the Java Language _ Generics (Updated))
- README_AS5K_v2.1.3
- File API
- Regular Expressions - JavaScript _ MDN
- Invoice Philips Trimmer
- Java Virtual Machine's Internal Architecture
- Job Description
- Spring Boot Reference
- ఒక పెళ్ళాం ముగ్గురు మొగుళ్ళు_తెలుగు సెక్స్ కథలు _ Telugu Sex Stories
- సరళ దూల తీర్చిన ఇంటి ఒనరు _ Telugu Sex Stories.pdf
- Industrial Devpt. Policy
- సేల్స్ స్టార్_Part 4_Telugu Sex Kathalu _ Telugu Sex Stories

You are on page 1of 2

Fibonacci numbers

**The Fibonacci numbers form a sequence of integers defined recursively in the following way.
**

The first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number

is the sum of the previous two.

0

for n = 0,

Fn = 1 for n = 1,

F + Fn−2 for n > 1.

n−1

The first twelve Fibonacci numbers are:

0 1 1 2 3 5 8 13 21 34 55 89

0 1 2 3 4 5 6 7 8 9 10 11

**Notice that recursive enumeration as described by the definition is very slow. The definition
**

of Fn repeatedly refers to the previous numbers from the Fibonacci sequence.

**11.1: Finding Fibonacci numbers recursively.
**

1 def fibonacci(n):

2 if (n <= 1):

3 return n

4 return fibonacci(n - 1) + fibonacci(n - 2)

**The above algorithm performs Fn additions of 1, and, as the sequence grows exponentially,
**

we get an inefficient solution.

Enumeration of the Fibonacci numbers can be done faster simply by using a basis of

dynamic programming. We can calculate the values F0 , F1 , . . . , Fn based on the previously

calculated numbers (it is sufficient to remember only the last two values).

**11.2: Finding Fibonacci numbers dynamically.
**

1 def fibonacciDynamic(n):

2 fib = [0] * (n + 2)

3 fib[1] = 1

4 for i in xrange(2, n + 1):

5 fib[i] = fib[i - 1] + fib[i - 2]

6 return fib[n]

**The time complexity of the above algorithm is O(n).
**

Copyright 2013 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure

prohibited.

1

11.1. Faster algorithms for Fibonacci numbers

Fibonacci numbers can be found in O(log n) time. However, for this purpose we have to use

matrix multiplication and the following formula:

" #n " #

1 1 F Fn

= n+1 , for n 1.

1 0 Fn Fn−1

**Even faster solution is possible by using the following formula:
**

√ √

( 1+2 5 )n − ( 1−2 5 )n

Fibn = √ (11.1)

5

These algorithms are not trivial and it will be presented in the future lessons.

11.2. Exercise

Problem: For all the given numbers x0 , x1 , . . . , xn−1 , such that 1 ¬ xi ¬ m ¬ 1 000 000,

check whether they may be presented as the sum of two Fibonacci numbers.

Solution: Notice that only a few tens of Fibonacci numbers are smaller than the maximal

m (exactly 31). We consider all the pairs. If some of them sum to k ¬ m, then we mark

index k in the array to denote that the value k can be presented as the sum of two Fibonacci

numbers.

In summary, for each number xi we can answer whether it is the sum of two Fibonacci

numbers in constant time. The total time complexity is O(n + m).

Every lesson will provide you with programming tasks at http://codility.com/train.

2

- 1. Introduction to AlgorithmsUploaded bybloggerbutt
- 451 Sample Questions 2Uploaded byAnuraj Sharma
- TCSUploaded byAbhishek Chaurasia
- chapter 6 reviewUploaded byapi-236739188
- Nota Bengkel 2015 SKAS(2) Bengkel Matematik 2015Uploaded byZamri Hashim
- AoPS Post Test PrealgebraUploaded byRyan Mills
- 451 Sample Questions 2Uploaded byVenkat Chary
- (Www.entrance Exam.net) 451 Sample Questions(TCS)Uploaded byBhano Uday
- 63800499 TCS Sample QuestionsUploaded byRohan Sathe
- Basic MathematicsUploaded bynetheulalio
- sample raft-1Uploaded byapi-215070525
- Mth 10 Rev SheetsUploaded byXyzer
- L5NumberUploaded bySai Saikrishna
- topic 12 kudUploaded byapi-275333643
- 1.1adding Dissimilar Fractions (1)Uploaded byGim Reyes
- Math Foundations Session 1 ArithmeticUploaded byBen Holloway
- Score a Notes3Uploaded byMaryam Jameelah Md Hassan
- CAT Quant - Factorials by Ashank Dubey (CAT 16 100%iler)Uploaded byAnonymous ETg9woW5
- Math Knowing Our Numbers IntegersUploaded byBoon India Trichy
- midterm study guideUploaded byapi-374735823

- GTWA.pdfUploaded byRithuik Yerrabrolu
- Bag in VoiceUploaded byRithuik Yerrabrolu
- 246616207-Understanding-Machine-Learning.pdfUploaded byRithuik Yerrabrolu
- Chandamama-2003-6Uploaded byRithuik Yerrabrolu
- Nodejs 0.8.15 API DocumentationUploaded byRithuik Yerrabrolu
- Data Structures and the Java Collections Framework, 3rd Edition.pdfUploaded byRithuik Yerrabrolu
- Chandamama-1953-6.pdfUploaded byRithuik Yerrabrolu
- Data Structures and the Java Collections Framework, 3rd Edition.pdfUploaded byRithuik Yerrabrolu
- VBScript TutorialUploaded byRithuik Yerrabrolu
- Tutorial Guide v Writing TestUploaded byRithuik Yerrabrolu
- 01_Matrices_and_Vectors_9_min.pdfUploaded byRithuik Yerrabrolu
- Generics, Inheritance, and Subtypes (The Java™ Tutorials _ Learning the Java Language _ Generics (Updated))Uploaded byRithuik Yerrabrolu
- README_AS5K_v2.1.3Uploaded byRithuik Yerrabrolu
- File APIUploaded byRithuik Yerrabrolu
- Regular Expressions - JavaScript _ MDNUploaded byRithuik Yerrabrolu
- Invoice Philips TrimmerUploaded byRithuik Yerrabrolu
- Java Virtual Machine's Internal ArchitectureUploaded byRithuik Yerrabrolu
- Job DescriptionUploaded byRithuik Yerrabrolu
- Spring Boot ReferenceUploaded byRithuik Yerrabrolu
- ఒక పెళ్ళాం ముగ్గురు మొగుళ్ళు_తెలుగు సెక్స్ కథలు _ Telugu Sex StoriesUploaded byRithuik Yerrabrolu
- సరళ దూల తీర్చిన ఇంటి ఒనరు _ Telugu Sex Stories.pdfUploaded byRithuik Yerrabrolu
- Industrial Devpt. PolicyUploaded byRithuik Yerrabrolu
- సేల్స్ స్టార్_Part 4_Telugu Sex Kathalu _ Telugu Sex StoriesUploaded byRithuik Yerrabrolu