University of Kirkuk
Student preparation : تابان شیرزاد خالد
Suppervisor : DR. AHMED MARWAN
Ministry of Higher Education
And Scientific Research
Kirkuk University
Collage of computer scince
and Information Technology
Department/Software
University of Kirkuk
Report: Fibonacci Theory
Introduction
Leonardo Fibonacci, also known as Leonardo of Pisa, was an
Italian mathematician born around 1170. He is best known for
introducing the Fibonacci sequence to Western mathematics
through his book Liber Abaci (The Book of Calculation)
published in 1202.
What is the Fibonacci Sequence?
The Fibonacci sequence is a series of numbers where each
number is the sum of the two preceding ones. It usually starts
like this: How Fibonacci Is Useful in Programming and
Technology
1. Algorithms and Problem Solving
• Fibonacci numbers are very important in teaching how
recursion and dynamic programming work.
• The Fibonacci sequence is a classic problem to learn about
optimization techniques like memoization.
University of Kirkuk
Example of Fibonacci with Recursion (C++):
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
Example with Dynamic Programming (C++):
int fibonacci(int n) {
int fib[n+2]; // array to store Fibonacci numbers
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; i++)
fib[i] = fib[i-1] + fib[i-2];
return fib[n];
}
2. Data Structures
• Fibonacci numbers are used in Fibonacci Heaps, an advanced
heap data structure.
• Fibonacci Heaps are used in graph algorithms like Dijkstra’s
algorithm for finding shortest paths more efficiently.
University of Kirkuk
3. Computer Graphics
• The Fibonacci sequence and the Golden Ratio are used to
create realistic models of plants, shells, trees, and galaxies.
• Many video games and simulations use Fibonacci-based
algorithms to make nature look natural.
4. Cryptography
• Fibonacci numbers have been used in designing encryption
algorithms.
• They can help generate complex pseudo-random numbers
for cryptographic keys.
5. Machine Learning and Artificial Intelligence
• Some optimization techniques, like Fibonacci Search, are
used for fine-tuning models.
University of Kirkuk
• Adjusting batch sizes or learning rates can sometimes follow
Fibonacci patterns for better results.
Conclusion
The Fibonacci sequence is not just a historical curiosity — it has
practical applications in coding, optimization, graphics,
encryption, and AI.
It proves that theoretical math can power real technological
advancements.
# Report on C++ Programming: Loops and Functions for the
Fibonacci Sequence
## Introduction
The Fibonacci sequence is one of the most famous
mathematical sequences frequently used in programming to
demonstrate the functionality of loops and functions. This
report discusses how to write a C++ program that generates the
Fibonacci sequence using loops and functions.
University of Kirkuk
## The Fibonacci Sequence
The Fibonacci sequence is a numerical series where each
number is the sum of the two preceding ones. The sequence
begins as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
## C++ Implementation
### 1. Using a Loop (for loop)#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
if(i == 1) {
University of Kirkuk
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
return 0;
}
### 2. Using a Function for a Specific Term#include <iostream>
using namespace std;
int fibonacci(int n) {
University of Kirkuk
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n;
cout << "Enter the term number: ";
cin >> n;
cout << "Fibonacci number at position " << n << " is: " <<
fibonacci(n);
return 0;
}
### 3. Using a Recursive Function to Generate the
Sequence#include <iostream>
using namespace std;
void printFibonacci(int n) {
static int t1 = 0, t2 = 1, nextTerm;
University of Kirkuk
if(n > 0) {
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
printFibonacci(n-1);
}
}
int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: 0, 1, ";
printFibonacci(n-2); // Because first two terms are already
shown
return 0;
University of Kirkuk
## Analysis
1. Loop Implementation: The first code demonstrates using a
for loop to generate the sequence. This is the most efficient
approach for this problem.
2. Recursive Function: The second code shows a recursive
function that returns the Fibonacci number at a specific
position.
3. Recursive Function for Sequence: The third code uses a
recursive function to display the entire sequence.
## Conclusion
The Fibonacci sequence serves as an excellent example to
demonstrate the differences between loop and function
implementations in C++. Each approach has its own advantages
and use cases, making the Fibonacci sequence a fundamental
exercise for understanding core programming concepts.
ChatGPT and DeepSeek :’’ Source’’
University of Kirkuk