March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,11 +1,43 @@
|
|||
public static List<BigInteger> primeFactorBig(BigInteger a){
|
||||
List<BigInteger> ans = new LinkedList<BigInteger>();
|
||||
private static final BigInteger TWO = BigInteger.valueOf(2);
|
||||
private static final BigInteger THREE = BigInteger.valueOf(3);
|
||||
private static final BigInteger FIVE = BigInteger.valueOf(5);
|
||||
|
||||
for(BigInteger divisor = BigInteger.valueOf(2);
|
||||
a.compareTo(ONE) > 0; divisor = divisor.add(ONE))
|
||||
while(a.mod(divisor).equals(ZERO)){
|
||||
ans.add(divisor);
|
||||
a = a.divide(divisor);
|
||||
public static ArrayList<BigInteger> primeDecomp(BigInteger n){
|
||||
if(n.compareTo(TWO) < 0) return null;
|
||||
ArrayList<BigInteger> factors = new ArrayList<BigInteger>();
|
||||
|
||||
// handle even values
|
||||
while(n.and(BigInteger.ONE).equals(BigInteger.ZERO)){
|
||||
n = n.shiftRight(1);
|
||||
factors.add(TWO);
|
||||
}
|
||||
|
||||
// handle values divisible by three
|
||||
while(n.mod(THREE).equals(BigInteger.ZERO)){
|
||||
factors.add(THREE);
|
||||
n = n.divide(THREE);
|
||||
}
|
||||
|
||||
// handle values divisible by five
|
||||
while(n.mod(FIVE).equals(BigInteger.ZERO)){
|
||||
factors.add(FIVE);
|
||||
n = n.divide(FIVE);
|
||||
}
|
||||
|
||||
// much like how we can skip multiples of two, we can also skip
|
||||
// multiples of three and multiples of five. This increment array
|
||||
// helps us to accomplish that
|
||||
int[] pattern = {4,2,4,2,4,6,2,6};
|
||||
int pattern_index = 0;
|
||||
BigInteger current_test = BigInteger.valueOf(7);
|
||||
while(!n.equals(BigInteger.ONE)){
|
||||
while(n.mod(current_test).equals(BigInteger.ZERO)){
|
||||
factors.add(current_test);
|
||||
n = n.divide(current_test);
|
||||
}
|
||||
return ans;
|
||||
current_test = current_test.add(BigInteger.valueOf(pattern[pattern_index]));
|
||||
pattern_index = (pattern_index + 1) & 7;
|
||||
}
|
||||
|
||||
return factors;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue