So basically, you don't need to check for every actual factor, you just need to search for how many common prime factors there are, then work out how many combos that adds up to. So any prime that occurs "x" times, multiplies the total number of combos by "x+1". if the two numbers are divisible by 4, 3, 5:Ĥ = 2^2, so you could have no "2s", 1 "2" or 2 "2s" in the combined factor, so the total combinations 3 x 2 x 2 = 12. If any of the primes occurs more than once, then it changes the equation slightly. So each prime that occurs once multiplies the number of combos by 2. if the two numbers are both divisible by 2, 3 and 5, then the number of ways those three primes can combine is 8 (2^3), treating the presence of each prime as a true/false thing. 2 and keep dividing before you go on).Į.g. (but keep checking whether either is divisible by e.g. Divide m and n by any "found" numbers as you go through it. if only one number is divisible by 5, and the other is not, you can divide that one by 5 and you still get the same answer for common factors. You can make this much faster by calculating how many common prime factors there are, and by dividing out any "found" primes as you go. It will add up, but there are obviously much better ways to improve things. In general loop invariant code can be calculated before the loop, and stored.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
May 2023
Categories |