Progression Library 0.0.1
A header only arithmetic, geometric and fibonacci progression library
Loading...
Searching...
No Matches
progression.hpp
1
10#pragma once
11
12#include <format>
13#include <iostream>
14
30
41public:
49 virtual long first_value();
50
58 virtual long next_value() = 0;
59
66 void print_progression(int n);
67
69 virtual ~Progression() = default;
70
71protected:
72 long curr;
73 long first;
74};
75
84public:
91 ArithProgression(long i = 1, long f = 0);
92
99 long next_value() override;
100
101 friend std::ostream &operator<<(std::ostream &out,
102 const ArithProgression &sdt);
103
104protected:
105 long inc;
106};
107
116public:
123 GeometricProgression(long b = 2);
124
131 long next_value() override;
132
133protected:
134 long base;
135};
136
143class FibonacciPg : public Progression {
144protected:
145 long sec_v;
146 long prev;
147
148public:
156 FibonacciPg(long f = 0, long s = 1);
157
162 long first_value() override;
163
168 long next_value() override;
169};
170
172 inc = i;
173 first = f;
174 curr = f;
175};
176
178 curr = first;
179 return curr;
180}
181
183 curr += inc;
184 return curr;
185}
186
188 std::cout << std::format("{}", first_value());
189 for (int i = 2; i <= n; ++i) {
190 std::cout << std::format(" {} ", next_value());
191 };
192 std::cout << std::endl;
193}
194
195std::ostream &operator<<(std::ostream &out, const ArithProgression &apg) {
196 out << "first value: " << apg.first << " Current value: " << apg.curr
197 << "increment by: " << apg.inc << "\n";
198
199 return out;
200};
201
204 first = 1;
205 curr = 1;
206};
207
209 curr *= base;
210 return curr;
211}
212
214FibonacciPg::FibonacciPg(long first_value, long second_value) {
216 sec_v = second_value;
217 prev = sec_v - first;
218}
219
221 curr = first;
222 prev = sec_v - first;
223 return curr;
224}
225
227 long next = curr + prev;
228 prev = curr;
229 curr = next;
230 return curr;
231}
Arithmetic progression: each term is obtained by adding a fixed increment.
Definition progression.hpp:83
long next_value() override
Returns the next term in the arithmetic progression.
Definition progression.hpp:182
long inc
Fixed increment added at each step.
Definition progression.hpp:105
ArithProgression(long i=1, long f=0)
Constructs an arithmetic progression.
Definition progression.hpp:171
long sec_v
second value
Definition progression.hpp:145
long next_value() override
Calculates the next value in the progression.
Definition progression.hpp:226
long first_value() override
sets the first value
Definition progression.hpp:220
FibonacciPg(long f=0, long s=1)
FibonacciPg constructor Takes two arguments first value and second value.
Definition progression.hpp:214
long prev
previous value
Definition progression.hpp:146
long next_value() override
Returns the next term in the geometric progression.
Definition progression.hpp:208
long base
Fixed multiplier applied at each step.
Definition progression.hpp:134
GeometricProgression(long b=2)
Constructs a geometric progression.
Definition progression.hpp:203
A header only c++ progression library.
Definition progression.hpp:40
virtual long first_value()
Resets the progression and returns the first value.
Definition progression.hpp:177
long first
First value of the progression.
Definition progression.hpp:73
virtual ~Progression()=default
Virtual destructor to allow proper cleanup of derived objects.
long curr
Current value in the progression.
Definition progression.hpp:72
virtual long next_value()=0
Advances to and returns the next value in the progression.
void print_progression(int n)
Prints the first n terms of the progression.
Definition progression.hpp:187