new: Solved problems 31 to 41
This commit is contained in:
parent
01eeffd2a4
commit
9b279bc5d0
10 changed files with 120 additions and 0 deletions
5
Problems 31-41/problem_31.hs
Normal file
5
Problems 31-41/problem_31.hs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
isqrt :: (Integral n) => n -> n
|
||||||
|
isqrt = floor . sqrt . fromIntegral
|
||||||
|
|
||||||
|
isPrime :: (Integral n) => n -> Bool
|
||||||
|
isPrime n = (n >= 2) && all ((/= 0) . (n `mod`)) [2 .. isqrt n]
|
7
Problems 31-41/problem_32.hs
Normal file
7
Problems 31-41/problem_32.hs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
myGCD :: (Integral n) => n -> n -> n
|
||||||
|
myGCD m n =
|
||||||
|
if remainder == 0
|
||||||
|
then abs n
|
||||||
|
else myGCD n remainder
|
||||||
|
where
|
||||||
|
remainder = m `mod` n
|
10
Problems 31-41/problem_33.hs
Normal file
10
Problems 31-41/problem_33.hs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
myGCD :: (Integral n) => n -> n -> n
|
||||||
|
myGCD m n =
|
||||||
|
if remainder == 0
|
||||||
|
then abs n
|
||||||
|
else myGCD n remainder
|
||||||
|
where
|
||||||
|
remainder = m `mod` n
|
||||||
|
|
||||||
|
coprime :: (Integral n) => n -> n -> Bool
|
||||||
|
coprime m n = myGCD m n == 1
|
13
Problems 31-41/problem_34.hs
Normal file
13
Problems 31-41/problem_34.hs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
myGCD :: (Integral n) => n -> n -> n
|
||||||
|
myGCD m n =
|
||||||
|
if remainder == 0
|
||||||
|
then abs n
|
||||||
|
else myGCD n remainder
|
||||||
|
where
|
||||||
|
remainder = m `mod` n
|
||||||
|
|
||||||
|
coprime :: (Integral n) => n -> n -> Bool
|
||||||
|
coprime m n = myGCD m n == 1
|
||||||
|
|
||||||
|
totient :: (Integral n) => n -> n
|
||||||
|
totient m = fromIntegral $ length $ filter (coprime m) [1 .. m - 1]
|
10
Problems 31-41/problem_35.hs
Normal file
10
Problems 31-41/problem_35.hs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import Data.List (find)
|
||||||
|
|
||||||
|
isqrt :: (Integral n) => n -> n
|
||||||
|
isqrt = floor . sqrt . fromIntegral
|
||||||
|
|
||||||
|
primeFactors :: (Integral n) => n -> [n]
|
||||||
|
primeFactors n =
|
||||||
|
case find ((== 0) . (n `mod`)) [2 .. isqrt n] of
|
||||||
|
Nothing -> [n]
|
||||||
|
Just m -> m : primeFactors (n `div` m)
|
13
Problems 31-41/problem_36.hs
Normal file
13
Problems 31-41/problem_36.hs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import Data.List (find, group)
|
||||||
|
|
||||||
|
isqrt :: (Integral n) => n -> n
|
||||||
|
isqrt = floor . sqrt . fromIntegral
|
||||||
|
|
||||||
|
primeFactors :: (Integral n) => n -> [n]
|
||||||
|
primeFactors n =
|
||||||
|
case find ((== 0) . (n `mod`)) [2 .. isqrt n] of
|
||||||
|
Nothing -> [n]
|
||||||
|
Just m -> m : primeFactors (n `div` m)
|
||||||
|
|
||||||
|
primeFactorsMult :: (Integral n) => n -> [(n, n)]
|
||||||
|
primeFactorsMult = map (\g -> (head g, fromIntegral $ length g)) . group . primeFactors
|
16
Problems 31-41/problem_37.hs
Normal file
16
Problems 31-41/problem_37.hs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import Data.List (find, group)
|
||||||
|
|
||||||
|
isqrt :: (Integral n) => n -> n
|
||||||
|
isqrt = floor . sqrt . fromIntegral
|
||||||
|
|
||||||
|
primeFactors :: (Integral n) => n -> [n]
|
||||||
|
primeFactors n =
|
||||||
|
case find ((== 0) . (n `mod`)) [2 .. isqrt n] of
|
||||||
|
Nothing -> [n]
|
||||||
|
Just m -> m : primeFactors (n `div` m)
|
||||||
|
|
||||||
|
primeFactorsMult :: (Integral n) => n -> [(n, n)]
|
||||||
|
primeFactorsMult = map (\g -> (head g, fromIntegral $ length g)) . group . primeFactors
|
||||||
|
|
||||||
|
phi :: (Integral n) => n -> n
|
||||||
|
phi = product . map (\(p, r) -> (p - 1) * p ^ (r - 1)) . primeFactorsMult
|
8
Problems 31-41/problem_39.hs
Normal file
8
Problems 31-41/problem_39.hs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
isqrt :: (Integral n) => n -> n
|
||||||
|
isqrt = floor . sqrt . fromIntegral
|
||||||
|
|
||||||
|
isPrime :: (Integral n) => n -> Bool
|
||||||
|
isPrime n = (n >= 2) && all ((/= 0) . (n `mod`)) [2 .. isqrt n]
|
||||||
|
|
||||||
|
primesR :: (Integral n) => n -> n -> [n]
|
||||||
|
primesR m n = filter isPrime [m .. n]
|
16
Problems 31-41/problem_40.hs
Normal file
16
Problems 31-41/problem_40.hs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import Data.List (find)
|
||||||
|
|
||||||
|
isqrt :: (Integral n) => n -> n
|
||||||
|
isqrt = floor . sqrt . fromIntegral
|
||||||
|
|
||||||
|
isPrime :: (Integral n) => n -> Bool
|
||||||
|
isPrime n = (n >= 2) && all ((/= 0) . (n `mod`)) [2 .. isqrt n]
|
||||||
|
|
||||||
|
goldbach :: (Integral n) => n -> (n, n)
|
||||||
|
goldbach n =
|
||||||
|
if odd n
|
||||||
|
then error "Odd number was given"
|
||||||
|
else (p, n - p)
|
||||||
|
where
|
||||||
|
Just p = find (\p -> (n - p) `elem` primes) primes
|
||||||
|
primes = filter isPrime [2 .. n - 1]
|
22
Problems 31-41/problem_41.hs
Normal file
22
Problems 31-41/problem_41.hs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import Data.List (find)
|
||||||
|
|
||||||
|
isqrt :: (Integral n) => n -> n
|
||||||
|
isqrt = floor . sqrt . fromIntegral
|
||||||
|
|
||||||
|
isPrime :: (Integral n) => n -> Bool
|
||||||
|
isPrime n = (n >= 2) && all ((/= 0) . (n `mod`)) [2 .. isqrt n]
|
||||||
|
|
||||||
|
goldbach :: (Integral n) => n -> (n, n)
|
||||||
|
goldbach n =
|
||||||
|
if odd n
|
||||||
|
then error "Odd number was given"
|
||||||
|
else (p, n - p)
|
||||||
|
where
|
||||||
|
Just p = find (\p -> (n - p) `elem` primes) primes
|
||||||
|
primes = filter isPrime [2 .. n - 1]
|
||||||
|
|
||||||
|
goldbachList' :: (Integral n) => n -> n -> n -> [(n, n)]
|
||||||
|
goldbachList' m n d = filter (\(p, q) -> p > d && q > d) $ map goldbach $ filter even [m .. n]
|
||||||
|
|
||||||
|
goldbachList :: (Integral n) => n -> n -> [(n, n)]
|
||||||
|
goldbachList m n = map goldbach $ filter even [m .. n]
|
Loading…
Reference in a new issue