-
Notifications
You must be signed in to change notification settings - Fork 7
254. Factor Combinations #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| import java.util.List; | ||
| import java.util.Stack; | ||
|
|
||
| public class FactorCombinations { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class PrimeFactorsGenerator { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a solution to the problem. This is just an algorithm for generating prime factors of a number. That's why it is in the math package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oke, now I understand. I will review then.
|
Your new debug-like explanation technique is really good by the way. |
altay9
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice codes and I have nothing to add about the coding side.
We can draw a backtracking tree for the code when we are available.
altay9
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the valuable contribution Erdem.
|
Thanks for the visualization by the way. It is a nice way to demonstrate the calls. |



Resolves: #60
254. Factor Combinations
Algorithm
In this question, there are few thing we aren't allowed to do:
{input number, 1}{3,9}and{9,3}by the input 27For this, we utilize a algorithm similar to that in #43. We keep our factors in a stack. We start iterating through our numbers initially at 2, then at the number that divides the other number when we reach the loop inside the method. At 24, 2 divides it. So we send a new backtracking branch with the values 12 and 2, we add the divisor 2 to the stack. Initially, to get around the 1. issue, we don't add the input number (the one other than the divisor) when the stack is empty, which could only occur when we initially call the method or the initial number is not factorizable as expected in the problem statement. Otherwise, we add the input number to the stack and add the stack to our factor combinations, then we pop the input number from the stack. Recursively, we also iterate through the possible numbers here and check if can branch off from there too.
We only iterate through possible divisors up to
i ≤ n/i, because otherwise we would encounter the same branch again. (For example12/3and12/4complement and disqualify themselves as such).Eventually, after the backtracking branch is complete, we remove the divisor from the stack to empty up place for the new possible divisor branches.
Here is video illustrating how this algorithm works for the input
n=12: https://youtu.be/ws8qX__wxq4Generating Prime Factors
Algorithm
This algorithm is actually quite simple and basically works the same way as the prime factorization one would have done in their school years. We take the number, we check if it is divisible by a prime starting with 2. If not, we check if it is divisible by the next prime 3 and then 5, 7, etc. The point is that the minimum prime of a number can at most be its square root, think of 121 as an example. So, we can apply the Sieve of Eratosthenes here, as we had done with #94. Up to this limiting maximum prime, we "sieve" through the factors of current primes and eliminate these. The remaining numbers are prime, so we add them to our list of primes and check whether they are our next prime factor.