I'm trying to code a program to write a fraction m/n
as a sum of different Egyptian Fractions (EFs) with the minimal number of EFs.
Here is my full code:
#include<bits/stdc++.h>
using namespace std;
int main(){
float a,b,sum=0,sct[100000];
float arr=1;
int stt=0;
cin>>a>>b;
while(sum!=a/b){
sum=sum+(1/arr);
float dif=a/b-sum;
printf("%f - sum = %f - dif = %f\n",arr,sum,dif);
if(dif>=0){
sct[stt]=arr;
cout<<"1/"<<arr<<"\n";
stt++;
} else {
sum=sum-(1/arr);
}
arr++;
}
cout<<stt<<"\n";
for(int m=0;m<stt;m++) cout<<sct[m]<<" ";
}
You can see well that if dif==0
then the process will end. But when arr==3
, I get dif==-0.000000
but the process doesn't end!
Output Caption:
I tried to fix it but all my efforts are useless. Do you know what should I change in my code?
Any help will be high appreciated!