Problem Set 2
Problem Set 2
Siti Hawa
Marker's comments:
1 40
2 70
Total 110
Extension certification:
Signature of Convener:
1
COS30008 Semester August 2024 Ms. Siti Hawa
In mathematics, the Fibonacci numbers (or the Fibonacci sequence) are an infinite series of
positive numbers with the following pattern
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, …
For n ≥ 3, we can define this sequence recursively by
Fibonacci( n ) = Fibonacci( n – 1 ) + Fibonacci( n – 2 ),
with seed values
Fibonacci( 1 ) = 1 and Fibonacci( 2 ) = 1.
Fibonacci numbers appear in numerous places, including computer science and biology.
Unfortunately, evaluating the Fibonacci sequence for a given n in a recursive and bottom-up
fashion is computationally expensive and may exceed available resources (in terms of both
space and time). The recursive definition calculates the smaller values of Fibonacci( n ) first
and then builds larger values from them. This process has O(2n) complexity (see lecture slides
page 179).
An alternative mathematical formulation of the Fibonacci sequence is due to dynamic
programming, a technique developed by Richard E. Bellmann in the 1940s while working for
the RAND Corporation. Dynamic programming uses memorization to save values that have
already been calculated. This yields a top-down approach that allows Fibonacci( n ) to be split
into sub-problems and then calculate and store values. This method produces a very efficient
iterative algorithm to generating the Fibonacci sequence.
The iterative formulation of the Fibonacci sequence uses two storage cells, previous and
current, to keep track of the values computed so far:
Fibonacci( n ) =
previous := 0;
current := 1;
for i := 1 to n do
next := current + previous;
previous := current;
current := next;
end;
For n ≥ 1, this algorithm produces the desired sequence in linear time and constant space.
1 Source: http://en.wikipedia.org/wiki/File:Fibonacci.png
2
COS30008 Semester August 2024 Ms. Siti Hawa
Problem 1:
Using the dynamic programming solution, we can construct a C++ class, called
FibonacciSequenceGenerator, that produces the correct Fibonacci sequence up to the
maximum integer value representable on a given computer architecture (e.g., 264-1).
#pragma once
#include <string>
#include <cstddef>
class FibonacciSequenceGenerator
{
private:
const std::string fID; // sequence identifier
long long fPrevious; // previous Fibonacci number (initially 0)
long long fCurrent; // current Fibonacci number (initially 1)
public:
// Get sequence ID
const std::string& id() const noexcept;
3
COS30008 Semester August 2024 Ms. Siti Hawa
4
COS30008 Semester August 2024 Ms. Siti Hawa
Problem 2:
The class FibonacciSequenceIterator implements a standard forward iterator for
FibonacciSequenceGenerator objects. It maintains to instance variables: an object of
class FibonacciSequenceGenerator and the iterator position.
Please note that Fibonacci iterators maintain a copy of the underlying collection. Technically,
this make iterator comparison difficult if not impossible. However, all objects of class
FibonacciSequenceGenerator have an id string and we can obtain it via method id().
Consequently, two iterators of class FibonacciSequenceIterator are positioned on the
same element (i.e., the same Fibonacci number) if their sequence objects has the same id
string and if their respective indices are equal.
#pragma once
#include "FibonacciSequenceGenerator.h"
class FibonacciSequenceIterator
{
private:
FibonacciSequenceGenerator fSequenceObject; // sequence object
long long fIndex; // current iterator position
public:
// iterator constructor
// FibonacciSequence objects has an id to allow for comparision
FibonacciSequenceIterator( const FibonacciSequenceGenerator& aSequenceObject,
long long aStart = 1 ) noexcept;
// iterator methods
const long long& operator*() const noexcept; // return current Fibonacci number
FibonacciSequenceIterator& operator++() noexcept; // prefix, next Fibonacci number
FibonacciSequenceIterator operator++(int) noexcept; // postfix (extra unused argument)
5
COS30008 Semester August 2024 Ms. Siti Hawa
The file Main.cpp contains a test function to check your implementation. Uncomment
#define P2 and compile your solution with x64 (default on macOS). Your program should
produce the following output: