Compare commits

...

2 Commits

Author SHA1 Message Date
Sayantan Santra 76b85d7d84
new: Solved problems 31 to 41 2024-05-26 01:10:33 -05:00
Sayantan Santra 01eeffd2a4
chg: Only import the required functions 2024-05-26 00:36:26 -05:00
15 changed files with 128 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import Data.List
import System.Random
import Data.List (nub)
import System.Random (getStdGen, randomRs)
rndSelect :: [a] -> Int -> IO [a]
rndSelect ls n = do

View File

@ -1,5 +1,5 @@
import Data.List
import System.Random
import Data.List (nub)
import System.Random (getStdGen, randomRs)
diffSelect :: Int -> Int -> IO [Int]
diffSelect n m = do

View File

@ -1,5 +1,5 @@
import Data.List
import System.Random
import Data.List (nub)
import System.Random (getStdGen, randomRs)
rndPermu :: [a] -> IO [a]
rndPermu ls = do

View File

@ -1,4 +1,4 @@
import Data.List
import Data.List (groupBy, nub, permutations, sortBy)
group' :: [Int] -> [a] -> [[[a]]]
group' ln ls = map ((([([fst] <*>)] <*>) . groupBy grouper . sortBy sorter) . zip ls) (nub (permutations placers))

View File

@ -1,4 +1,4 @@
import Data.List
import Data.List (sortBy)
lsort :: [[a]] -> [[a]]
lsort = sortBy (\x y -> length x `compare` length y)

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 = goldbachList' m n 1