@ChrisCheng/

PIthon (By @ebest)

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import datetime
from json_store_client import Client
import os
from os.path import join, dirname
from dotenv import load_dotenv

#dotenv_path = join(dirname(__file__), '.env')
#load_dotenv(dotenv_path)
#come on
SECRET_KEY = "2a92320f57f6bfffabd91fea9f7b788d290e6d1cd274ce6c16237ac3df1c4bd3"
#      Functions
def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]
def make_pi(amount, info):
	q, r, t, k, m, x, digit = info[0], info[1], info[2], info[3], info[4], info[5], 0
	while digit <= amount:
		if 4 * q + r - t < m * t:
			digit += 1
			yield m, [q, r, t, k, m, x]
			q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
		else:
			q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2
def rpi():
	length = client.retrieve("alen")
	far = []
	for i in range(0, length):
		nar = client.retrieve("pi" + str(i + 1))
		if nar is None:
			nar = []
		far = far + nar
	return far
def spi(ar):
	r = 1
	for i in chunks(ar, 20):
		client.store("pi" + str(r), i)
		r += 1
	client.store("alen", r)
def rin():
  q, r, t, k, m, x = client.retrieve("q"), client.retrieve("r"), client.retrieve("t"), client.retrieve("k"), client.retrieve("m"), client.retrieve("x")
  return [q, r, t, k, m, x]
def sin(info):
  q, r, t, k, m, x = info[0], info[1], info[2], info[3], info[4], info[5]
  client.store("q", q)
  client.store("r", r)
  client.store("t", t)
  client.store("k", k)
  client.store("m", m)
  client.store("x", x)
def cleara():
	length = client.retrieve("alen")
	if length is None:
		length = 10
	for i in range(0, length):
		client.store("pi" + str(i), [])
	client.store("runs", 0)
	client.store("names", {})
	sin([1, 0, 1, 1, 3, 3])
	client.store("alen", 1)
	input("ok")
def ginfo():
	runs = client.retrieve("runs")
	names = client.retrieve("names")
	info = client.retrieve("info")
	pi = rpi()
	print(str(runs) + " runs")
	print(str(names) + " people")
	print(str(info) + " info")
	print(str(pi) + " pi")
# Setup
client = Client(SECRET_KEY)
info = client.retrieve("info")
#cleara()
#ginfo()
my_array = []
digit = 0
runs = client.retrieve("runs")
client.store("runs", runs + 1)
names = client.retrieve("names")
# Start interface
print("""PIthon: Crowdsourced Pi generation using Python \n
PIthon has been run """ + str(runs) + " times.")
name = input("What's your username (You'll be seen with this name)? ")
print("\033[A                                                                   \033[A")
lens = int(input("How many digits do you want to \"contribute\" (Please, at least 20)?\n"))
print("\033[A                                                                   \033[A")
print("\033[A                                                                   \033[A")
if name in names.keys():
	print("Hello again! You're doing a great servise to PI by doing this!\n\n")
	names[name] += lens
else:
	names[name] = lens
client.store("names", names)
#Calculations
info = rin()
pi = rpi()
r = 1
for pidn, v in make_pi(lens, info):
	pi.append(str(pidn))
	if r == 1:
		print("\033[A                                                                         \033[A")
		print("Processing... This may take a while, so you can go do something else.")
	if r == 11:
		print("\033[A                                                                         \033[A")
		print("Processing. This may take a while, so you can go do something else.")
	if r == 21:
		print("\033[A                                                                         \033[A")
		print("Processing.. This may take a while, so you can go do something else.")
	if r == 31:
		print("\033[A                                                                         \033[A")
		print("Processing... This may take a while, so you can go do something else.")
	if r == 41:
		print("\033[A                                                                         \033[A")
		print("Processing. This may take a while, so you can go do something else.")
	if r == 51:
		print("\033[A                                                                         \033[A")
		print("Processing.. This may take a while, so you can go do something else..")
		sin(info)
		spi(pi)
		r = 0
	r += 1
	digit += 1
print("\033[A                                                                   \033[A")
names = client.retrieve("names")
print("Processing complete. Currently, PIthon has " + str(len(pi)) + " digits calculated.\n")
ds = input("How many digits do you want to view? (all for all) ")
print("\033[A                                                                   \033[A")
while (not ds.isnumeric()) and ds.lower() != "all":
	ds = input("That is not valid. Please try again. ")
if ds.lower() == "all":
	print("3." + "".join(rpi()[1:]))
elif int(ds) <= len(pi):
	print("3." + "".join(rpi()[1:int(ds)]))
else:
	print("We don't have that many digits! We're " + str(pi - int(ds)) + " digits away. So we'll show all the digits we have so far...")
	print("3." + "".join(rpi())[1:])
ynun = input("Do you want to see who's been here (and how much they've served the community)? ")
if ynun.lower() == "yes":
	names = client.retrieve("names")
	for user in names.keys():
		if not names[user] > len(pi):
			print(user + " has added " + str(names[user]) + " digits.")
	print("Bye!")
else:
	print("Well then, bye!")