Sort by

recency

|

44 Discussions

|

  • + 0 comments

    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!

  • + 1 comment
    import numpy as np
    from fractions import Fraction
    
    def closest_to_pi(min_val, max_val):
        best_fraction = None
        for i in range(min_val, max_val + 1):
            a = Fraction(int(np.pi) * i, i)
            b = Fraction(int(np.pi) * i + 1, i)
            
            if best_fraction is None or abs(np.pi - a) < abs(np.pi - best_fraction):
                best_fraction = a
            if abs(np.pi - b) < abs(np.pi - best_fraction):
                best_fraction = b
        
        return best_fraction
    
    result = closest_to_pi(1, 10)
    print(result)
    
  • + 0 comments

    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);

    vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
    
    long min = stol(first_multiple_input[0]);
    
    long max = stol(first_multiple_input[1]);
    
    // Write your code here
    if(min > 0 && max > min)
    

    {
    long num, denom; double dist1, dist2, minD = PI; long n; //cout << PI << "\t" << to_string(PI) << endl;

    for(double  d  = min; d <= max; d++){
        n = PI * d;
       // cout << n  << "\t" << d << "\t";
    

    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;

    }*/
    //cout << minD << endl;
    

    if(minD == 0) break; } cout << num << "/" << denom << endl; } return 0; }

    string ltrim(const string &str) { string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );
    
    return s;
    

    }

    string rtrim(const string &str) { string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );
    
    return s;
    

    }

    vector split(const string &str) { vector tokens;

    string::size_type start = 0;
    string::size_type end = 0;
    
    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));
    
        start = end + 1;
    }
    
    tokens.push_back(str.substr(start));
    
    return tokens;
    

    }

  • + 0 comments

    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);

    vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
    
    long min = stol(first_multiple_input[0]);
    
    long max = stol(first_multiple_input[1]);
    
    // Write your code here
    if(min > 0 && max > min)
    

    {
    long num, denom; double dist1, dist2, minD = PI; double n; //cout << PI << "\t" << to_string(PI) << endl;

    for(double  d  = min; d <= max; d++){
        n = 3 * d;
       // cout << n  << "\t" << d << "\t";
    

    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;

    }*/
    //cout << minD << endl;
    
    }
    cout << num << "/"
    << denom << endl;
    

    } return 0; }

  • + 0 comments

    minimal distance to pi code