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.
Common Divisors
Common Divisors
Sort by
recency
|
9 Discussions
|
Please Login in order to post a comment
haskell
I decided not to complicate and optimize, so I just used tail recursion:
Works for 9 test cases, but failed on one due to the time limit.
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
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.