I want a combination algorithm,...to multiple the elements in the array, to every other element but itself....
please help me to combine eg:: {1,2,3,4,5,6}
output:: 1*2,1*3....1*6, 1*2*3 !*2开发者_StackOverflow社区*4.......1*2*3*4*5*6.......5*6
code removed it was irrelevant to my logical requirement of question...
I believe this is what you are looking for:
//This method return a list of lists, where each "inner list" contains as
//its first number the number for which the factors are being found, and
//every other number is a factor.
//It takes an array of all the numbers whose factor you wish to find
public List<List<Integer>> getAllFactors(int[] toCalculate){
List<List<Integer>> toReturn = new ArrayList<List<Integer>>();
List<Integer> factors;
for(int i = 0; i < toCalculate.length; i++){
factors = new ArrayList<Integer>();
factors.add(toCalculate[i]); //add the number whose factors will be calculated
//the square root is used because it is the largest number you can divide by without getting "overlap"
for(int j = 1; j <= Math.sqrt(toCalculate[i]); j++){
if(toCalculate[i]%j==0){ //if it divides evenly, add it
factors.add(j);
factors.add((Integer)toCalculate[i]/j); //also add its "partner" number
}
}
toReturn.add(factors);
}
return toReturn;
}
I put in class Test
... Here is my testing method:
public static void main(String[] args){
Test t = new Test();
List<List<Integer>> blah = t.getAllFactors(new int[]{10, 12, 1, 5});
for(List<Integer> i: blah)
System.out.println(Arrays.toString(i.toArray(new Integer[i.size()])));
}
I realized that this wasn't what the question asked for... I think... I can't really tell... Anyway, I wrote an alternative method:
public List<Integer> getAllFactors(int number){
List<Integer> factors = new ArrayList<Integer>();
for(int i = 2; i <= Math.sqrt(number); i++){
if(number%i==0){
factors.add(number);
factors.addAll(getAllFactors(i));
factors.addAll(getAllFactors(number/i));
}
}
if(factors.isEmpty())
factors.add(number);
return factors;
}
But then I realized that might not be what you want either. The fact is, I actually have no fricking clue what you are looking for, I can't understand your post. I have just been trying to glean info from the comments.
Why not just declare arr
as a Set
from the start?
Set<Integer> arr = new LinkedHashSet<Integer>();
And leave it at that. You never want duplicates, and if order is important, a LinkedHashSet preserves insertion order.
First, finding factors. Here is a fragment from my own code. It builds a list of factors of target
in an ArrayList
called factors
. As each factor is found it is recorded once and divided out as many time as is needed. The upper limit is then reduced accordingly. The variable residue
holds the unfactored portion of target
.
int trialFactor = 0;
int residue = target;
limit = iSqrt(residue);
while (trialFactor < limit) {
trialFactor = nextPrime(trialFactor);
if (residue % trialFactor == 0) {
factors.add(trialFactor);
do {
// Remove repeated factors.
residue /= trialFactor;
} while (residue % trialFactor == 0);
limit = iSqrt(residue);
}
}
// Record largest factor, if present.
if (residue > 1) { factors.add(residue); }
There are two of my own functions used:
int nextPrime(int)
which returns the next highest prime.
int iSqrt(int)
which returns the integer square root; iSqrt(10)
returns 3.
Now for the second part of your question, combinations. You have a group of items and you want all the possible combinations. If there are n items then just iterate through all the numbers 0 to (2^n) - 1. That will give you all the bit patterns from 000 ... 000 to 111 ... 111. For each position in your group a '0' means not to include it in your output while a '1' means to include it.
So, given your {1,2,3,4,5,6} example, you need numbers from 0 to 2^6 - 1 = 63. Take an example, 45 is 101101 in binary, so your output for 45 is {1,3,4,6}.
精彩评论