Skip to content
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
32 changes: 7 additions & 25 deletions app/src/main/java/control/Single.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package control;
import java.util.stream.IntStream;
import java.util.Arrays;

import java.util.Vector;


public class Single {
/**
* This method is used to calculate the sum of the first n natural numbers.
Expand All @@ -11,15 +14,7 @@ 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;
return (n * (n - 1)) / 2;
}

/**
Expand All @@ -29,13 +24,7 @@ public static int sumRange(int n) {
* @return The maximum value in the array.
*/
public static int maxArray(int[] arr) {
int max = 0;
for (int i : arr) {
if (i > max) {
max = i;
}
}
return max;
return Arrays.stream(arr).max().getAsInt();
}

/**
Expand All @@ -45,13 +34,6 @@ 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);
}
}

return multiples.stream().mapToInt(Integer::valueOf).sum();
return IntStream.range(0, n).filter(i -> i % m == 0).sum();
}
}
}
Empty file modified gradlew
100755 → 100644
Empty file.