ok Any Forums, there's been enough fizzbuzz threads, let's step up your leetcode slightly and calculate pi
Ok Any Forums, there's been enough fizzbuzz threads, let's step up your leetcode slightly and calculate pi
pi = 3.14
(defun get-pi(acc)
(let ((npi 0))
(loop for i from 2 to acc by 4 do (setq npi (+ npi (coerce (/ 4 (* i (+ i 1) (+ i 2))) 'long-float))))
(loop for i from 4 to acc by 4 do (setq npi (- npi (coerce (/ 4 (* i (+ i 1) (+ i 2))) 'long-float))))
(+ 3 npi)))
Here's a quick bit in Lisp, for some inspiration
var pi = System.Math.PI;
# formula is 3+ 4/(2x3x4) - 4/(4x5x6) + 4/(6x7x8) - .....
three = 3
#input is how many times it does the thingy above
times = input("Until how many UwU? ")
switch = 0
counter = 2
while switch < int(times): # this makes every other line a + or a -
if int(switch/2) - float(switch/2) == 0: sign = "+"
else: sign = "-"
# test if this works print(sign)
if sign == "+": # if it is a + do math with +
three = three + 4/((counter)*(counter+1)*(counter+2))
if sign == "-": #does the math with -
three = three - 4/((counter)*(counter+1)*(counter+2))
print(three) # this 3 should be the 3 + stuff
counter = counter + 2
switch = switch + 1
# owo
>three = 3
kek
Pi~N~500
That was hard.
>getting filtered by code one level above fizzbuzz
You forgot to declare it as an int so it gets rounded down to 3
>calculate pi
not possible
print(22/7)
pi = Math.PI + 0
haven't slept in three days and i dont wanna code anymore
but essentially what you wanna do is create a function that does this:
π=4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...
and just add more depending on how accurate you'd want it to be
That obviously approximates to
π≈3,1416...
midwits need not apply
const pi = 3.14;
import std;
struct PiGen
{
int i;
double front()
{
if(i == 0) return 3;
double d = i * 2.0;
int sign = i % 2 == 0 ? -1 : 1;
return sign * 4.0 / (d * (d + 1) * (d + 2));
}
void popFront() { i++; }
enum empty = false;
}
void main()
{
PiGen().take(100).sum.writeln;
}
any way to improve this function? I think i optimized it as-well as possible.
>they don't know the chudnovsky algorithm
from decimal import Decimal, getcontext
from math import ceil, factorial
def pi(precision):
if not isinstance(precision, int):
raise TypeError("Undefined for non-integers")
elif precision < 1:
raise ValueError("Undefined for non-natural numbers")
getcontext().prec = precision
num_iterations = ceil(precision / 14)
constant_term = 426880 * Decimal(10005).sqrt()
exponential_term = 1
linear_term = 13591409
partial_sum = Decimal(linear_term)
for k in range(1, num_iterations):
multinomial_term = factorial(6 * k) // (factorial(3 * k) * factorial(k) ** 3)
linear_term += 545140134
exponential_term *= -262537412640768000
partial_sum += Decimal(multinomial_term * linear_term) / exponential_term
return str(constant_term / partial_sum)[:-1]
Winner is the one who gets the most decimals.
function nilakantha_pi(n) {
let x = 2;
let pi = 3;
for (let i = 0; i < n; i++) {
if (i % 2 === 0) {
pi += 4 / (x * (x + 1) * (x + 2));
} else {
pi -= 4 / (x * (x + 1) * (x + 2));
}
x += 2;
}
return pi;
}
>tfw filtered by chud's algorithm
My shitty attempt.