diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index ad37b787..903afbaf 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -23,7 +23,7 @@ public class Average { } /** - * find average value of int array + * find average value of an int array * * @param numbers the array contains element and the sum does not excess long * value limit diff --git a/src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java b/src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java index 401bedbc..79b0dafa 100644 --- a/src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java +++ b/src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java @@ -4,7 +4,7 @@ import java.util.*; /* * @author Ojasva Jain - * Determinant of Matrix Wikipedia link : https://en.wikipedia.org/wiki/Determinant + * Determinant of a Matrix Wikipedia link: https://en.wikipedia.org/wiki/Determinant */ public class DeterminantOfMatrix { diff --git a/src/main/java/com/thealgorithms/maths/DigitalRoot.java b/src/main/java/com/thealgorithms/maths/DigitalRoot.java index 78066f40..9eeb4a65 100644 --- a/src/main/java/com/thealgorithms/maths/DigitalRoot.java +++ b/src/main/java/com/thealgorithms/maths/DigitalRoot.java @@ -51,17 +51,17 @@ class DigitalRoot { } } - // This function is used for finding the sum of digits of number + // This function is used for finding the sum of the digits of number public static int single(int n) { if (n <= 9) { // if n becomes less than 10 than return n return n; } else { return (n % 10) + single(n / 10); // n % 10 for extracting digits one by one } - } // n / 10 is the number obtainded after removing the digit one by one - // Sum of digits is stored in the Stack memory and then finally returned + } // n / 10 is the number obtained after removing the digit one by one + // The Sum of digits is stored in the Stack memory and then finally returned } /** - * Time Complexity : O((Number of Digits)^2) Auxiliary Space Complexity : - * O(Number of Digits) Constraints : 1 <= n <= 10^7 + * Time Complexity: O((Number of Digits)^2) Auxiliary Space Complexity: + * O(Number of Digits) Constraints: 1 <= n <= 10^7 */ diff --git a/src/main/java/com/thealgorithms/maths/DistanceFormula.java b/src/main/java/com/thealgorithms/maths/DistanceFormula.java index 9a2654db..6efc2fbd 100644 --- a/src/main/java/com/thealgorithms/maths/DistanceFormula.java +++ b/src/main/java/com/thealgorithms/maths/DistanceFormula.java @@ -16,7 +16,7 @@ public class DistanceFormula { int d = 0; if (b1.length != b2.length) { - return -1; // error, both array must be have the same length + return -1; // error, both arrays must have the same length } for (int i = 0; i < b1.length; i++) { @@ -31,7 +31,7 @@ public class DistanceFormula { double distance = 0.0; if (p1.length != p2.length) { - return -1; // error, both array must be have the same length + return -1; // error, both arrays must have the same length } for (int i = 0; i < p1.length; i++) { diff --git a/src/main/java/com/thealgorithms/maths/DudeneyNumber.java b/src/main/java/com/thealgorithms/maths/DudeneyNumber.java index 69231af7..3bb56c5c 100644 --- a/src/main/java/com/thealgorithms/maths/DudeneyNumber.java +++ b/src/main/java/com/thealgorithms/maths/DudeneyNumber.java @@ -16,21 +16,21 @@ public class DudeneyNumber { if (cube_root * cube_root * cube_root != n) { return false; } - int sum_of_digits = 0; // Stores the sums of the digit of the entered number + int sum_of_digits = 0; // Stores the sums of the digits of the entered number int temp = n; // A temporary variable to store the entered number - // Loop to calculate sum of the digits. + // Loop to calculate the sum of the digits. while (temp > 0) { - // Extracting Last digit of the number + // Extracting the Last digit of the number int rem = temp % 10; - // Calculating sum of digits. + // Calculating the sum of digits. sum_of_digits += rem; // Removing the last digit temp /= 10; } - // If the cube root of the number is not equal to the sum of its digits we return false. + // If the cube root of the number is not equal to the sum of its digits, we return false. return cube_root == sum_of_digits; } } diff --git a/src/main/java/com/thealgorithms/maths/EulerMethod.java b/src/main/java/com/thealgorithms/maths/EulerMethod.java index 336e23dd..40e65462 100644 --- a/src/main/java/com/thealgorithms/maths/EulerMethod.java +++ b/src/main/java/com/thealgorithms/maths/EulerMethod.java @@ -87,7 +87,7 @@ public class EulerMethod { double xCurrent = xStart; while (xCurrent < xEnd) { - // Euler method for next step + // Euler's method for next step yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation); xCurrent += stepSize; double[] point = {xCurrent, yCurrent}; diff --git a/src/main/java/com/thealgorithms/maths/FindKthNumber.java b/src/main/java/com/thealgorithms/maths/FindKthNumber.java index d869ca47..bcb83b5e 100644 --- a/src/main/java/com/thealgorithms/maths/FindKthNumber.java +++ b/src/main/java/com/thealgorithms/maths/FindKthNumber.java @@ -11,7 +11,7 @@ public class FindKthNumber { private static final Random random = new Random(); public static void main(String[] args) { - /* generate array with random size and random elements */ + /* generate an array with random size and random elements */ int[] nums = generateArray(100); /* get 3th largest element */ diff --git a/src/main/java/com/thealgorithms/maths/FindMax.java b/src/main/java/com/thealgorithms/maths/FindMax.java index 76bde5ff..0ff2bdd1 100644 --- a/src/main/java/com/thealgorithms/maths/FindMax.java +++ b/src/main/java/com/thealgorithms/maths/FindMax.java @@ -12,11 +12,12 @@ public final class FindMax { * @return the maximum value stored in the input array */ public static int findMax(final int[] array) { - if (array.length == 0) { - throw new IllegalArgumentException("array must be non-empty."); + int n = array.length; + if (n == 0) { + throw new IllegalArgumentException("Array must be non-empty."); } int max = array[0]; - for (int i = 1; i < array.length; i++) { + for (int i = 1; i < n; i++) { if (array[i] > max) { max = array[i]; } diff --git a/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java b/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java index 574fe5f2..950a0ebe 100644 --- a/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java +++ b/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java @@ -5,7 +5,7 @@ public final class FindMaxRecursion { private FindMaxRecursion() { } /** - * Get max of array using divide and conquer algorithm + * Get max of an array using divide and conquer algorithm * * @param array contains elements * @param low the index of the first element @@ -14,7 +14,7 @@ public final class FindMaxRecursion { */ public static int max(final int[] array, final int low, final int high) { if (array.length == 0) { - throw new IllegalArgumentException("array must be non-empty."); + throw new IllegalArgumentException("Array must be non-empty."); } if (low == high) { return array[low]; // or array[high] @@ -25,11 +25,11 @@ public final class FindMaxRecursion { int leftMax = max(array, low, mid); // get max in [low, mid] int rightMax = max(array, mid + 1, high); // get max in [mid+1, high] - return leftMax < rightMax ? rightMax : leftMax; + return Math.max(leftMax, rightMax); } /** - * Get max of array using recursion algorithm + * Get max of an array using recursion algorithm * * @param array contains elements * @return max value of {@code array} diff --git a/src/main/java/com/thealgorithms/maths/FindMin.java b/src/main/java/com/thealgorithms/maths/FindMin.java index 10a03c80..76fa2e81 100644 --- a/src/main/java/com/thealgorithms/maths/FindMin.java +++ b/src/main/java/com/thealgorithms/maths/FindMin.java @@ -13,7 +13,7 @@ public final class FindMin { */ public static int findMin(final int[] array) { if (array.length == 0) { - throw new IllegalArgumentException("array must be non-empty."); + throw new IllegalArgumentException("Array must be non-empty."); } int min = array[0]; for (int i = 1; i < array.length; i++) { diff --git a/src/main/java/com/thealgorithms/maths/FindMinRecursion.java b/src/main/java/com/thealgorithms/maths/FindMinRecursion.java index 6ffe034e..a2cf1b36 100644 --- a/src/main/java/com/thealgorithms/maths/FindMinRecursion.java +++ b/src/main/java/com/thealgorithms/maths/FindMinRecursion.java @@ -4,6 +4,16 @@ public final class FindMinRecursion { private FindMinRecursion() { } + + /** + * Get min of an array using divide and conquer algorithm + * + * @param array contains elements + * @param low the index of the first element + * @param high the index of the last element + * @return min of {@code array} + */ + public static int min(final int[] array, final int low, final int high) { if (array.length == 0) { throw new IllegalArgumentException("array must be non-empty."); @@ -17,14 +27,13 @@ public final class FindMinRecursion { int leftMin = min(array, low, mid); // get min in [low, mid] int rightMin = min(array, mid + 1, high); // get min in [mid+1, high] - return leftMin > rightMin ? rightMin : leftMin; + return Math.min(leftMin, rightMin); } /** - * Get min of array using recursion algorithm + * Get min of an array using recursion algorithm * * @param array contains elements - * @param len length of given array * @return min value of {@code array} */ public static int min(final int[] array) { diff --git a/src/main/java/com/thealgorithms/maths/GCD.java b/src/main/java/com/thealgorithms/maths/GCD.java index 3a69fcab..0f3125bd 100644 --- a/src/main/java/com/thealgorithms/maths/GCD.java +++ b/src/main/java/com/thealgorithms/maths/GCD.java @@ -1,15 +1,15 @@ package com.thealgorithms.maths; /** - * This is Euclid's algorithm which is used to find the greatest common - * denominator Overide function name gcd + * This is Euclid's algorithm, used to find the greatest common + * denominator Override function name gcd * * @author Oskar Enmalm 3/10/17 */ public class GCD { /** - * get greatest common divisor + * get the greatest common divisor * * @param num1 the first number * @param num2 the second number diff --git a/src/main/java/com/thealgorithms/maths/Gaussian.java b/src/main/java/com/thealgorithms/maths/Gaussian.java index fd0f86d8..442c51e9 100644 --- a/src/main/java/com/thealgorithms/maths/Gaussian.java +++ b/src/main/java/com/thealgorithms/maths/Gaussian.java @@ -38,7 +38,7 @@ public class Gaussian { return mat; } - // calculate the x_1, x_2,... values of the gaussian and save it in an arraylist. + // calculate the x_1, x_2, ... values of the gaussian and save it in an arraylist. public static ArrayList valueOfGaussian(int mat_size, double[][] x, double[][] mat) { ArrayList answerArray = new ArrayList(); int i, j;