Skip to content
Open
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
14 changes: 10 additions & 4 deletions app/src/main/java/algorithms/Primes.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package algorithms;
import java.util.List;
import java.util.ArrayList;
import java.util.Vector;

public class Primes {
Expand Down Expand Up @@ -28,11 +30,15 @@ public static boolean IsPrime(int n) {
*/
public static int SumPrimes(int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
List<Integer> primes = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (IsPrime(i)) {
sum = sum + i;
primes.add(i);
}
}
for (int p : primes) {
sum += p;
}
return sum;
}

Expand All @@ -42,8 +48,8 @@ public static int SumPrimes(int n) {
* @param n The number to find the prime factors of.
* @return An vector of all prime factors of n.
*/
public static Vector<Integer> PrimeFactors(int n) {
Vector<Integer> ret = new Vector<Integer>();
public static List<Integer> PrimeFactors(int n) {
List<Integer> ret = new ArrayList<>();

for (int i = 2; i * i <= n; i++) { // Optimized loop condition
while (n % i == 0 && IsPrime(i)) { // Optimized to handle repeated factors
Expand Down