shnarazk / leibniz-formula-benchmark Goto Github PK
View Code? Open in Web Editor NEWA small benchmark program using Leibniz formula
A small benchmark program using Leibniz formula
import std/num/float64
pub fun main()
println("The " ++ (4.0 * pi(0, 0.0)).show)
fun pi(n: int, sum : float64): pure float64
if 100_000_000 < n
then sum
else
val n4 = n.float64 * 4.0;
pi(n + 1, sum + 2.0 / ((n4 + 1.0) * (n4 + 3.0)))
nix$ time .koka/v3.0.4/cc-drelease/pi
The 3.1415926445762157
.koka/v3.0.4/cc-drelease/pi 0.08s user 0.00s system 96% cpu 0.089 total
PiFinder class>>#runTo: pairs
| denominator currSum tmp |
denominator := -1.
currSum := 0.0.
pairs timesRepeat: [
denominator := denominator + 4.
tmp := denominator asFloat.
currSum := currSum + (2.0 / (tmp * (tmp - 2.0))) ].
^ 4.0 * currSum
USING: kernel math ;
IN: leibniz
: lpi ( num-of-pairs: integer -- pi )
0.0 0 rot
[ [ 4.0 * [ 3.0 + ] [ 1.0 + ] bi * 2.0 swap / + ] keep 1 + ] times
drop 4 *
;
USING: kernel math ;
IN: Leibniz
: calcpi ( -- pi index )
0.0 0
[
dup [ 4.0 * [ 3.0 + ] [ 1.0 + ] bi * 2.0 swap / + ] dip
1 +
dup 100000000 <
] loop
[ 4 * ] dip
! drop
;
100M pairs => 0.137 sec. on 2.3GHz dual core Core i5 😲
def leibniz (n : Nat) (sum : Float) : Float :=
match n with
| 0 => sum + 8.0 / 3.0
| succ n' => let k := (n * 4).toFloat; leibniz n' (sum + 8.0 / ((k + 1.0) * (k + 3.0)))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.