Guest User

Untitled

a guest
Dec 10th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include <algorithm>
  2. #include <vector>
  3. #include <numeric>
  4. #include <iostream>
  5.  
  6. class Fibonacci
  7. {
  8. public:
  9.     // An empty constructor that initializes the object.
  10.     Fibonacci();
  11.    
  12.     // A constructor that initializes the object to the nth member in the Fibonacci series.
  13.     Fibonacci(size_t n);
  14.    
  15.     // Overloading of the () -operator, which always advances the sequence to its next value.
  16.     size_t operator()();
  17.    
  18.     // A function that returns the index of the next value in the series. Remember that indexing starts from 0.
  19.     size_t getIndex() const;
  20.    
  21.     /* In addition to the functions above the Fibonacci class must have an overloaded output stream operator,
  22.     that prints the current value of the series to the output stream. Printing the value of an object which has not yet been used
  23.     (no values have been calculated using the () -operator), is implementation defined (do as you like). */
  24.     friend std::ostream& operator<< (std::ostream& out, Fibonacci f);
  25. private:
  26.     size_t n;
  27.     size_t value;
  28.     size_t fn1; // f(n-1)
  29.     size_t fn2; // f(n-2)
  30. };
  31.  
  32. class OEISA090000 {
  33. public:
  34.     // An empty constructor that initializes the object.
  35.     OEISA090000();
  36.    
  37.     // A constructor that initializes the object to the nth member in the OEISA090000 series.
  38.     OEISA090000(size_t n);
  39.    
  40.     // Overloading of the () -operator, which always advances the sequence to its next value.
  41.     size_t operator()();
  42.    
  43.     // A function that returns the index of the next value in the series. Remember that indexing starts from 0.
  44.     size_t getIndex() const;
  45.    
  46.     //In addition to the functions above the OEISA090000 class must have an overloaded output stream operator,
  47.     //that prints the current value of the series to the output stream. Printing the value of an object which has not yet been used
  48.     //(no values have been calculated using the () -operator), is implementation defined (do as you like).
  49.     friend std::ostream& operator<< (std::ostream& out, OEISA090000 o);
  50. private:
  51.     size_t n;
  52.     size_t current;
  53.     size_t value;
  54.     size_t idx;
  55. };
  56.  
  57. bool isPrime(size_t number);
  58. size_t longestOne(size_t number);
  59.  
  60. #include <algorithm>
  61. #include <vector>
  62. #include <numeric>
  63. #include <iostream>
  64. #include "series.hh"
  65.  
  66. //time 8h 30min
  67.  
  68. Fibonacci::Fibonacci() { fn2 = 0; fn1 = 1; n = 0;}
  69.  
  70. Fibonacci::Fibonacci(size_t n) : n(0)
  71. { fn2 = 0; fn1 = 1;
  72. size_t ankka = n-1;
  73.     for (size_t i = 0; i < ankka; i++) {
  74.     value = (*this)();
  75. } }
  76.  
  77. size_t Fibonacci::operator()() {
  78.     size_t temp;
  79.     if (n == 0) {
  80.         temp = 0;
  81.     } else if (n == 1) {
  82.         temp = 1;
  83.     } else {
  84.         temp = fn1 + fn2;
  85.         fn2 = fn1;
  86.         fn1 = temp; // for the next homo
  87.     }
  88.     n++;
  89.     return temp;
  90. }
  91.  
  92. size_t Fibonacci::getIndex() const {
  93.     return n+1;
  94. }
  95.  
  96. std::ostream& operator<< (std::ostream& out, Fibonacci f) {
  97.     out << f();
  98.     return out;
  99. }
  100.  
  101. // needed: function calculating primes, function calculating longest 1-row
  102. OEISA090000::OEISA090000() { current = 1; idx = 0;}
  103.  
  104. OEISA090000::OEISA090000(size_t n) {
  105. // search n:th prime
  106. current = 1;
  107. idx = 0;
  108. for (size_t i = 0; i < n-1; i++) {
  109.     value = (*this)();
  110. }
  111. idx++;
  112. }
  113.  
  114. size_t OEISA090000::operator()() {
  115.     // search next prime. if 2, idx 0, ei idx++
  116.     if (current == 1) {
  117.         // first prime, 2
  118.         current = 2;
  119.         return 1;
  120.     }
  121.     for (size_t i = current+1;;i++) {
  122.         if (isPrime(i) == true) {
  123.             current = i;
  124.             idx++;
  125.             break;
  126.         }
  127.     }
  128.     //count longest series of number 1s
  129.     return longestOne(current);
  130. }
  131.  
  132. size_t OEISA090000::getIndex() const {
  133.     return idx+1;
  134. }
  135.  
  136. std::ostream& operator<< (std::ostream& out, OEISA090000 o) {
  137.     out << o();
  138.     return out;
  139. }
  140.  
  141. bool isPrime(size_t number) {
  142.     if (number == 2) {
  143.         return true;
  144.     }
  145.     for (size_t i = 2; i < number; i++) {
  146.         if (number % i == 0) {
  147.             return false;
  148.         }
  149.     }
  150.     return true;
  151. }
  152.  
  153. size_t longestOne(size_t number) {
  154.     size_t remainder = 0, length = 0, longest = 0;
  155.     while (number != 0) {
  156.         remainder = number % 2;
  157.         number = number/2;
  158.         if (remainder == 1) {
  159.             length++;
  160.         } else {
  161.             if (length > longest) {
  162.                 longest = length;
  163.             }
  164.             length = 0;
  165.         }
  166.     }
  167.     if (length > longest) {
  168.         longest = length;
  169.     }
  170.     return longest;
  171. }
Add Comment
Please, Sign In to add comment