Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main/java/algorithms/curated170/medium/FactorCombinations.java
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 {
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


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));
}
}
}
111 changes: 111 additions & 0 deletions src/main/java/algorithms/util/math/PrimeFactorsGenerator.java
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 {
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.


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));
}
}
}