Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <vector>
- #include <numeric>
- #include <iostream>
- class Fibonacci
- {
- public:
- // An empty constructor that initializes the object.
- Fibonacci();
- // A constructor that initializes the object to the nth member in the Fibonacci series.
- Fibonacci(size_t n);
- // Overloading of the () -operator, which always advances the sequence to its next value.
- size_t operator()();
- // A function that returns the index of the next value in the series. Remember that indexing starts from 0.
- size_t getIndex() const;
- /* In addition to the functions above the Fibonacci class must have an overloaded output stream operator,
- that prints the current value of the series to the output stream. Printing the value of an object which has not yet been used
- (no values have been calculated using the () -operator), is implementation defined (do as you like). */
- friend std::ostream& operator<< (std::ostream& out, Fibonacci f);
- private:
- size_t n;
- size_t value;
- size_t fn1; // f(n-1)
- size_t fn2; // f(n-2)
- };
- class OEISA090000 {
- public:
- // An empty constructor that initializes the object.
- OEISA090000();
- // A constructor that initializes the object to the nth member in the OEISA090000 series.
- OEISA090000(size_t n);
- // Overloading of the () -operator, which always advances the sequence to its next value.
- size_t operator()();
- // A function that returns the index of the next value in the series. Remember that indexing starts from 0.
- size_t getIndex() const;
- //In addition to the functions above the OEISA090000 class must have an overloaded output stream operator,
- //that prints the current value of the series to the output stream. Printing the value of an object which has not yet been used
- //(no values have been calculated using the () -operator), is implementation defined (do as you like).
- friend std::ostream& operator<< (std::ostream& out, OEISA090000 o);
- private:
- size_t n;
- size_t current;
- size_t value;
- size_t idx;
- };
- bool isPrime(size_t number);
- size_t longestOne(size_t number);
- #include <algorithm>
- #include <vector>
- #include <numeric>
- #include <iostream>
- #include "series.hh"
- //time 8h 30min
- Fibonacci::Fibonacci() { fn2 = 0; fn1 = 1; n = 0;}
- Fibonacci::Fibonacci(size_t n) : n(0)
- { fn2 = 0; fn1 = 1;
- size_t ankka = n-1;
- for (size_t i = 0; i < ankka; i++) {
- value = (*this)();
- } }
- size_t Fibonacci::operator()() {
- size_t temp;
- if (n == 0) {
- temp = 0;
- } else if (n == 1) {
- temp = 1;
- } else {
- temp = fn1 + fn2;
- fn2 = fn1;
- fn1 = temp; // for the next homo
- }
- n++;
- return temp;
- }
- size_t Fibonacci::getIndex() const {
- return n+1;
- }
- std::ostream& operator<< (std::ostream& out, Fibonacci f) {
- out << f();
- return out;
- }
- // needed: function calculating primes, function calculating longest 1-row
- OEISA090000::OEISA090000() { current = 1; idx = 0;}
- OEISA090000::OEISA090000(size_t n) {
- // search n:th prime
- current = 1;
- idx = 0;
- for (size_t i = 0; i < n-1; i++) {
- value = (*this)();
- }
- idx++;
- }
- size_t OEISA090000::operator()() {
- // search next prime. if 2, idx 0, ei idx++
- if (current == 1) {
- // first prime, 2
- current = 2;
- return 1;
- }
- for (size_t i = current+1;;i++) {
- if (isPrime(i) == true) {
- current = i;
- idx++;
- break;
- }
- }
- //count longest series of number 1s
- return longestOne(current);
- }
- size_t OEISA090000::getIndex() const {
- return idx+1;
- }
- std::ostream& operator<< (std::ostream& out, OEISA090000 o) {
- out << o();
- return out;
- }
- bool isPrime(size_t number) {
- if (number == 2) {
- return true;
- }
- for (size_t i = 2; i < number; i++) {
- if (number % i == 0) {
- return false;
- }
- }
- return true;
- }
- size_t longestOne(size_t number) {
- size_t remainder = 0, length = 0, longest = 0;
- while (number != 0) {
- remainder = number % 2;
- number = number/2;
- if (remainder == 1) {
- length++;
- } else {
- if (length > longest) {
- longest = length;
- }
- length = 0;
- }
- }
- if (length > longest) {
- longest = length;
- }
- return longest;
- }
Add Comment
Please, Sign In to add comment