Skip to content
TheChatArchiveThe Chat Archive
Metallama-3.3-70b-instruct· MetaHugging Face LMArena Human Preference 140K

Write a Python script that takes a number and finds the closest rational approximation…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-opus-4-20250514’s answer.
  1. User

    Write a Python script that takes a number and finds the closest rational approximation to the Nth digit.

  2. Meta Assistant

    Rational Approximation of Pi

    Below is a Python script that calculates the closest rational approximation to the Nth digit of Pi.

    from fractions import Fraction
    from decimal import Decimal, getcontext
    
    def calculate_pi(n):
        """
        Calculate Pi to n decimal places.
    
        Args:
            n (int): The number of decimal places.
    
        Returns:
            Decimal: Pi to n decimal places.
        """
        getcontext().prec = n + 1
        pi = Decimal(0)
        for k in range(n):
            pi += (Decimal(1) / (16 ** k)) * (
                (Decimal(4) / (8 * k + 1))
                - (Decimal(2) / (8 * k + 4))
                - (Decimal(1) / (8 * k + 5))
                - (Decimal(1) / (8 * k + 6))
            )
        return pi
    
    def closest_rational_approximation(pi, n):
        """
        Find the closest rational approximation to the Nth digit of Pi.
    
        Args:
            pi (Decimal): Pi to n decimal places.
            n (int): The Nth digit.
    
        Returns:
            Fraction: The closest rational approximation.
        """
        # Convert Pi to a string to access individual digits
        pi_str = str(pi)
        # Remove the decimal point
        pi_str = pi_str.replace('.', '')
        # Get the Nth digit
        nth_digit = int(pi_str[n])
        # Find the closest rational approximation
        closest_approx = Fraction(nth_digit, 10 ** (n - 1))
        return closest_approx
    
    def main():
        n = int(input("Enter the number of decimal places: "))
        pi = calculate_pi(n)
        closest_approx = closest_rational_approximation(pi, n)
        print(f"The closest rational approximation to the {n}th digit of Pi is: {closest_approx}")
    
    if __name__ == "__main__":
        main()
    

    Example Usage

    1. Run the script and enter the number of decimal places when prompted.
    2. The script will calculate Pi to the specified number of decimal places and find the closest rational approximation to the Nth digit.

    Note: This script uses the Bailey–Borwein–Plouffe formula to calculate Pi, which is a spigot algorithm that allows for the computation of any binary digit of Pi without having to compute any of the preceding digits. The decimal module is used to handle the high-precision arithmetic required for this calculation.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.