-
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
Changes from all commits
c1a9cdd
d8ab322
ce03f0c
8b88f65
d2b7882
df3e80b
82439fc
e648a9b
130f1aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package algorithms.curated170.medium; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Stack; | ||
|
|
||
| public class FactorCombinations { | ||
|
|
||
| List<List<Integer>> combinations; | ||
| Stack<Integer> currFactors; | ||
|
|
||
| void backtracking(int n, int index) { | ||
| if (n > 1) { | ||
| if (!currFactors.isEmpty()) { | ||
| currFactors.add(n); | ||
| combinations.add(new ArrayList<>(currFactors)); | ||
| currFactors.pop(); | ||
| } | ||
| } | ||
|
|
||
| for (int i = index; i <= n / i; i++) { | ||
| if (n % i == 0) { | ||
| currFactors.add(i); | ||
| backtracking(n / i, i); | ||
| currFactors.pop(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public List<List<Integer>> getFactors(int n) { | ||
| combinations = new ArrayList<>(); | ||
| if (n < 4) | ||
| { | ||
| return combinations; | ||
| } | ||
| currFactors = new Stack<>(); | ||
| backtracking(n, 2); | ||
| return combinations; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| var solution = new FactorCombinations(); | ||
| for (int i = 0; i < (1 << 5); i++) { | ||
| System.out.println("Factors of " + i + ": " + new FactorCombinations().getFactors(i)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package algorithms.util.math; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class PrimeFactorsGenerator { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oke, now I understand. I will review then. |
||
|
|
||
| List<List<Integer>> ans = new ArrayList<>(); | ||
| Set<Integer> hasFactor = new HashSet<>(); | ||
| int limitPrime; | ||
|
|
||
| List<Integer> primes = new ArrayList<>(List.of(2, 3, 5, 7)); | ||
|
|
||
| boolean sieveFilled = false; | ||
| boolean[] sieve; | ||
| List<Integer> primeFactors = new ArrayList<>(); | ||
|
|
||
| int orig; | ||
|
|
||
| public List<Integer> getPrimeFactors(int num) { | ||
|
|
||
| orig = num; | ||
| limitPrime = (int) Math.sqrt(num); | ||
| sieve = new boolean[limitPrime + 1]; | ||
| if (limitPrime < 11) { | ||
| sieveFilled = true; | ||
| } | ||
|
|
||
| divideAndFindPrimeFactors(num, 0, 1); | ||
|
|
||
| return primeFactors; | ||
| } | ||
|
|
||
| private boolean isPrime(int n) { | ||
| for (int p : primes) { | ||
| if (n % p == 0) { | ||
| return false; | ||
| } | ||
| } | ||
| return sieveFilled; | ||
| } | ||
|
|
||
| private void divideAndFindPrimeFactors(int n, int ip, int prev) { | ||
|
|
||
| if (prev == orig) { | ||
| return; | ||
| } | ||
|
|
||
| boolean divisible = false; | ||
| int prime = getPrime(ip); | ||
| if (n < 2) { | ||
| return; | ||
| } | ||
|
|
||
| divisible = n % prime == 0; | ||
|
|
||
| if (!divisible && isPrime(n)) { | ||
| primeFactors.add(n); | ||
| return; | ||
| } | ||
|
|
||
| if (divisible) { | ||
| primeFactors.add(prime); | ||
| divideAndFindPrimeFactors(n / prime, ip, prev * prime); | ||
| } else { | ||
| divideAndFindPrimeFactors(n, ip + 1, prev); | ||
| } | ||
| } | ||
|
|
||
| private int getPrime(int ip) { | ||
| if (primes.size() > ip) { | ||
| return primes.get(ip); | ||
| } | ||
|
|
||
| fillPrimeSleeve(); | ||
|
|
||
| return findFirstValidPrime(); | ||
| } | ||
|
|
||
| private int findFirstValidPrime() { | ||
| int firstP = -1; | ||
| for (int i = 3; i < sieve.length; i += 2) { | ||
| if (!sieve[i]) { | ||
| primes.add(i); | ||
| firstP = firstP < 0 ? i : firstP; | ||
| } | ||
| } | ||
| sieveFilled = true; | ||
| return firstP; | ||
| } | ||
|
|
||
| private void fillPrimeSleeve() { | ||
| for (int p : primes) { | ||
| if (p == 2 || sieve[p]) { | ||
| continue; | ||
| } | ||
|
|
||
| for (int i = p; i < sieve.length; i += p) { | ||
| sieve[i] = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| for (int i = 0; i < (1 << 10); i++) { | ||
| System.out.println("Factors of " + i + ": " + new PrimeFactorsGenerator().getPrimeFactors(i)); | ||
| } | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.
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 great.
