Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » I need to generate 30 decimals of pi per second - what should I do?
- sumsarasmus
-
Scratcher
20 posts
I need to generate 30 decimals of pi per second - what should I do?
My latest project is based on pi, but I only have 44002 decimals. What should I do to generate more decimals?
- 9to0
-
Scratcher
92 posts
I need to generate 30 decimals of pi per second - what should I do?
look up how to calculate pi and find a method that is infinite
- floppasyay
-
Scratcher
1000+ posts
I need to generate 30 decimals of pi per second - what should I do?
(#2)You can't calculate “pi” accurately, it has no repeating pattern to it and can't be represented as a decimal.
look up how to calculate pi and find a method that is infinite
The closest decimal you can get to pi is 22/7 but yet that is still an overestimate.
- scratchcode1_2_3
-
Scratcher
1000+ posts
I need to generate 30 decimals of pi per second - what should I do?
Step 1. Buy a supercomputer with at least 10000 cores
Jokes aside, you’d need a way to implement bigint in scratch and find a way to store those numbers without lagging out the ram. It would only take 40 kb of ram but it will be laggy. Since scratch uses javascript’s floating point precision, you’d need to implement arbitrary precision arithmetic
Jokes aside, you’d need a way to implement bigint in scratch and find a way to store those numbers without lagging out the ram. It would only take 40 kb of ram but it will be laggy. Since scratch uses javascript’s floating point precision, you’d need to implement arbitrary precision arithmetic
Last edited by scratchcode1_2_3 (March 5, 2026 01:37:06)
- Geotale
-
Scratcher
100+ posts
I need to generate 30 decimals of pi per second - what should I do?
(#2)You can't calculate “pi” accurately, it has no repeating pattern to it and can't be represented as a decimal.
look up how to calculate pi and find a method that is infinite
The closest decimal you can get to pi is 22/7 but yet that is still an overestimate.
You can calculate pi as close you want with decimals or fractions, but you can't compute it exactly due to it being irrational!
The first few fractions which most closely approximate pi go as follows:
3 / 1 = 3.0 (1 digit)
13 / 4 = 3.25 (1 digit)
16 / 5 = 3.2 (1 digit)
19 / 6 = 3.16 (2 digits)
22 / 7 = 3.142857 (3 digits)
179 / 57 = 3.140350877192982456 (3 digits)
201 / 64 = 3.140625 (3 digits)
223 / 71 = 3.14084507042253521126760563380281690 (3 digits)
245 / 78 = 3.1410256 (4 digits)
267 / 85 = 3.14117647058823529 (4 digits)
289 / 92 = 3.141304347826086956521739 (4 digits)
311 / 99 = 3.14 (4 digits)
333 / 106 = 3.14150943396226 (5 digits)
355 / 113 = 3.1415929203539823… (7 digits)
52163 / 16604 = 3.1415923873765357… (7 digits)
52873 / 16830 = 3.14159239453357100 (7 digits)
… (skipped)
86953 / 27678 = 3.1415926006214323… (8 digits)
… (skipped)
102928 / 32763 = 3.141592650245704… (9 digits)
103283 / 32876 = 3.1415926511741086… (9 digits)
103638 / 32989 = 3.1415926520961532… (9 digits)
103993 / 33102 = 3.1415926530119025… (10 digits)
…
In fact, because Scratch doesn't have infinite precision, there is actually a fraction which it believes to be exactly equal to pi! The simplest of which being 245850922 / 78256779
This is to say, Scratch believes:
<((245850922) / (78256779)) = [3.1415926535897932384626433]>
- ispretty
-
Scratcher
500+ posts
I need to generate 30 decimals of pi per second - what should I do?
If you want a lot of decimal places, just copy a million digits off of a website and you should be all set for most projects. If you actually do need to generate them on the go, you should use an existing algorithm like the Chudnovsky Algorithm to calculate digits of pi alongside an algorithm that can handle calculations with large numbers (How to Use Large Numbers in Scratch) so that floating-point precision isn't an issue.
- 52525rr
-
Scratcher
17 posts
I need to generate 30 decimals of pi per second - what should I do?
If you want a lot of decimal places, just copy a million digits off of a website and you should be all set for most projects. If you actually do need to generate them on the go, you should use an existing algorithm like the Chudnovsky Algorithm to calculate digits of pi alongside an algorithm that can handle calculations with large numbers (How to Use Large Numbers in Scratch) so that floating-point precision isn't an issue.this is what i have done before actually. in particular, i've made this project which computes a handful of math constants to very high precision. https://scratch.mit.edu/projects/926652006/
- kpoovakan
-
Scratcher
100+ posts
I need to generate 30 decimals of pi per second - what should I do?
Do you need to actually generate thirty digits of pi per second? Couldn't you copy a million digits of pi from a website online and use that?
- ajskateboarder
-
Scratcher
1000+ posts
I need to generate 30 decimals of pi per second - what should I do?
Do you need to actually generate thirty digits of pi per second? Couldn't you copy a million digits of pi from a website online and use that?That takes a ~megabyte of space in the project and is also quite boring tbh
Last edited by ajskateboarder (March 7, 2026 03:35:39)
- MineTurte
-
Scratcher
1000+ posts
I need to generate 30 decimals of pi per second - what should I do?
My latest project is based on pi, but I only have 44002 decimals. What should I do to generate more decimals?sigh i guess no one can provide any good answers… well time to pull out something I literally posted in AT relating to this exact thing… BAM.. Now because it's written in Python it certainly isn't the fastest but it's a LOT faster than 30 digits per second (around 500 digits per second on average). Enjoy.
Also if you actually need more digits you can get them via the script.
Last edited by MineTurte (March 7, 2026 05:56:13)
- sumsarasmus
-
Scratcher
20 posts
I need to generate 30 decimals of pi per second - what should I do?
And if it's in python, I can just use scratchattach, right?My latest project is based on pi, but I only have 44002 decimals. What should I do to generate more decimals?sigh i guess no one can provide any good answers… well time to pull out something I literally posted in AT relating to this exact thing… BAM.. Now because it's written in Python it certainly isn't the fastest but it's a LOT faster than 30 digits per second (around 500 digits per second on average). Enjoy.
Also if you actually need more digits you can get them via the script.
- MineTurte
-
Scratcher
1000+ posts
I need to generate 30 decimals of pi per second - what should I do?
Theoretically, yes actually. However trying to update a cloud variable that fast to actually get up to 500 digits per second won't work. Assuming you can only do 256 in say 1 second or more. But if all you need is something like 30 digits per second, it could work. You'll just have to modify some of the code for it to actually output correctly. Also if you plan to do this I recommend removing the extra “find” stuff that is just there for fun.And if it's in python, I can just use scratchattach, right?My latest project is based on pi, but I only have 44002 decimals. What should I do to generate more decimals?sigh i guess no one can provide any good answers… well time to pull out something I literally posted in AT relating to this exact thing… BAM.. Now because it's written in Python it certainly isn't the fastest but it's a LOT faster than 30 digits per second (around 500 digits per second on average). Enjoy.
Also if you actually need more digits you can get them via the script.
Well because I'm not lazy (lies) I'll do it for ya
here's the clean version with optional file writing so you don't lose anything calculated. Also I will warn you it generates the digits all at once so you can't break it up unfortunately.
WHAT DO YOU MEAN THIS POST CONTAINS UNSUITABLE LANGUAGE
I KID YOU NOT IT IS SAYING THAT code=py IS UNSUITABLE LANGUAGE… OH MY GOD
from math import factorial
from decimal import Decimal, getcontext
import psutil
import json
import time
import os # Note: If your terminal doesn't use 'clear' just swap it with 'cls' or whatever else your terminal uses.
# Get the requested amount of digits
os.system('clear')
digits = int(input("Enter the number of digits you'd like to calculate: "))
os.system('clear')
prev = psutil.virtual_memory().percent
# Initiates timer
start_time = time.time()
psutil.cpu_percent(interval=None)
global tot
tot = 0
def calc(n):
### Main calculations. Uses the algorithm.
getcontext().prec=digits ### Sets the precision of the decimal module
t= Decimal(0)
pi = Decimal(0)
deno= Decimal(0)
k = 0
print("Doing some quick calculations... This should go very quickly (usually appears instant)\n")
for k in range(n):
t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
pi += Decimal(t)/Decimal(deno)
print("Done!\n\nFinishing up... This is a massive calculation, it may take a while")
# Split up the massive calculation into smaller parts to prevent lagspikes and provide % completion.
# pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5)) is the full calculation.
p1 = Decimal(1.5)
print("25%")
time.sleep(0.3)
p2 = Decimal(640320**p1)
print("50%")
p3 = Decimal(12)/p2
global tot
tot = psutil.cpu_percent(interval=None)
print("75%")
pi = pi * p3
print("85%")
pi = 1/pi
print("100%")
return pi
f_pi = str(calc(25))
os.system('clear')
print("\033[31m") # Just for coloring. That's all.
for i in f_pi:
print(i, end="", flush=True) # Prints the digits one by one. This is to prevent the massive lagspike that can occur when printing the whole thing at once.
# Output information from the calculations (the actual digits are printed above this)
print("\n\n\033[36m-------------------------------------------------------------")
elapsed = round(time.time() - start_time, 2)
print(f"Elapsed Time: \033[94;1m{elapsed} seconds\033[0m\033[36m")
print(f"Total CPU Usage: \033[94;1m{tot}%\033[0m\033[36m")
print(f"Average Digits Per Second: \033[94;1m{round(digits/(time.time() - start_time), 1)} DPS\033[0m\033[36m")
print("-------------------------------------------------------------\033[0m")
inp = input("Where do you want to store this in?")
if inp != "" and inp != " ":
with open(inp, 'w') as f:
json.dump(f_pi, f, indent=4)
Last edited by MineTurte (March 7, 2026 14:08:53)
- sumsarasmus
-
Scratcher
20 posts
I need to generate 30 decimals of pi per second - what should I do?
The problem is… Well… I've never used python and I can only do simple things such as “print” or “if”… I just pasted one million decimals, so that will probably last for a bitTheoretically, yes actually. However trying to update a cloud variable that fast to actually get up to 500 digits per second won't work. Assuming you can only do 256 in say 1 second or more. But if all you need is something like 30 digits per second, it could work. You'll just have to modify some of the code for it to actually output correctly. Also if you plan to do this I recommend removing the extra “find” stuff that is just there for fun.And if it's in python, I can just use scratchattach, right?My latest project is based on pi, but I only have 44002 decimals. What should I do to generate more decimals?sigh i guess no one can provide any good answers… well time to pull out something I literally posted in AT relating to this exact thing… BAM.. Now because it's written in Python it certainly isn't the fastest but it's a LOT faster than 30 digits per second (around 500 digits per second on average). Enjoy.
Also if you actually need more digits you can get them via the script.
Well because I'm not lazy (lies) I'll do it for ya
here's the clean version with optional file writing so you don't lose anything calculated. Also I will warn you it generates the digits all at once so you can't break it up unfortunately.
WHAT DO YOU MEAN THIS POST CONTAINS UNSUITABLE LANGUAGE
I KID YOU NOT IT IS SAYING THAT code=py IS UNSUITABLE LANGUAGE… OH MY GODfrom math import factorial
from decimal import Decimal, getcontext
import psutil
import json
import time
import os # Note: If your terminal doesn't use 'clear' just swap it with 'cls' or whatever else your terminal uses.
# Get the requested amount of digits
os.system('clear')
digits = int(input("Enter the number of digits you'd like to calculate: "))
os.system('clear')
prev = psutil.virtual_memory().percent
# Initiates timer
start_time = time.time()
psutil.cpu_percent(interval=None)
global tot
tot = 0
def calc(n):
### Main calculations. Uses the algorithm.
getcontext().prec=digits ### Sets the precision of the decimal module
t= Decimal(0)
pi = Decimal(0)
deno= Decimal(0)
k = 0
print("Doing some quick calculations... This should go very quickly (usually appears instant)\n")
for k in range(n):
t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
pi += Decimal(t)/Decimal(deno)
print("Done!\n\nFinishing up... This is a massive calculation, it may take a while")
# Split up the massive calculation into smaller parts to prevent lagspikes and provide % completion.
# pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5)) is the full calculation.
p1 = Decimal(1.5)
print("25%")
time.sleep(0.3)
p2 = Decimal(640320**p1)
print("50%")
p3 = Decimal(12)/p2
global tot
tot = psutil.cpu_percent(interval=None)
print("75%")
pi = pi * p3
print("85%")
pi = 1/pi
print("100%")
return pi
f_pi = str(calc(25))
os.system('clear')
print("\033[31m") # Just for coloring. That's all.
for i in f_pi:
print(i, end="", flush=True) # Prints the digits one by one. This is to prevent the massive lagspike that can occur when printing the whole thing at once.
# Output information from the calculations (the actual digits are printed above this)
print("\n\n\033[36m-------------------------------------------------------------")
elapsed = round(time.time() - start_time, 2)
print(f"Elapsed Time: \033[94;1m{elapsed} seconds\033[0m\033[36m")
print(f"Total CPU Usage: \033[94;1m{tot}%\033[0m\033[36m")
print(f"Average Digits Per Second: \033[94;1m{round(digits/(time.time() - start_time), 1)} DPS\033[0m\033[36m")
print("-------------------------------------------------------------\033[0m")
inp = input("Where do you want to store this in?")
if inp != "" and inp != " ":
with open(inp, 'w') as f:
json.dump(f_pi, f, indent=4)
- MineTurte
-
Scratcher
1000+ posts
I need to generate 30 decimals of pi per second - what should I do?
The problem is… Well… I've never used python and I can only do simple things such as “print” or “if”… I just pasted one million decimals, so that will probably last for a bitAh alright. And yeah, 1 million is bigger than most people realize.
- sumsarasmus
-
Scratcher
20 posts
I need to generate 30 decimals of pi per second - what should I do?
Yeah, I think that will last a while, but thanks for helping me!The problem is… Well… I've never used python and I can only do simple things such as “print” or “if”… I just pasted one million decimals, so that will probably last for a bitAh alright. And yeah, 1 million is bigger than most people realize.
- Discussion Forums
- » Advanced Topics
-
» I need to generate 30 decimals of pi per second - what should I do?