TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Another slice of π ? (generating the digits of π in Common Lisp & Haskell)

3 pointsby jgrant27almost 16 years ago

1 comment

flambardalmost 16 years ago
This is kind of situation where LOOP really shines.<p><pre><code> (defun machin-pi (digits) "Accurately calculates PI digits using Machin's formula with fixed point arithmetic and variable digit guarding." (flet ((arccot (x unity) (loop with xsq = (* x x) for positive = t then (not positive) for n upfrom 1 by 2 for xpow = (floor (/ unity x)) then (floor xpow xsq) for term = (floor xpow n) until (= 0 term) sum (if positive term (- term))))) (when (&#62; digits 0) (let* ((guard (floor (* 10 (log digits 10)))) (unity (expt 10 (+ digits guard))) (thispi (* 4 (- (* 4 (arccot 5 unity)) (arccot 239 unity))))) (floor thispi (expt 10 guard))))))</code></pre>