We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Minimal Distance to Pi
Minimal Distance to Pi
Sort by
recency
|
44 Discussions
|
Please Login in order to post a comment
Interesting problem! Trying to find the closest rational approximation to pi is always fascinating. Has anyone explored using continued fractions? They often provide surprisingly accurate results. Also, this reminds me of the addictive puzzle game Wordle Unlimited, in its own way – searching for the correct answer among many possibilities. Perhaps a visual representation could help illustrate the distance more intuitively. Looking forward to seeing more solutions!
Here's an update on where I am at on this challenge: I now got it to 15.00 points with test cases 0 - 10 passed of the 50 ones. Any suggestions please?
include
using namespace std; const long double PI = 3.1415926535897932384626433832795028841971693993751; string ltrim(const string &); string rtrim(const string &); vector split(const string &);
int main() { string first_multiple_input_temp; getline(cin, first_multiple_input_temp);
{
long num, denom; double dist1, dist2, minD = PI; long n; //cout << PI << "\t" << to_string(PI) << endl;
dist1 = abs(((n / d) - PI)); //cout << static_cast ((n / d) )<< endl; n++; // cout << n << "\t" << d << endl; dist2 = abs(((n / d) - PI)); //cout << static_cast ((n / d) )<< endl; // cout << dist1 << '\t' << dist2 << endl; if(dist1 < dist2){ if(minD > dist1){ num = n - 1; denom = d; minD = dist1; } } if(dist2 < dist1){ if(minD > dist2){ num = n; denom = d; minD = dist2; } }
/* if(dist1 == minD || dist2 == minD){ num = n; denom = d;
if(minD == 0) break; } cout << num << "/" << denom << endl; } return 0; }
string ltrim(const string &str) { string s(str);
}
string rtrim(const string &str) { string s(str);
}
vector split(const string &str) { vector tokens;
}
Here's part of my code: It passed test case 0, but failed the other test cases. I am wondering where I am going wrong. Any suggestions? Thank you.
include
using namespace std; const long double PI = 3.141592653589793284626433832795028841971693993751; string ltrim(const string &); string rtrim(const string &); vector split(const string &);
int main() { string first_multiple_input_temp; getline(cin, first_multiple_input_temp);
{
long num, denom; double dist1, dist2, minD = PI; double n; //cout << PI << "\t" << to_string(PI) << endl;
dist1 = abs(((n / d) - PI)); //cout << static_cast ((n / d) )<< endl; n++; // cout << n << "\t" << d << endl; dist2 = abs(((n / d) - PI)); //cout << static_cast ((n / d) )<< endl; // cout << dist1 << '\t' << dist2 << endl; if(dist1 < dist2){ if(minD > dist1){ num = n; denom = d; minD = dist1; } } if(dist2 < dist1){ if(minD > dist2){ num = n; denom = d; minD = dist2; } }
/* if(dist1 == minD || dist2 == minD){ num = n; denom = d;
} return 0; }
minimal distance to pi code