🦁

円周率

2020/12/19に公開

やあ、みんな!チョーさんだよ!!(あいさつ)

過去のfriends.nicoの投稿からサルベージ。

整数平方根

isqrt n = length (takeWhile (<= n) squares) 
  where odds = 1 : map (+ 2) odds; 
    squares = 1 : zipWith (+) (tail odds) squares

> isqrt 500000000000000
22360679

円周率

pi = (head k) : zipWith (+) (takeWhile ((> 0) . abs) (zipWith quot (tail k) (tail odds))) pi 
  where odds = 1 : map (+ 2) odds; 
    k = ((isqrt 3000000000000) * 2) : map (\x -> quot (- x) 3) k

> pi
[3464100,3079200,3156180,3137852,3142603,3141308,3141673,3141568,3141599,3141590,3141592]

Discussion