How would I go about using multiple arraylists to filter factors of a number?
题
Okay, so say i want to find factors of a number a. I know how to do that, using a for loop and an arraylist. My problem is that (and i have no idea how to go about this) I want the factors in pairs.(that multiply together to get the original number) I would assume I could get the factors in multiple arraylists that each have 2 variables.. but that is extremely unwieldy as different numbers will have different numbers of factors. (Not to mention, I dont know how to do this..)
After this, I want to test those variables in an equation to narrow them down. Say, whichever of those pairs add up to a given number is the one I want to use. I have been trying to figure out how to go about all of this using tutorials but I am completely lost, as I am somewhat new to Java.
Thank you for any and all help :-)
解决方案
You could create your own class that had fields for each side of the pair, but it would be acceptable and convenient to use a Map<Integer, Integer>
. A basic implementation would be:
private static Map<Integer, Integer> factorize(int number) {
Map<Integer, Integer> factors = new HashMap<Integer, Integer>();
for (int i = 1; i < Math.sqrt(number); i++) {
if (number % i == 0)
factors.put(i, number / i);
}
return factors;
}
Here's some test code to show how it would be called and what the result is:
public static void main(String[] args) {
Map<Integer, Integer> factorPairs = factorize(12345);
for (Map.Entry<Integer, Integer> factor : factorPairs.entrySet())
System.out.println(factor.getKey() + " x " + factor.getValue());
}
Output:
1 x 12345
3 x 4115
5 x 2469
15 x 823
That should be enough to get you started
其他提示
Say you want the factors of a number x:
Let y = √x
Then loop from 1 to y to find the factors.
Example
Say you have the number 218. I'll just write Javaish pseudocode since I don't remember Java very well:
int x = 218;
int y = Math.sqrt(218); // 14
for(int i = 1; i <= y; i++){
if i is a factor of x
Add it to the list
}
// Your list will now be: 1, 2
The factor that pairs with each element in your list is just x divided by that element.
So the pairing factor of 1 is 218/1 = 218, and the pairing factor of 2 is 218/2 = 109
try something like this using a list of lists of integers:
import java.util.*;
public class Main {
static List<Integer> findListThatSumsTo10(List<List<Integer>> list) {
for (List<Integer> l : list) {
int sum = 0;
for (Integer i : l)
sum += i;
if (sum == 10) return l;
}
return null;
}
public static void main(String[] args) {
List<List<Integer>> list = new LinkedList<List<Integer>>();
Integer[] factorsOf15 = { 3, 5 };
Integer[] factorsOf30 = { 2, 3, 5 };
list.add(Arrays.asList(factorsOf15));
list.add(Arrays.asList(factorsOf30));
System.out.println(list);
System.out.println(findListThatSumsTo10(list));
}
}
You don't need to store pairs.
Lets say that N
is your original number, and F1
and F2
are a pair of factors; i.e. N = F1 x F2
.
Given that N
is a known, you only need to store the F1
s. The F2
s can be recalculated trivially using:
int f2 = n / f1;