Created
November 24, 2020 10:17
-
-
Save rbricheno/87ded3ac9a2dd70343aebc9bcc479384 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from turtle import * | |
DIGITS = 1000 | |
def pi_digits(x): | |
"""Generate x digits of Pi. | |
Nicked from https://stackoverflow.com/a/54967370/9794932""" | |
k,a,b,a1,b1 = 2,4,1,12,4 | |
while x > 0: | |
p,q,k = k * k, 2 * k + 1, k + 1 | |
a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1 | |
d,d1 = a/b, a1/b1 | |
while d == d1 and x > 0: | |
yield int(d) | |
x -= 1 | |
a,a1 = 10*(a % b), 10*(a1 % b1) | |
d,d1 = a/b, a1/b1 | |
digits = [str(n) for n in list(pi_digits(DIGITS))] | |
color('red') | |
just_turned = False | |
for digit in digits: | |
digit = int(digit) | |
if not just_turned: | |
left(digit * 36) | |
just_turned = True | |
else: | |
forward(digit * 5) | |
just_turned = False | |
done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment