We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
importqualifiedData.Map.StrictasMapcounter=foldr(\x->Map.insertWith(+)x1)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 sieveprimeFactors=counter.flipgo(2:[3,5..])wheregon(x:xs)|x*x>n=[n|n>=x]|n`mod`x==0=x:go(n`div`x)(x:xs)|otherwise=gonxs-- the aforementioned formula of N = (p + 1)(q + 1)(r + 1)(...)numFactors=product.map(+1).Map.elems.primeFactorssolveTestCaseab=numFactors$gcdabmain=getLine>>docases<-map(mapread.words)<$>lines<$>getContentsmapM_(\[a,b]->print$solveTestCaseab)cases
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Common Divisors
You are viewing a single comment's thread. Return to all comments →
A fast way to solve this is by utilizing a fact of the prime factors of a number, which can be found here:
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