Files
pi-integral/main.ua
2025-09-21 18:03:16 +02:00

26 lines
629 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# approximate pi by integrating 1/(1+x²) from 0 to 1,
# doing so in parallel with threading for a speed-up.
f ← ˜÷1+1ⁿ2
# create interval [a, b) with n values
# interval ? a b n
ι ← ⍜-×⊙⊙(÷⟜⇡)
# integrate f on given interval
# integral ? f a b n
i! ← ÷⊃⧻/+ ^0 ι
# time a unary function
# ? f
t! ← ⊙(&p" ms"&pf×1e3⌵-)⊙now^0⊙now
# partition into n subintervals
# [subintervals a_b] ? a b n
p ← ⊂⟜(\+↯⤙(˜÷⌵-))
p 0 1 4 # n=4 threads
⊃(&p $ sequential:
t!(⧈₂/(i!f⊙⊙1e7))
| &p $ parallel:
t!(wait⧈₂/pool(i!f⊙⊙1e7))
)
&p $ error:
&p ⌵-π×4÷⊃⧻/+ ◌