Skip to content

Instantly share code, notes, and snippets.

@RichardBarrell
Created September 18, 2015 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBarrell/3e619d06d9b0088de116 to your computer and use it in GitHub Desktop.
Save RichardBarrell/3e619d06d9b0088de116 to your computer and use it in GitHub Desktop.
looks like a roughly even split between 1,3,7,9 up to the millionth prime. I got bored waiting for 10 million to finish.
module PrimeLastDigit where
-- compile with:
-- ghc --make PrimeLastDigit.hs -O2 -main-is PrimeLastDigit.main
import Data.List (foldl')
primes :: [Integer]
primes = 2 : [ c | c <- [3,5..], all (\p -> c `mod` p > 0) (takeWhile (\p -> p*p <= c) primes) ]
lastDigits = map (flip mod 10) primes
czero :: (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)
czero = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
count 0 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (1 + c0, c1, c2, c3, c4, c5, c6, c7, c8, c9)
count 1 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, 1 + c1, c2, c3, c4, c5, c6, c7, c8, c9)
count 2 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, 1 + c2, c3, c4, c5, c6, c7, c8, c9)
count 3 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, c2, 1 + c3, c4, c5, c6, c7, c8, c9)
count 4 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, c2, c3, 1 + c4, c5, c6, c7, c8, c9)
count 5 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, c2, c3, c4, 1 + c5, c6, c7, c8, c9)
count 6 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, c2, c3, c4, c5, 1 + c6, c7, c8, c9)
count 7 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, c2, c3, c4, c5, c6, 1 + c7, c8, c9)
count 8 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, c2, c3, c4, c5, c6, c7, 1 + c8, c9)
count 9 (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) = (c0, c1, c2, c3, c4, c5, c6, c7, c8, 1 + c9)
countLastDigitsUpTo n = foldl' (flip count) czero (take n lastDigits)
-- main loop does:
-- read an integer, N
-- count the last digits of the primes up to N
-- print the distribution of digits seen
-- back to start
main = interact (unlines . map (show . countLastDigitsUpTo . read) . lines)
RichardB@callisto ~$ ./PrimeLastDigit
1000
(0,245,1,253,0,1,0,254,0,246)
1000000
(0,249934,1,250110,0,1,0,250014,0,249940)
10000000
^C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment