-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
154 lines (146 loc) · 5.12 KB
/
Copy pathmain.cpp
File metadata and controls
154 lines (146 loc) · 5.12 KB
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
#include <iostream>
#include <algorithm>
#include <random>
#include <string>
#include <ctime>
#include <fstream>
#include "Number.h"
class Multiplicator{
public:
static Number schoolGradeMultiplication(Number &a, Number &b)
{
Number res = Number("0");
std::string mainZeros;
for(int i = 0; i < a.num.length(); i ++)
{
int num = a.getd();
std::string zeros = mainZeros;
for(int j = 0; j < b.num.length(); j ++)
{
res += Number(std::to_string(b.getd() * num) + zeros);
zeros += "0";
}
mainZeros += "0";
}
if(res.num[0] == '0') // multiplication by 0 case
res.num = "0";
return res;
}
static Number divideAndConquer (Number a, Number b)
{
if(a.num.length() != b.num.length()) // equalizing length
{
std::string zeros;
if (a.num.length() > b.num.length())
{
int dif = a.num.length() - b.num.length();
for(int i = 0; i < dif; i++)
zeros += '0';
b.num = zeros + b.num;
} else
{
int dif = b.num.length() - a.num.length();
for(int i = 0; i < dif; i++)
zeros += '0';
a.num = zeros + a.num;
}
}
if(a.num.length() == 1 && b.num.length() == 1)
{
int res = a.getd() * b.getd();
return Number(std::to_string(res));
} else
{
Number z1 = a.splitLeft();
Number z2 = b.splitLeft();
Number q1 = a.splitRight();
Number q2 = b.splitRight();
std::string zeros;
for(int i = 0; i < q1.num.length(); i++)
zeros += '0';
Number x = divideAndConquer(z1, z2);
Number z = divideAndConquer(q1, q2);
Number y = divideAndConquer(z1 + q1, z2 + q2) - x - z;
x.num += zeros + zeros;
y.num += zeros;
Number res = x + y + z;
for(int i = 0; i < res.num.length() - 1; i++) // getting rid of zeros before numbers
{
if (res.num[0] == '0')
res.num.erase(0,1);
else
break;
}
return res;
}
}
static Number randomNumberTime(int k)
{
std::string num;
std::random_device rd;
std::mt19937 gen(clock());
// std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 9);
std::uniform_int_distribution<> dis1(1, 9);
num += dis1(gen) + '0';
for(int i = 0; i < k - 1; i++)
num += dis(gen) + '0';
return Number(num);
}
static Number randomNumber(int k)
{
std::string num;
std::random_device rd;
// std::mt19937 gen(clock());
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 9);
std::uniform_int_distribution<> dis1(1, 9);
num += dis1(gen) + '0';
for(int i = 0; i < k - 1; i++)
num += dis(gen) + '0';
return Number(num);
}
static std::vector<std::pair<double, double>> conductResearch(int k)
{
std::vector<std::pair<double, double>> experiment;
for(int i = 1; i <= k; i++)
{
double schoolTime = 0, smartTime = 0;
for(int j = 0; j < 3; j++)
{
Number a = randomNumber(i); // having 2 different constructors allows
Number b = randomNumberTime(i); // to get 2 dif numbers for each multiplication
const clock_t begin_time1 = clock();
schoolGradeMultiplication(a,b);
schoolTime += double(clock () - begin_time1);
const clock_t begin_time2 = clock();
divideAndConquer(a,b);
smartTime += double(clock () - begin_time2);
}
experiment.emplace_back(std::make_pair(schoolTime/3, smartTime/3));
std::cout << i <<'\t' << schoolTime << '\t' << smartTime << '\n';
}
// for( int i = 0; i < k - 1; i++)
// std::cout << experiment[i].first << '\t' << experiment[i].second << '\n';
return experiment;
}
static void CSV(std::vector<std::pair<double, double>> res)
{
std::ofstream file;
file.open ("Data.csv");
for (int i = 0; i < res.size(); i++)
file << i + 1 << ";" << res[i].first << ";" << res[i].second << "\n";
file.close();
}
};
int main() {
// Number a = Number("123456789");
// Number b = Number("1");
// Number s = Multiplicator::randomNumber(3);
// std::cout<<s.num<<std::endl;
// Number f = Multiplicator::randomNumber(3);
// std::cout<<f.num<<std::endl;
Multiplicator::CSV(Multiplicator::conductResearch(400));
// std::cout<<s.splitLeft().num<<std::endl;
// std::cout<<s.splitRight().num<<std::endl;
}