Sort by

recency

|

9 Discussions

|

  • + 0 comments

    haskell

    import Data.List (intercalate)
    
    -- a <= b
    
    incr a b k
     | a `mod` k /= 0 = 0
     | b `mod` k /= 0 = x
     | a == k * k = 1
     | otherwise = 1 + x
     where x = if 0 == b `mod` (a `div` k) then 1 else 0
    
    help a b k acc
     | k2 < a = help a b (k+1) $ next
     | k2 > a = acc
     | otherwise = next
     where k2 = k * k
           next = acc + incr a b k
    
    common :: Int -> Int -> Int
    common x y =
      let a = min x y
          b = max x y
        in help a b 1 0
    
    foo s = let ws = map read $ words s in common (head ws) (ws !! 1)
    
    main = do
      cont <- getContents
      putStrLn $ intercalate "\n" $ map (show . foo) $ tail $ lines cont
    
  • + 0 comments

    I decided not to complicate and optimize, so I just used tail recursion:

        def noOfCd(x: Int, y: Int): Int = {
            def noOfCdRec(x: Int, y: Int, divisor: Int, result: Int = 0): Int = {
                if (divisor == 0) result
                else if (x % divisor == 0 && y % divisor == 0)
                    noOfCdRec(x, y, divisor - 1, result + 1)
                else noOfCdRec(x, y, divisor - 1, result)
            }
            
            noOfCdRec(x, y, min(x, y))
        }
    
  • + 0 comments
        def numDiv(l: List[Int]) ={
            val x = l.min
            var set = (1 to x).filter(x % _ == 0).toSet
            l.foreach(i => set = set.filter(i % _ ==0).toSet)
            set.size
        }
    

    Works for 9 test cases, but failed on one due to the time limit.

  • + 0 comments

    A fast way to solve this is by utilizing a fact of the prime factors of a number, which can be found here:

    N = A^p x B^q x C^r  here A, B , C are prime numbers and p,q,and r were respective powers of that prime numbers.
    
    Total numbers of factors for N = (p + 1)(q + 1)(r + 1)
    

    So, what you need to solve this problem is to get the prime factors of a number, and get the product of the powers (+1 for each power) of its prime factors.

    SPOILERS (Haskell)

    SPOILERS (Haskell)

    SPOILERS (Haskell)

    Here is my implementation of this

    import qualified Data.Map.Strict as Map
    
    
    counter = foldr (\x -> Map.insertWith (+) x 1) Map.empty
    
    -- I'm too lazy to actually implement a prime sieve, so doing (2 : [odd numbers]) is good enough for me
    -- Though if you want it to be actually efficient, do a real prime sieve
    primeFactors = counter . flip go (2 : [3,5..]) where
      go n (x:xs)
        | x * x > n = [n | n >= x]
        | n `mod` x == 0 = x : go (n `div` x) (x:xs)
        | otherwise = go n xs
    
    -- the aforementioned formula of N = (p + 1)(q + 1)(r + 1)(...)
    numFactors = product . map (+1) . Map.elems . primeFactors
    
    solveTestCase a b = numFactors $ gcd a b
    
    main = getLine >> do
        cases <- map (map read . words) <$> lines <$> getContents
        mapM_ (\[a,b] -> print $ solveTestCase a b) cases
    
  • + 2 comments

    An efficient way to find all the factors for a number is to first find factors less than or equal to the root of the number.

    For example for 26 the root is 5.099. Using the above method we have to test just 5 numbers instead of 25. Only 100 tests in case of 10000.

    The factors greater than the root can be found by dividing the number by the factors less than or equal to the number.

    Finding the intersection of the factors of all numbers is trivial after that.