# This program computes an approximation of pi using the proof # of the basel problem. It requires many terms to calculate a # good approximation. # see: https://en.wikipedia.org/wiki/Basel_problem # The amount of terms terms := 1000000 # The running sum of terms tot := 0.0 n := 1 while n <= terms { tot = tot + 1.0/float(n*n) n = n + 1 } tot = tot * 6.0 # get the absolute value of a number fn abs(x: float) -> float { if x < 0.0 { -x } else { x } } # calculate an approximation of the square root of tot using # newton's method. # see: https://en.wikipedia.org/wiki/Newton's_method # The required accuracy SQRT_ACC := 0.00000001 fn sqrt(x: float) -> float { pg := 0.0 # previous guess g := 1.0 # current guess while abs(pg - g) >= SQRT_ACC { pg = g g = (pg + x/pg)/2.0 } return g } pi := sqrt(tot) # output the result println(pi)