-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP98957.hs
128 lines (103 loc) · 4.69 KB
/
P98957.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
------------------------------------------------------------------------------------
-------------------------------------- ones ---------------------------------------
------------------------------------------------------------------------------------
ones :: [Integer]
ones = repeat 1
------------------------------------------------------------------------------------
-------------------------------------- nats ---------------------------------------
------------------------------------------------------------------------------------
nats :: [Integer]
nats = iterate (+1) 0
------------------------------------------------------------------------------------
-------------------------------------- ints ---------------------------------------
------------------------------------------------------------------------------------
ints :: [Integer]
ints = iterate (integer) 0
where
integer :: Integer -> Integer
integer x
| x > 0 = (-x)
| otherwise = (-x) + 1
------------------------------------------------------------------------------------
---------------------------------- triangulars ------------------------------------
------------------------------------------------------------------------------------
triangulars :: [Integer]
triangulars = triangle 0
where
triangle :: Integer -> [Integer]
triangle x = div (x * (x + 1)) 2 : triangle (x + 1)
------------------------------------------------------------------------------------
----------------------------------- factorials ------------------------------------
------------------------------------------------------------------------------------
factorials :: [Integer]
factorials = facts 0
where
facts :: Integer -> [Integer]
facts x = factorial x : facts (x + 1)
where
factorial :: Integer -> Integer
factorial 0 = 1
factorial x = x * factorial (x - 1)
------------------------------------------------------------------------------------
-------------------------------------- fibs ---------------------------------------
------------------------------------------------------------------------------------
fibs :: [Integer]
fibs = fibonaccis 0 1
where
fibonaccis :: Integer -> Integer -> [Integer]
fibonaccis x y = x : fibonaccis y (x + y)
------------------------------------------------------------------------------------
------------------------------------- primes --------------------------------------
------------------------------------------------------------------------------------
isPrimeRec :: Integer -> Integer -> Bool
isPrimeRec x d
| d == 1 = True
| mod x d == 0 = False
| otherwise = isPrimeRec x (d - 1)
isPrime :: Integer -> Bool
isPrime x
| x == 0 = False
| x == 1 = False
| otherwise = isPrimeRec x (floor (sqrt (fromIntegral x)))
primes :: [Integer]
primes = primers 2
where
primers :: Integer -> [Integer]
primers x
| isPrime x = x : primers (x + 1)
| otherwise = primers (x + 1)
------------------------------------------------------------------------------------
------------------------------------ hammings -------------------------------------
------------------------------------------------------------------------------------
hammings :: [Integer]
hammings = 1 : merge3 (map (* 2) hammings) (map (* 3) hammings) (map (* 5) hammings)
merge3 :: [Integer] -> [Integer] -> [Integer] -> [Integer]
merge3 xs ys zs = merge2 (merge2 xs ys) zs
merge2 :: [Integer] -> [Integer] -> [Integer]
merge2 a [] = a
merge2 [] b = b
merge2 (a:as) (b:bs)
| a < b = a : (merge2 as (b:bs))
| b < a = b : (merge2 (a:as) bs)
|otherwise = merge2 (a:as) bs
------------------------------------------------------------------------------------
------------------------------------ lookNsay -------------------------------------
------------------------------------------------------------------------------------
lookNsay :: [Integer]
lookNsay = iterate count 1
count :: Integer -> Integer
count a = read $ next $ show a
next :: [Char] -> [Char]
next [] = []
next cs = (show n) ++ [pr] ++ next cua
where
pr = head cs
n = length $ takeWhile ( == pr) cs
cua = dropWhile ( == pr) cs
------------------------------------------------------------------------------------
----------------------------------- tartaglia -------------------------------------
------------------------------------------------------------------------------------
tartaglia :: [[Integer]]
tartaglia = (iterate (pascal) [1])
pascal :: [Integer] -> [Integer]
pascal xs = zipWith (+) (xs ++ [0]) ([0] ++ xs)