Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artemis: Refactor Primes Class for Enhanced Efficiency and Clarity #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 22 additions & 11 deletions app/src/main/java/algorithms/Primes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package algorithms;
import java.util.ArrayList;

import java.util.Vector;
import java.util.ArrayList;

public class Primes {
/**
Expand All @@ -13,8 +14,14 @@ public static boolean IsPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i < n; i++) {
if (n % i == 0 && i != n) {
if (n == 2) {
return true;
}
if (n % 2 == 0) {
return false;
}
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
return false;
}
}
Expand All @@ -29,9 +36,9 @@ public static boolean IsPrime(int n) {
*/
public static int SumPrimes(int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
for (int i = 2; i < n; i++) {
if (IsPrime(i)) {
sum = sum + i;
sum += i;
}
}
return sum;
Expand All @@ -41,16 +48,20 @@ public static int SumPrimes(int n) {
* Finds all primes factors of a number
*
* @param n The number to find the prime factors of.
* @return An vector of all prime factors of n.
* @return An ArrayList of all prime factors of n.
*/
public static Vector<Integer> PrimeFactors(int n) {
Vector<Integer> ret = new Vector<Integer>();
public static ArrayList<Integer> PrimeFactors(int n) {
ArrayList<Integer> ret = new ArrayList<>();

for (int i = 2; i < n; i++) {
if (n % i == 0 && IsPrime(i)) {
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret.add(i);
n /= i;
}
}
if (n > 1) {
ret.add(n);
}
return ret;
}
}
}
Empty file modified gradlew
100755 → 100644
Empty file.