You are viewing a single comment's thread. Return to all comments →
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)
Seems like cookies are disabled on this browser, please enable them to open this website
Minimal Distance to Pi
You are viewing a single comment's thread. Return to all comments →