forked from Zenius2016/BoardAlfa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
three.hs
104 lines (47 loc) · 1.59 KB
/
three.hs
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
module Three where
import Data.List
sol1 lim = iter 1 0
where iter i result
| i > lim = result
| rem i 3 == 0 || rem i 5 == 0 = iter (i+1) (result + i)
| otherwise = iter (i+1) result
sum' [] = 0
sum' (x:xs) = x + sum' xs
sum'' lst = helper lst 0
where helper [] res = res
helper (x:xs) res = helper xs (x+res)
-- sum [2,3,4] = helper [2,3,4] 0
-- helper [2,3,4] 0 = helper [3,4] (2+0)
-- helper [3,4] 2 = helper [4] (3+2)
-- helper [4] 5 = helper [] (4+5)
-- sum [3,5,5,8] = 3 + sum [5,5,8]
-- sum [5,5,8] = 5 + sum [5,8]
-- .. sum [] = 0
-- iter 1 0 => iter 2 0
-- iter 2 0 => iter 3 0
-- iter 3 0 => iter 4 3
-- iter 4 3 => iter 5 3
-- iter 5 3 => iter 6 8
-- iter 6 8 => iter 7 14
-- p => q
-- ~ q => ~ p
eulerNo1a = sum [x | x <- [1..999], rem x 3 == 0 || rem x 5 == 0]
eulerNo1b = sum [3,6..999] + sum [5,10..999] - sum [15,30..999]
pitagoras = [(x,y,z) | x <- [1..100], y <- [x..100], z <- [y..100], z^2 == x^2 + y^2]
daduGanjil = [(a,b) | a <- [1..6], b <- [1..6], let c = a*b, odd c]
words' st = iter [] [] $ reverse st
where iter res cur [] = cur:res
iter res cur (' ':[]) = cur:res
iter res cur (' ':xs) = iter (cur:res) [] xs
iter res cur (x:xs) = iter res (x:cur) xs
-- words "asd jon" = iter [] [] "noj dsa"
-- iter [] "n" "oj dsa"
-- iter [] "on" "j dsa"
-- iter [] "jon" " dsa"
-- iter ["jon"] "" "dsa"
-- iter ["jon"] "d" "sa"
-- iter ["jon"] "dsa"
-- iter ["dsa","jon"]
lfibo = zipWith (+) lfibo $ tail lfibo
pascal = iterate (\x -> zipWith (+) (0:x) (x::[0])) [1]
-- save