Apple or coconut π?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-- A solution to the π-problem posted at http://lpaste.net/108485

type Radius = Int
type Π = Int

π :: Radius -> Π -- this is the updated approach.
π r = let domain = [negate r .. r]
      in length [(x, y) | x <- domain, y <- domain, within r (x, y)]
  
distance :: (Int, Int) -> Int
distance = floor . sqrt . fromInteger . squares

squares :: (Int, Int) -> Integer
squares (x, y) = toInteger (x * x + y * y)

within :: Int -> (Int, Int) -> Bool
within r (x, y) = distance (x, y) <= r

-- what (scaled) values of π do you come up with?

{--

*Main> π 1000
3147821
*Main> import Data.Time.Clock
*Main Data.Time.Clock> getCurrentTime >>= \start -> print (π 10000) >>
                       getCurrentTime >>= return . (flip diffUTCTime start)
314221637
566.529064s

Fun π-facts: 22/7 is bleh! Chuck Moore, inventor of the Forth programming
language (for the purpose of guiding telescopes) developed 355 / 113 which
was a close enough approximation for π to guide the telescopes right to
the viewed stellar object.

π is now known to zillions of digits, Fabrice Bellard calculated π to almost
three TRILLION digits on a home computer. That's sick!

 --}