-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.hs
More file actions
37 lines (25 loc) · 960 Bytes
/
util.hs
File metadata and controls
37 lines (25 loc) · 960 Bytes
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
import Data.Char
import Numeric
import Data.List
isPrime x = not $ any divisible $ takeWhile notTooBig [2..] where
divisible y = x `mod`y == 0
notTooBig y = y*y <= x
isFactor x y = if x `mod` y == 0 then True else False
divisors n = 1 : filter ( (==0) . rem n) [2..n `div` 2]
fib 0 = 0
fib 1 = 1
fib n | even n = f1 * (f1 + 2 * f2)
| n `mod` 4 == 1 = (2 * f1 + f2) * (2 * f1 - f2) + 2
| otherwise = (2 * f1 + f2) * (2 * f1 - f2) - 2
where k = n `div` 2
f1 = fib k
f2 = fib (k-1)
twentyone n = sum(divisors(n))
isAmicable n = n == twentyone(twentyone(n)) && n /= twentyone(n)
isPalindrome n = numberToList n == reverse(numberToList n)
numberToList n = map digitToInt (show n)
toBin n = showIntAtBase 2 intToDigit n ""
slice :: [a] -> [[a]]
slice = filter (not . null) . concatMap tails . inits
triangularNumbers n = scanl (+) 1 [2..n]
abundantNumbers n = [n | n<-[1..n], sum(divisors n) > n]