Logic.hs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. -- Calculate and store values of Pi.
  2. module Logic (hexDigits) where
  3. import Control.Parallel (par)
  4. -- Get the hex digit of Pi at place n.
  5. hexPi :: Integer -> Integer
  6. hexPi n =
  7. let
  8. summation = let
  9. sone = (4 * sumPi n 1)
  10. sfour = (2 * sumPi n 4)
  11. sfive = (sumPi n 5)
  12. ssix = (sumPi n 6)
  13. in
  14. sone `par` sfour `par` sfive `par` ssix `par` sone - sfour - sfive - ssix
  15. skimmedSum = summation - (fromIntegral (floor summation :: Integer)) -- Take only the decimal portion
  16. in
  17. floor (16 * skimmedSum) :: Integer
  18. -- Calculate the summation.
  19. -- 5000 is used in place of Infinity. This value drops off quickly, so no need to go too far.
  20. sumPi :: Integer -> Integer -> Double
  21. sumPi n x =
  22. let
  23. summation1 = sum [(fromIntegral ((16^(n-k) `mod` ((8*k)+x)))) / (fromIntegral ((8*k)+x)) | k <- [0..n]]
  24. summation2 = sum [16^^(n-k) / (fromIntegral ((8*k)+x)) | k <- [(n+1)..5000]]
  25. in
  26. summation1 + summation2
  27. -- The list of answers.
  28. hexDigits :: [Integer]
  29. hexDigits = [hexPi x | x <- [0..]]