Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jun 3, 2021

Resolves: #60

254. Factor Combinations

Algorithm

In this question, there are few thing we aren't allowed to do:

  1. The factors don't include the pair {input number, 1}
  2. We don't include the duplicate pairs, for example {3,9} and {9,3} by the input 27

For 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 example 12/3 and 12/4 complement 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__wxq4

Generating 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.

@ErdemT09 ErdemT09 marked this pull request as ready for review June 3, 2021 15:09
import java.util.List;
import java.util.Stack;

public class FactorCombinations {
Copy link
Collaborator

@altay9 altay9 Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great.
image

import java.util.List;
import java.util.Set;

public class PrimeFactorsGenerator {
Copy link
Collaborator

@altay9 altay9 Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix the return value?

image

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

@altay9
Copy link
Collaborator

altay9 commented Jun 4, 2021

Your new debug-like explanation technique is really good by the way.

https://youtu.be/ws8qX__wxq4

altay9
altay9 previously approved these changes Jun 4, 2021
Copy link
Collaborator

@altay9 altay9 left a 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.

@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented Jun 5, 2021

We can draw a backtracking tree for the code when we are available.

I tried to illustrate the method calls like this.
image

Copy link
Collaborator

@altay9 altay9 left a 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.

@ErdemT09 ErdemT09 merged commit ce3902e into master Jun 5, 2021
@ErdemT09 ErdemT09 deleted the 254.-Factor-Combinations branch June 5, 2021 08:47
@altay9
Copy link
Collaborator

altay9 commented Jun 5, 2021

Thanks for the visualization by the way. It is a nice way to demonstrate the calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

254. Factor Combinations

3 participants