Skip to content

Instantly share code, notes, and snippets.

@yovchev
Created August 11, 2020 12:35
Show Gist options
  • Save yovchev/81b0ae92d6306d6d10d4155415d75a10 to your computer and use it in GitHub Desktop.
Save yovchev/81b0ae92d6306d6d10d4155415d75a10 to your computer and use it in GitHub Desktop.
Haskel Primes with Infinite Data Structures
-- Computerphile Infinite Data Structures see https://youtu.be/bnRNiE_OVWA
-- Output the factors of n
factors n = [x | x <- [1..n], mod n x == 0]
-- Twin primes
twin (x,y) = y==x+2
-- Test n is prime number
prime n = factors n == [1,n] -- filter prime [1..]
-- Infinite primes list
primes = sieve [2..]
-- Infinite twin primes list see https://en.wikipedia.org/wiki/Twin_prime
twins = filter twin (zip primes (tail primes))
-- Sive algorithm to generate primes see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
sieve (p:ps) = p : sieve [x | x <- ps, mod x p /= 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment