diff options
Diffstat (limited to '31-41')
-rw-r--r-- | 31-41/31.hs | 4 | ||||
-rw-r--r-- | 31-41/32.hs | 5 | ||||
-rw-r--r-- | 31-41/33.hs | 9 | ||||
-rw-r--r-- | 31-41/34.hs | 11 | ||||
-rw-r--r-- | 31-41/35.hs | 9 | ||||
-rw-r--r-- | 31-41/36.hs | 15 | ||||
-rw-r--r-- | 31-41/37.hs | 19 | ||||
-rw-r--r-- | 31-41/39.hs | 5 | ||||
-rw-r--r-- | 31-41/40.hs | 10 | ||||
-rw-r--r-- | 31-41/41.hs | 16 |
10 files changed, 103 insertions, 0 deletions
diff --git a/31-41/31.hs b/31-41/31.hs new file mode 100644 index 0000000..d6d53a9 --- /dev/null +++ b/31-41/31.hs @@ -0,0 +1,4 @@ +isPrime :: Int -> Bool + +isPrime 1 = False +isPrime x = and $ map (\y -> x `mod` y /= 0) $ fst $ span (\y -> y * y <= x) [2..] diff --git a/31-41/32.hs b/31-41/32.hs new file mode 100644 index 0000000..585039d --- /dev/null +++ b/31-41/32.hs @@ -0,0 +1,5 @@ +myGCD :: Int -> Int -> Int + + +myGCD a 0 = abs a +myGCD a b = myGCD b (a `mod` b) diff --git a/31-41/33.hs b/31-41/33.hs new file mode 100644 index 0000000..52bebbb --- /dev/null +++ b/31-41/33.hs @@ -0,0 +1,9 @@ +myGCD :: Int -> Int -> Int + + +myGCD a 0 = abs a +myGCD a b = myGCD b (a `mod` b) + +coprime :: Int -> Int -> Bool + +coprime a b = myGCD a b == 1 diff --git a/31-41/34.hs b/31-41/34.hs new file mode 100644 index 0000000..fe542a2 --- /dev/null +++ b/31-41/34.hs @@ -0,0 +1,11 @@ +import Data.List (foldl') + +myGCD :: Int -> Int -> Int + + +myGCD a 0 = abs a +myGCD a b = myGCD b (a `mod` b) + +totient :: Int -> Int + +totient x = foldl' (\acc y -> acc + if myGCD x y == 1 then 1 else 0) 0 [1..x] diff --git a/31-41/35.hs b/31-41/35.hs new file mode 100644 index 0000000..185d024 --- /dev/null +++ b/31-41/35.hs @@ -0,0 +1,9 @@ +primeFactors :: Int -> [Int] + +primeFactors x = factor x primes + where primes = filterPrime [2..] + filterPrime (p:xs) = p:filterPrime [x | x <- xs, x `mod` p /= 0] + factor x l@(p:xs) + | p > x = [] + | x `mod` p == 0 = p:factor (x `div` p) l + | otherwise = factor x xs diff --git a/31-41/36.hs b/31-41/36.hs new file mode 100644 index 0000000..cb9d09a --- /dev/null +++ b/31-41/36.hs @@ -0,0 +1,15 @@ +import Data.List (group) + +primeFactors :: Int -> [Int] + +primeFactors x = factor x primes + where primes = filterPrime [2..] + filterPrime (p:xs) = p:filterPrime [x | x <- xs, x `mod` p /= 0] + factor x l@(p:xs) + | p > x = [] + | x `mod` p == 0 = p:factor (x `div` p) l + | otherwise = factor x xs + +primeFactorsMult :: Int -> [(Int, Int)] + +primeFactorsMult x = [(head l, length l) | l <- group $ primeFactors x] diff --git a/31-41/37.hs b/31-41/37.hs new file mode 100644 index 0000000..ead0514 --- /dev/null +++ b/31-41/37.hs @@ -0,0 +1,19 @@ +import Data.List (group, foldl') + +primeFactors :: Int -> [Int] + +primeFactors x = factor x primes + where primes = filterPrime [2..] + filterPrime (p:xs) = p:filterPrime [x | x <- xs, x `mod` p /= 0] + factor x l@(p:xs) + | p > x = [] + | x `mod` p == 0 = p:factor (x `div` p) l + | otherwise = factor x xs + +primeFactorsMult :: Int -> [(Int, Int)] + +primeFactorsMult x = [(head l, length l) | l <- group $ primeFactors x] + +phi :: Int -> Int + +phi x = foldl' (\acc (p, m) -> acc * (p - 1) ^ m) 1 (primeFactorsMult x) diff --git a/31-41/39.hs b/31-41/39.hs new file mode 100644 index 0000000..68909d7 --- /dev/null +++ b/31-41/39.hs @@ -0,0 +1,5 @@ +primesR :: Int -> Int -> [Int] + +primesR l r = takeWhile (<= r) $ dropWhile (< l) primes + where primes = filterPrime[2..] + filterPrime (p:xs) = p:filterPrime [x | x <- xs, x `mod` p /= 0] diff --git a/31-41/40.hs b/31-41/40.hs new file mode 100644 index 0000000..9d9b2f7 --- /dev/null +++ b/31-41/40.hs @@ -0,0 +1,10 @@ +isPrime :: Int -> Bool + +isPrime 1 = False +isPrime x = and $ map (\y -> x `mod` y /= 0) $ fst $ span (\y -> y * y <= x) [2..] + +goldbach :: Int -> (Int, Int) + +goldbach x = head [(a, b) | a <- primes, let b = x - a, isPrime b] + where primes = filterPrime[3..] + filterPrime (p:xs) = p:filterPrime [x | x <- xs, x `mod` p /= 0] diff --git a/31-41/41.hs b/31-41/41.hs new file mode 100644 index 0000000..16fd51f --- /dev/null +++ b/31-41/41.hs @@ -0,0 +1,16 @@ +isPrime :: Int -> Bool + +isPrime 1 = False +isPrime x = and $ map (\y -> x `mod` y /= 0) $ fst $ span (\y -> y * y <= x) [2..] + +goldbach :: Int -> (Int, Int) + +goldbach x = head [(a, b) | a <- primes, let b = x - a, isPrime b] + where primes = filterPrime[3..] + filterPrime (p:xs) = p:filterPrime [x | x <- xs, x `mod` p /= 0] + +goldbachList :: Int -> Int -> [(Int, Int)] +goldbachList' :: Int -> Int -> Int -> [(Int, Int)] + +goldbachList l r = map goldbach $ filter even $ dropWhile (< 4) [l..r] +goldbachList' l r i = filter (\(a, b) -> a > i && b > i) $ goldbachList l r |