-
Notifications
You must be signed in to change notification settings - Fork 1
/
p46.idr
54 lines (39 loc) · 1.32 KB
/
p46.idr
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Test first 10000 numbers (empirically determined)
import Data.Nat
divides : Nat -> Nat -> Bool
divides n d = (mod n d) == 0
anyDivides : Nat -> List Nat -> Bool
anyDivides n [] = False
anyDivides n (m::ms) = (divides n m) || (anyDivides n ms)
truncatedSquareRoot : Nat -> Nat
truncatedSquareRoot n = cast (sqrt (cast n))
isPrime : Nat -> Bool
isPrime 1 = False
isPrime 2 = True
isPrime n = not (anyDivides n [2..(max 2(truncatedSquareRoot n))])
isComposite : Nat -> Bool
isComposite n = (n > 2) && (not (isPrime n))
isOdd : Nat -> Bool
isOdd n = not (divides n 2)
-- Note: I couldn't find built-ins for these...
isElem : Nat -> List Nat -> Bool
isElem a [] = False
isElem a (b::bs) = (a == b) || (isElem a bs)
firstOrDefault : List a -> a -> a
firstOrDefault [] a = a
firstOrDefault (b::bs) a = b
-- Actually do the search
limit : Nat
limit = 10000
primes : List Nat
primes = [ p | p <- [1..limit], isPrime p ]
oddComposites : List Nat
oddComposites = [ c | c <- [1..limit], (isComposite c) && (isOdd c) ]
squares : List Nat
squares = [ b * b | b <- [1..limit], b * b <= limit ]
predicted : List Nat
predicted = [ p + (2 * s) | p <- primes, s <- squares, (p + (2 * s)) <= limit ]
contradictions : List Nat
contradictions = [ c | c <- oddComposites, not (isElem c predicted)]
main : IO ()
main = print (firstOrDefault contradictions 0)