I have an algorithm that uses a List in Java that is declared like this:
public static List<Integer> primeFactors(int numbers) {
int n = numbers;
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
if (n > 1) {
factors.add(n);
}
return factors;
}
What I would like to be able to do is to take the primes returned by this function and add them. I know I can use
for (Integer integer : primeFactors(NUMBER))
to do something with every prime added to the List, so I am guessing the answer will be something along these lines when I add them. Is something like this possible?
Thanks for any and all help.
So basically have a variable outside the loop, and then you can do something like this:
At the end of that loop, sum will contain the number you want!