-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.cpp
More file actions
46 lines (38 loc) · 1.02 KB
/
day2.cpp
File metadata and controls
46 lines (38 loc) · 1.02 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
#include<iostream>
#include<vector>
#include<cmath> //Ceil
#include <numeric> // gcd
using namespace std;
/*
The ancient Egyptians used to express fractions as a sum of several terms
where each numerator is one.For example, 4 / 13 can be represented as
1 / 4 + 1 / 18 + 1 / 468.
Create an algorithm to turn an ordinary fraction a / b,
where a < b, into an Egyptian fraction.
*/
/*
thoughts:
*/
void solution(int a, int b) {
vector<int> denominators;
while (a != 0) {
int n = ceil((double)b / a);
denominators.push_back(n);
a = a * n - b;
b = b * n;
int g = std::gcd(abs(a), abs(b)); // <-- use abs() here
a /= g;
b /= g;
}
cout << "Egyptian Fraction representation: ";
for (size_t i = 0; i < denominators.size(); ++i) {
cout << "1/" << denominators[i];
if (i != denominators.size() - 1) cout << " + ";
}
cout << endl;
}
int main(){
int a = 4, b = 13;
solution(a, b);
return 0;
}