new: Solved problems 31 to 41

This commit is contained in:
Sayantan Santra 2024-05-26 01:13:00 -05:00
parent 01eeffd2a4
commit 9b279bc5d0
Signed by: SinTan1729
GPG key ID: EB3E68BFBA25C85F
10 changed files with 120 additions and 0 deletions

View 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]

View 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

View 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

View 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]

View 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)

View 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

View 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

View 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]

View 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]

View 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]