-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressions.cpp
176 lines (137 loc) · 3.67 KB
/
progressions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
class ArithmeticProgression{
private:
int d_ = 0;
vector <int> a_;
public:
void create(int start = 0, int d = 0){
a_.push_back(start);
d_ = d;
}
vector <int> get_sequence(){
return a_;
}
int get_difference(){
return d_;
}
void set_start_element(int start){
a_.clear();
a_.push_back(start);
}
void set_difference(int d){
int start = a_[0];
a_.clear();
a_.push_back(start);
d_ = d;
}
int get_nth(int n){
return a_[0] + (n - 1) * d_;
}
void count_to_nth(int n){
for(int _ = a_.size(); _ < n; _ ++){
a_.push_back(a_[a_.size() - 1] + d_);
}
}
vector <int> get_to_nth(int n){
count_to_nth(n);
return a_;
}
int get_sum_to_nth(int n){
return n * (a_[0] + get_nth(n)) / 2;
}
};
class GeometricProgression{
private:
int r_ = 1;
vector <int> b_;
public:
void create(int start = 1, int r = 1){
if(start == 0 || r == 0){
cout << "start_element or ratio can't be 0" << endl;
return;
}
b_.push_back(start);
r_ = r;
}
vector <int> get_sequence(){
return b_;
}
int get_ratio(){
return r_;
}
void set_start_element(int start){
if(start == 0){
cout << "start_element can't be 0" << endl;
return;
}
b_.clear();
b_.push_back(start);
}
void set_ratio(int r){
if(r == 0){
cout << "ratio can't be 0" << endl;
return;
}
int start = b_[0];
b_.clear();
b_.push_back(start);
r_ = r;
}
int get_nth(int n){
return b_[0] * pow(r_, n - 1);
}
void count_to_nth(int n){
for(int _ = b_.size(); _ < n; _ ++){
b_.push_back(b_[b_.size() - 1] * r_);
}
}
vector <int> get_to_nth(int n){
count_to_nth(n);
return b_;
}
int get_sum_to_nth(int n){
if(r_ == 1) return n * b_[0];
return (b_[0] * (pow(r_, n) - 1)) / (r_ - 1);
}
};
class HarmonicProgression{
private:
int d_ = 0;
vector <float> c_;
public:
void create(float start = 1.0, int d = 0){
c_.push_back(start);
d_ = d;
}
vector <float> get_sequence(){
return c_;
}
int get_difference(){
return d_;
}
void set_start_element(float start){
c_.clear();
c_.push_back(start);
}
void set_difference(int d){
float start = c_[0];
c_.clear();
c_.push_back(start);
d_ = d;
}
float get_nth(int n){
return pow((pow(c_[0], -1) + (n - 1) * d_), -1);
}
void count_to_nth(int n){
for(int _ = c_.size(); _ < n; _ ++){
c_.push_back(pow((pow(c_[c_.size() - 1], -1) + d_), -1));
}
}
vector <float> get_to_nth(int n){
count_to_nth(n);
return c_;
}
};