mardi 18 juillet 2017

Python value of pi represented in a 10 digit/10 digit fraction

So I was assigned this function to generate a random number and find its numerator so that the function could be APPROXIMATELY equal to pi. I run this and it draws to a blank im sure its because i defined the numerator to be exactly the value of pi, but how do i bypass it to achieve the function approximately equal to pi. I may have made some more mistakes im kinda new to this.

import random
import math


def pi_frac():
    while 1:
        denominator = [1,2,3,4,5,6,7,8,9,0]
        random.shuffle(denominator)
        denominator_int = int("".join(map(str, denominator)))
        numerator = abs(denominator_int * abs(math.pi))
        if len(str(numerator)) == 10:
            print denominator_int
            print numerator
        else:
            pi_frac()


pi_frac()




Aucun commentaire:

Enregistrer un commentaire