Skip to content
Draft
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
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Gradle
.gradle/
build/
app/build/

# IDE
.classpath
.project
.settings/
.idea/
*.iml

# OS
.DS_Store
9 changes: 5 additions & 4 deletions app/src/main/java/algorithms/Primes.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public static int SumPrimes(int n) {
public static Vector<Integer> PrimeFactors(int n) {
Vector<Integer> ret = new Vector<Integer>();

for (int i = 2; i * i <= n; i++) { // Optimized loop condition
while (n % i == 0 && IsPrime(i)) { // Optimized to handle repeated factors
// No need to check IsPrime - i is already a factor
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret.add(i);
n /= i; // Reduce n to avoid redundant checks.
n /= i;
}
}
if (n > 1) { // Add any remaining prime factor.
if (n > 1) {
ret.add(n);
}
return ret;
Expand Down
45 changes: 22 additions & 23 deletions app/src/main/java/control/Double.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package control;

import java.util.HashMap;

public class Double {
/**
* Sums all values squared from 0 to n
Expand All @@ -8,13 +10,10 @@ public class Double {
* @return The sum of the first n natural numbers squared.
*/
public static int sumSquare(int n) {
// Simplified: sum of i^2 from 0 to n-1
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
sum = sum + i * j;
}
}
sum += i * i;
}
return sum;
}
Expand All @@ -26,11 +25,11 @@ public static int sumSquare(int n) {
* @return The sum of the first n triangular numbers.
*/
public static int sumTriangle(int n) {
// Triangular number T(i) = i*(i-1)/2
// Sum of T(1) to T(n) can be calculated efficiently
int sum = 0;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < i; j++) {
sum = sum + j;
}
for (int i = 1; i <= n; i++) {
sum += i * (i - 1) / 2;
}
return sum;
}
Expand All @@ -44,19 +43,19 @@ public static int sumTriangle(int n) {
* @return The number of pairs in the array.
*/
public static int countPairs(int[] arr) {
// Use HashMap to count occurrences in O(n) time
HashMap<Integer, Integer> counts = new HashMap<>();
for (int num : arr) {
counts.put(num, counts.getOrDefault(num, 0) + 1);
}

int count = 0;
for (int i = 0; i < arr.length; i++) {
int nDuplicates = 0;
for (int j = 0; j < arr.length; j++) {
if (arr[i] == arr[j]) {
nDuplicates++;
}
}
if (nDuplicates == 2) {
for (int occurrences : counts.values()) {
if (occurrences == 2) {
count++;
}
}
return count / 2;
return count;
}

/**
Expand All @@ -68,12 +67,12 @@ public static int countPairs(int[] arr) {
* equal.
*/
public static int countDuplicates(int[] arr0, int[] arr1) {
// Single pass comparison - no nested loop needed
int count = 0;
for (int i = 0; i < arr0.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (i == j && arr0[i] == arr1[j]) {
count++;
}
int minLength = Math.min(arr0.length, arr1.length);
for (int i = 0; i < minLength; i++) {
if (arr0[i] == arr1[i]) {
count++;
}
}
return count;
Expand Down
23 changes: 7 additions & 16 deletions app/src/main/java/control/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ public class Single {
* @return The sum of the first n natural numbers.
*/
public static int sumRange(int n) {
int[] arr = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = i;
}
for (int i : arr) {
sum += i;
}
return sum;
// Use arithmetic formula: sum of 0 to n-1 = n*(n-1)/2
return n * (n - 1) / 2;
}

/**
Expand All @@ -45,13 +38,11 @@ public static int maxArray(int[] arr) {
* @param m The modulus.
*/
public static int sumModulus(int n, int m) {
Vector<Integer> multiples = new Vector<Integer>();
for (int i = 0; i < n; i++) {
if (i % m == 0) {
multiples.add(i);
}
// Calculate sum directly without storing all multiples
int sum = 0;
for (int i = 0; i < n; i += m) {
sum += i;
}

return multiples.stream().mapToInt(Integer::valueOf).sum();
return sum;
}
}
13 changes: 3 additions & 10 deletions app/src/main/java/datastructures/DsVector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datastructures;

import java.util.Collections;
import java.util.Vector;

public class DsVector {
Expand Down Expand Up @@ -40,16 +41,8 @@ public static Vector<Integer> searchVector(Vector<Integer> v, int n) {
*/
public static Vector<Integer> sortVector(Vector<Integer> v) {
Vector<Integer> ret = new Vector<Integer>(v);

for (int i = 0; i < ret.size(); i++) {
for (int j = 0; j < ret.size() - 1; j++) {
if (ret.get(j) > ret.get(j + 1)) {
int temp = ret.get(j);
ret.set(j, ret.get(j + 1));
ret.set(j + 1, temp);
}
}
}
// Use efficient built-in sort instead of bubble sort
Collections.sort(ret);
return ret;
}

Expand Down
7 changes: 2 additions & 5 deletions app/src/main/java/strings/Strops.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ public class Strops {
* @return The reversed string.
*/
public String reverse(String str) {
StringBuilder reversed = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversed.append(str.charAt(i));
}
return reversed.toString();
// Use StringBuilder's built-in reverse method
return new StringBuilder(str).reverse().toString();
}

/**
Expand Down