In this tutorial, we will learn how to generate a Fibonacci series up to a given number in Swift. We will understand what the Fibonacci series is, write an algorithm to generate it, and implement the algorithm step by step with a complete Swift program and output.
What is the Fibonacci Series?
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21...
The Fibonacci series is widely used in mathematics, computer science, and even nature to describe patterns such as spirals in shells and trees.
Algorithm to Generate Fibonacci Series
To generate a Fibonacci series up to a given number n
, follow these steps:
- Start with two initial numbers:
a = 0
andb = 1
. - Print the value of
a
. - Calculate the next number in the series as
c = a + b
. - Update
a
tob
andb
toc
. - Repeat the process until the value of
a
exceeds the given numbern
.
This algorithm is straightforward and ensures that all Fibonacci numbers less than or equal to n
are printed.
Step-by-Step Implementation in Swift
Let’s implement the algorithm in Swift step by step.
1. Define the Function
Create a function generateFibonacci
that accepts the maximum number n
as a parameter and prints the Fibonacci series:
func generateFibonacci(upTo n: Int) {
var a = 0 // First Fibonacci number
var b = 1 // Second Fibonacci number
while a <= n {
print(a, terminator: " ") // Print the current number
let c = a + b // Calculate the next number
a = b // Update a to b
b = c // Update b to the new value
}
print() // Move to the next line after printing the series
}
Explanation:
var a = 0
: Initializes the first Fibonacci number.
var b = 1
: Initializes the second Fibonacci number.
while a <= n
: Ensures that the series stops once the value of a
exceeds n
.
print(a, terminator: " ")
: Prints the current number without moving to the next line.
let c = a + b
: Calculates the next Fibonacci number.
a = b
and b = c
: Updates the variables for the next iteration.
2. Test the Function
Let’s test the function by generating Fibonacci series for different values of n
:
// Test cases
print("Fibonacci series up to 10:")
generateFibonacci(upTo: 10)
print("Fibonacci series up to 50:")
generateFibonacci(upTo: 50)
print("Fibonacci series up to 100:")
generateFibonacci(upTo: 100)
Each test case calls the function with a different maximum value, generating the corresponding Fibonacci series.
Complete Swift Program
Here’s the complete Swift program that generates Fibonacci series up to a given number:
main.swift
import Foundation
// Function to generate Fibonacci series up to a given number
func generateFibonacci(upTo n: Int) {
var a = 0 // First Fibonacci number
var b = 1 // Second Fibonacci number
while a <= n {
print(a, terminator: " ") // Print the current number
let c = a + b // Calculate the next number
a = b // Update a to b
b = c // Update b to the new value
}
print() // Move to the next line after printing the series
}
// Test cases
print("Fibonacci series up to 10:")
generateFibonacci(upTo: 10)
print("Fibonacci series up to 50:")
generateFibonacci(upTo: 50)
print("Fibonacci series up to 100:")
generateFibonacci(upTo: 100)
Output
Fibonacci series up to 10:
0 1 1 2 3 5 8
Fibonacci series up to 50:
0 1 1 2 3 5 8 13 21 34
Fibonacci series up to 100:
0 1 1 2 3 5 8 13 21 34 55 89
Program ended with exit code: 0
Screenshot from Xcode