Skip to content

Instantly share code, notes, and snippets.

@donno
Created February 11, 2012 14:55
Show Gist options
  • Save donno/1800324 to your computer and use it in GitHub Desktop.
Save donno/1800324 to your computer and use it in GitHub Desktop.
Provides a way to iterate over the Fibonacci sequence.
//===----------------------------------------------------------------------===//
//
// NAME : FibonacciGenerator
// VERSION : 0.2
// NAMESPACE : Global namespace.
// PURPOSE : Provided a way to iterate over the Fibonacci sequence.
// COPYRIGHT : (c) 2012 Sean Donnellan. All Rights Reserved.
// AUTHORS : Sean Donnellan ([email protected])
// DESCRIPTION : Provided a way to iterate over the Fibonacci sequence.
//
// Supports ++, != and * operators.
//
// Example usage:
// FibonacciGenerator g;
// This will print out the first 10 Fibonacci numbers.
// std::for_each(FibonacciGenerator::begin(),
// FibonacciGenerator::end(10),
// [](int fib){
// std::cout << fib << " ";
// });
//
// This makes it incrediablity easy if you want to know
// the sum of the first 10 Fibonacci numbers.
// std::accumulate(FibonacciGenerator::begin(),
// FibonacciGenerator::end(10),
// 0);
//
//===----------------------------------------------------------------------===//
class FibonacciGenerator
{
// last and secondlast is used in the calcuations.
unsigned long last;
unsigned long secondlast;
// This is used for dealing with iteration.
// n keeps track of the which 'n'-th Fibonacci number we are up to.
unsigned int n;
FibonacciGenerator(unsigned int n);
public:
FibonacciGenerator& operator ++();
// Returns the 'n-th' fibonacci number.
unsigned long operator *() const;
bool operator !=(const FibonacciGenerator& That) const;
// Starts at the beging of the sequence.
static FibonacciGenerator begin();
// Defines the termination case.
// Note: Deferencing this is meaninless, it is mearly a terminator.
static FibonacciGenerator end(unsigned int nth);
};
//===----------------------------------------------------------------------===//
FibonacciGenerator::FibonacciGenerator(unsigned n)
: last(1), secondlast(0), n(n)
{
}
FibonacciGenerator& FibonacciGenerator::operator ++() {
last = last + secondlast;
secondlast = last - secondlast;
++n;
return *this;
}
unsigned long FibonacciGenerator::operator *() const {
return secondlast;
}
bool FibonacciGenerator::operator !=(const FibonacciGenerator& That) const {
return n != That.n;
}
FibonacciGenerator FibonacciGenerator::begin() {
return FibonacciGenerator(0);
}
FibonacciGenerator FibonacciGenerator::end(unsigned int nth) {
return FibonacciGenerator(nth);
}
//===----------------------------------------------------------------------===//
#include <algorithm>
#include <iostream>
#include <numeric>
int main()
{
std::cout << "Fibonacci: ";
std::for_each(FibonacciGenerator::begin(), FibonacciGenerator::end(10),
[](int fib){ std::cout << fib << " "; });
std::cout << std::endl;
std::cout << "Sum of first 10 Fibonacci numbers: "
<< std::accumulate(FibonacciGenerator::begin(),
FibonacciGenerator::end(10), 0)
<< std::endl;
return 0;
}
//===----------------------------------------------------------------------===//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment