Calculate PI Digits with P5.JS / JavaScript - Looking for maximum efficiency
Budget: $20 – $30 USD
Hi,
I am working on a project in P5.JS /JavaScript and I need the ability to calculate PI digits starting at the beginning and going for as long as possible.
Currently, I am using the Spigot Algorithm that can be found here:
https://rosettacode.org/wiki/Pi#JavaScript
This code works but after a day of it running non-stop it begins to slow down because it takes more and more CPU calculations to generate the next digit of PI(I believe), and this in turn slows the P5.JS program that is using the digits of PI. I believe BigInt is part of the reason it gets slower as time goes on.
So, I'm looking for a more efficient way to generator each digit of PI and I've heard there are two major ways to do this: one is CPU intensive and the second is more memory intensive? If this is true, I would like a method that is more memory intensive because the P5.JS program needs CPU power to run well.
Is there a memory intensive way to generate PI? Is it as good as or better than the Spigot Algorithm
If not, is there a better way to generate PI other than the Spigot Algorithm?
If so will this method run longer, before there is noticeable speed loss?
I want to be able to generate PI for as long as possible with as little CPU loss a possible.
Here is the P5.JS code I have that uses the Spigot Algorithm:
let piBuffer;
function setup() {
createCanvas(400, 400);
piBuffer = new PiBuffer();
piBuffer.calculatePi(0);
frameRate(30);
}
function draw() {
print('NextPiDigit = ' + piBuffer.getNextPiDigit());
}
class PiBuffer {
constructor() {
this.q = BigInt(1);
this.r = BigInt(180);
this.t = BigInt(60);
this.i = BigInt(2);
// y is the previous digit
this.y
this.u
this.piBufferCharArry = [];
}
// Caluculate the next numDigits of Pi
async calculatePi(loopDelay) {
while(true){
await new Promise(resolve => {
setTimeout(() => {
resolve(true);
this.piBufferCharArry.push(this.calculateNextPiDigit());
}, loopDelay);
});
}
}
calculateNextPiDigit() {
// Spigot Method of calculating Pi
this.y = (this.q*(BigInt(27)*this.i-BigInt(12))+BigInt(5)*this.r)/(BigInt(5)*this.t);
this.u = BigInt(3)*(BigInt(3)*this.i+BigInt(1))*(BigInt(3)*this.i+BigInt(2));
this.r = BigInt(10)*this.u*(this.q*(BigInt(5)*this.i-BigInt(2))+this.r-this.y*this.t);
this.q = BigInt(10)*this.q*this.i*(BigInt(2)*this.i-BigInt(1));
this.t = this.t*this.u;
this.i = this.i+BigInt(1);
if (this.i % BigInt(1000) === BigInt(0)){
print("Current Digit=" + this.i);
}
return this.y
}
getNextPiDigit() {
return this.piBufferCharArry.shift();
}
}
I am working on a project in P5.JS /JavaScript and I need the ability to calculate PI digits starting at the beginning and going for as long as possible.
Currently, I am using the Spigot Algorithm that can be found here:
https://rosettacode.org/wiki/Pi#JavaScript
This code works but after a day of it running non-stop it begins to slow down because it takes more and more CPU calculations to generate the next digit of PI(I believe), and this in turn slows the P5.JS program that is using the digits of PI. I believe BigInt is part of the reason it gets slower as time goes on.
So, I'm looking for a more efficient way to generator each digit of PI and I've heard there are two major ways to do this: one is CPU intensive and the second is more memory intensive? If this is true, I would like a method that is more memory intensive because the P5.JS program needs CPU power to run well.
Is there a memory intensive way to generate PI? Is it as good as or better than the Spigot Algorithm
If not, is there a better way to generate PI other than the Spigot Algorithm?
If so will this method run longer, before there is noticeable speed loss?
I want to be able to generate PI for as long as possible with as little CPU loss a possible.
Here is the P5.JS code I have that uses the Spigot Algorithm:
let piBuffer;
function setup() {
createCanvas(400, 400);
piBuffer = new PiBuffer();
piBuffer.calculatePi(0);
frameRate(30);
}
function draw() {
print('NextPiDigit = ' + piBuffer.getNextPiDigit());
}
class PiBuffer {
constructor() {
this.q = BigInt(1);
this.r = BigInt(180);
this.t = BigInt(60);
this.i = BigInt(2);
// y is the previous digit
this.y
this.u
this.piBufferCharArry = [];
}
// Caluculate the next numDigits of Pi
async calculatePi(loopDelay) {
while(true){
await new Promise(resolve => {
setTimeout(() => {
resolve(true);
this.piBufferCharArry.push(this.calculateNextPiDigit());
}, loopDelay);
});
}
}
calculateNextPiDigit() {
// Spigot Method of calculating Pi
this.y = (this.q*(BigInt(27)*this.i-BigInt(12))+BigInt(5)*this.r)/(BigInt(5)*this.t);
this.u = BigInt(3)*(BigInt(3)*this.i+BigInt(1))*(BigInt(3)*this.i+BigInt(2));
this.r = BigInt(10)*this.u*(this.q*(BigInt(5)*this.i-BigInt(2))+this.r-this.y*this.t);
this.q = BigInt(10)*this.q*this.i*(BigInt(2)*this.i-BigInt(1));
this.t = this.t*this.u;
this.i = this.i+BigInt(1);
if (this.i % BigInt(1000) === BigInt(0)){
print("Current Digit=" + this.i);
}
return this.y
}
getNextPiDigit() {
return this.piBufferCharArry.shift();
}
}