Coprimes: a solution

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{--

A solution to the exercise posted at http://lpaste.net/107819

From the P-99 Prolog problem set, exercise 33: coprimality.

Two integers are coprime if their greatest common divisor is 1.

P33: Determine whether two integers are coprime: --}

data CoPrime = CoPrime Integer Integer
   deriving Show
data CoComposite =
    CoComposite { a :: Integer, b :: Integer,  exempli_gratia :: Integer }
   deriving Show

coprime :: Integer -> Integer -> Either CoPrime CoComposite
coprime a b = co' (gcd a b)
   where co' 1 = Left $ CoPrime a b
         co' n = Right $ CoComposite a b n

{-- 

Specifically, are the following number pairs coprime, if not, prove (or
counter-prove) they are not by producing a common factor greater than one.

The first two answers are provided.

a) 91245979	18211570	CoPrime
á) 76779654	2624340		CoComposite, exempli_gratia = 6
b) 22373537	16993305
d) 91251012	2784034
ð) 7663739	47537650
e) 86513675	7669917
é) 85891852	63428723
f) 15503901	85895700
g) 31982302	90125051
h) 97823598	85631050
i) 17380683	74696928
í) 81561621	95190700
j) 11635887	8645601
k) 66949253	40993744
l) 73283906	53717210
m) 4170817	42949996
n) 65366428	47796033
o) 1031137	22251410
ó) 55765011	53035953
p) 71790684	21607514
r) 93339395	68106578
s) 21636197	5541357
t) 90635974	90661702
u) 73316048	87732563
ú) 76216545	35135410
v) 62967518	31910986
x) 39888199	57250871
y) 14126109	14607057
ý) 67664871	67884032
þ) 85196042	65138766
æ) 83463295	3120879
ö) 43471387	82986575

(thank you, random.org)

(Letters are of the íslenska stafrófið)

 --}

runCoPrime :: FilePath -> IO [(String, Either CoPrime CoComposite)]
runCoPrime file =
   readFile file >>=
   mapM (\[ltr, a, b] -> return (ltr, coprime (read a) (read b)))
        . map words . lines

{--

So, for me:

runCoPrime "is.dat" >>= mapM_ (\(r, ans) -> putStrLn (r ++ "\t" ++ show ans)) ~>

a)	Left (CoPrime 91245979 18211570)
á)	Right (CoComposite {a = 76779654, b = 2624340, exempli_gratia = 6})
b)	Left (CoPrime 22373537 16993305)
d)	Right (CoComposite {a = 91251012, b = 2784034, exempli_gratia = 2})
ð)	Left (CoPrime 7663739 47537650)
e)	Left (CoPrime 86513675 7669917)
é)	Left (CoPrime 85891852 63428723)
f)	Right (CoComposite {a = 15503901, b = 85895700, exempli_gratia = 3})
g)	Left (CoPrime 31982302 90125051)
h)	Right (CoComposite {a = 97823598, b = 85631050, exempli_gratia = 2})
i)	Right (CoComposite {a = 17380683, b = 74696928, exempli_gratia = 3})
í)	Left (CoPrime 81561621 95190700)
j)	Right (CoComposite {a = 11635887, b = 8645601, exempli_gratia = 3})
k)	Left (CoPrime 66949253 40993744)
l)	Right (CoComposite {a = 73283906, b = 53717210, exempli_gratia = 2})
m)	Left (CoPrime 4170817 42949996)
n)	Left (CoPrime 65366428 47796033)
o)	Left (CoPrime 1031137 22251410)
ó)	Right (CoComposite {a = 55765011, b = 53035953, exempli_gratia = 3})
p)	Right (CoComposite {a = 71790684, b = 21607514, exempli_gratia = 2})
r)	Left (CoPrime 93339395 68106578)
s)	Left (CoPrime 21636197 5541357)
t)	Right (CoComposite {a = 90635974, b = 90661702, exempli_gratia = 2})
u)	Left (CoPrime 73316048 87732563)
ú)	Right (CoComposite {a = 76216545, b = 35135410, exempli_gratia = 5})
v)	Right (CoComposite {a = 62967518, b = 31910986, exempli_gratia = 2})
x)	Left (CoPrime 39888199 57250871)
y)	Right (CoComposite {a = 14126109, b = 14607057, exempli_gratia = 3})
ý)	Left (CoPrime 67664871 67884032)
þ)	Right (CoComposite {a = 85196042, b = 65138766, exempli_gratia = 2})
æ)	Left (CoPrime 83463295 3120879)
ö)	Left (CoPrime 43471387 82986575)

 --}
13:1: Warning: Use camelCase
Found:
data CoComposite = CoComposite{a :: Integer, b :: Integer,
exempli_gratia :: Integer}
deriving Show
Why not:
data CoComposite = CoComposite{a :: Integer, b :: Integer,
exempliGratia :: Integer}
deriving Show
71:4: Error: Use .
Found:
mapM (\ [ltr, a, b] -> return (ltr, coprime (read a) (read b))) .
map words . lines
Why not:
(mapM
((\ [ltr, a, b] -> return (ltr, coprime (read a) (read b))) .
words)
. lines)