c++ – How to convert a / b` to the sum of numbers like `1 / n`?

Question:

Task: Convert a/b to the sum of numbers like 1/n . For example, when a=3 and b=7 , then the program should output 3/7 = 1/3 + 1/11 + 1/231 .

I already thought of an algorithm, but I just can't write it.

The algorithm itself:

if a>b , then you need to remember an integer, for example, if a=10 and b=7 , then you need to bring to 3/7 and remember 1 as an integer.

If a < b , then everything is fine and the for loop starts. In it i = 2 and it should reach the value b . (After that, there is no point in checking.) Take b and divide by i . If the value is less than the value of a , then i becomes 1 more. And this continues until a > b/i . Once there the first such number, it is stored (ie, stored i ) to display) ( i = 3 , the console should be deduced 1/3+ . Then he stands out this value to the start and start all over again, until а not will be equal to 1 (Example: it was 3/7 and becomes 3/7 - 1/3 = 2/21 , а value is 2 , and b is assigned the value 21 )

my extraordinary code:

#include <iostream>
using namespace std;

int main(){
int a;
int b;
int c;
float d;
int k;
int g;
int e [k];  

cout << "значение а";
cin >>  a;
cout << "значение б";
cin >>  b;

if( a > b ){
    c = a / b;
    b = a - (b*c);

    return c, b;
}  else
for( int i = 2 ; i++ ; i >= b ){
    while(a>d){
        d = b / i;
    }

    int f = b;
    b = b * i;
    a = ( (b/f) * a ) - (b/i);

    e [k] = i;
    k = k+1;
}

while(e[k]>0){
    cout << "1/+" << e[k];
    k++;
}

}

Answer:

Fibonacci decomposition into Egyptian fractions in Python (long arithmetic is built-in), the code does not check that the fraction is correct.

def egy(p, q):
    res = []
    while p > 0:
        den = (q + p - 1) // p
        res.append(den)
        rem = (-q) % p
        p, q = rem, den * q
    return res

print(egy(3,7))
print(egy(7,15))
print(egy(5,121))

[Dbg]>>> 
[3, 11, 231]
[3, 8, 120]
[25, 757, 763309, 873960180913, 1527612795642093418846225]
Scroll to Top