- * E.g. num = 13 = 00001101 in binary log2N = 8 Then reversed = 176 =
+ * E.g. num = 13 = 00001101 in binary log2n = 8 Then reversed = 176 =
* 10110000 in binary
*
*
@@ -252,14 +255,14 @@ public static ArrayList inverseFFT(int N, boolean inverse, ArrayList x, boolean inverse) {
- int N = x.size();
- int bnSize = 2 * N - 1;
+ int n = x.size();
+ int bnSize = 2 * n - 1;
int direction = inverse ? -1 : 1;
ArrayList an = new ArrayList<>();
ArrayList bn = new ArrayList<>();
@@ -36,32 +38,32 @@ public static void fftBluestein(ArrayList x, boolean inverse) {
bn.add(new FFT.Complex());
}
- for (int i = 0; i < N; i++) {
- double angle = (i - N + 1) * (i - N + 1) * Math.PI / N * direction;
+ for (int i = 0; i < n; i++) {
+ double angle = (i - n + 1) * (i - n + 1) * Math.PI / n * direction;
bn.set(i, new FFT.Complex(Math.cos(angle), Math.sin(angle)));
bn.set(bnSize - i - 1, new FFT.Complex(Math.cos(angle), Math.sin(angle)));
}
/* Initialization of the a(n) sequence */
- for (int i = 0; i < N; i++) {
- double angle = -i * i * Math.PI / N * direction;
+ for (int i = 0; i < n; i++) {
+ double angle = -i * i * Math.PI / n * direction;
an.add(x.get(i).multiply(new FFT.Complex(Math.cos(angle), Math.sin(angle))));
}
ArrayList convolution = ConvolutionFFT.convolutionFFT(an, bn);
/* The final multiplication of the convolution with the b*(k) factor */
- for (int i = 0; i < N; i++) {
- double angle = -1 * i * i * Math.PI / N * direction;
+ for (int i = 0; i < n; i++) {
+ double angle = -1 * i * i * Math.PI / n * direction;
FFT.Complex bk = new FFT.Complex(Math.cos(angle), Math.sin(angle));
- x.set(i, bk.multiply(convolution.get(i + N - 1)));
+ x.set(i, bk.multiply(convolution.get(i + n - 1)));
}
- /* Divide by N if we want the inverse FFT */
+ /* Divide by n if we want the inverse FFT */
if (inverse) {
- for (int i = 0; i < N; i++) {
+ for (int i = 0; i < n; i++) {
FFT.Complex z = x.get(i);
- x.set(i, z.divide(N));
+ x.set(i, z.divide(n));
}
}
}
diff --git a/src/main/java/com/thealgorithms/maths/FactorialRecursion.java b/src/main/java/com/thealgorithms/maths/FactorialRecursion.java
index 85e03c4dd1a4..d9bafd1e39e9 100644
--- a/src/main/java/com/thealgorithms/maths/FactorialRecursion.java
+++ b/src/main/java/com/thealgorithms/maths/FactorialRecursion.java
@@ -1,16 +1,8 @@
package com.thealgorithms.maths;
-public class FactorialRecursion {
-
- /* Driver Code */
- public static void main(String[] args) {
- assert factorial(0) == 1;
- assert factorial(1) == 1;
- assert factorial(2) == 2;
- assert factorial(3) == 6;
- assert factorial(5) == 120;
+public final class FactorialRecursion {
+ private FactorialRecursion() {
}
-
/**
* Recursive FactorialRecursion Method
*
diff --git a/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java b/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
index 886337478d01..a0dbfb1f70a4 100644
--- a/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
+++ b/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
@@ -9,7 +9,9 @@
package com.thealgorithms.maths;
-public class FastInverseSqrt {
+public final class FastInverseSqrt {
+ private FastInverseSqrt() {
+ }
public static boolean inverseSqrt(float number) {
float x = number;
diff --git a/src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java b/src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java
index a9663d39a988..72bae57c27b0 100644
--- a/src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java
+++ b/src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java
@@ -9,7 +9,9 @@
* @author: caos321
* @date: 14 October 2021 (Thursday)
*/
-public class FibonacciJavaStreams {
+public final class FibonacciJavaStreams {
+ private FibonacciJavaStreams() {
+ }
public static Optional calculate(final BigDecimal index) {
if (index == null || index.compareTo(BigDecimal.ZERO) < 0) {
diff --git a/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java b/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java
index 937786546fc3..781275d3130d 100644
--- a/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java
+++ b/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java
@@ -5,7 +5,9 @@
* This code checks Fibonacci Numbers up to 45th number.
* Other checks fail because of 'long'-type overflow.
*/
-public class FibonacciNumberCheck {
+public final class FibonacciNumberCheck {
+ private FibonacciNumberCheck() {
+ }
/**
* Check if a number is perfect square number
*
diff --git a/src/main/java/com/thealgorithms/maths/FindKthNumber.java b/src/main/java/com/thealgorithms/maths/FindKthNumber.java
index bcb83b5ee2fc..daea3f96332b 100644
--- a/src/main/java/com/thealgorithms/maths/FindKthNumber.java
+++ b/src/main/java/com/thealgorithms/maths/FindKthNumber.java
@@ -6,9 +6,11 @@
/**
* use quick sort algorithm to get kth largest or kth smallest element in given array
*/
-public class FindKthNumber {
+public final class FindKthNumber {
+ private FindKthNumber() {
+ }
- private static final Random random = new Random();
+ private static final Random RANDOM = new Random();
public static void main(String[] args) {
/* generate an array with random size and random elements */
@@ -29,17 +31,18 @@ public static void main(String[] args) {
}
private static int[] generateArray(int capacity) {
- int size = random.nextInt(capacity) + 1;
+ int size = RANDOM.nextInt(capacity) + 1;
int[] array = new int[size];
for (int i = 0; i < size; i++) {
- array[i] = random.nextInt() % 100;
+ array[i] = RANDOM.nextInt() % 100;
}
return array;
}
private static int findKthMax(int[] nums, int k) {
- int start = 0, end = nums.length;
+ int start = 0;
+ int end = nums.length;
while (start < end) {
int pivot = partition(nums, start, end);
if (k == pivot) {
diff --git a/src/main/java/com/thealgorithms/maths/FrizzyNumber.java b/src/main/java/com/thealgorithms/maths/FrizzyNumber.java
index 3b1ff5fde3b3..3ae5e021df1b 100644
--- a/src/main/java/com/thealgorithms/maths/FrizzyNumber.java
+++ b/src/main/java/com/thealgorithms/maths/FrizzyNumber.java
@@ -7,7 +7,9 @@
package com.thealgorithms.maths;
-public class FrizzyNumber {
+public final class FrizzyNumber {
+ private FrizzyNumber() {
+ }
/**
* Returns the n-th number that is a sum of powers
diff --git a/src/main/java/com/thealgorithms/maths/GCD.java b/src/main/java/com/thealgorithms/maths/GCD.java
index 0f3125bde209..5156e4ac881d 100644
--- a/src/main/java/com/thealgorithms/maths/GCD.java
+++ b/src/main/java/com/thealgorithms/maths/GCD.java
@@ -6,7 +6,9 @@
*
* @author Oskar Enmalm 3/10/17
*/
-public class GCD {
+public final class GCD {
+ private GCD() {
+ }
/**
* get the greatest common divisor
diff --git a/src/main/java/com/thealgorithms/maths/GCDRecursion.java b/src/main/java/com/thealgorithms/maths/GCDRecursion.java
index 05e44f941ac7..e95ce97c8a04 100644
--- a/src/main/java/com/thealgorithms/maths/GCDRecursion.java
+++ b/src/main/java/com/thealgorithms/maths/GCDRecursion.java
@@ -3,7 +3,9 @@
/**
* @author https://github.com/shellhub/
*/
-public class GCDRecursion {
+public final class GCDRecursion {
+ private GCDRecursion() {
+ }
public static void main(String[] args) {
System.out.println(gcd(20, 15));
diff --git a/src/main/java/com/thealgorithms/maths/Gaussian.java b/src/main/java/com/thealgorithms/maths/Gaussian.java
index 442c51e9d32d..cefbaea5b9b4 100644
--- a/src/main/java/com/thealgorithms/maths/Gaussian.java
+++ b/src/main/java/com/thealgorithms/maths/Gaussian.java
@@ -2,35 +2,38 @@
import java.util.ArrayList;
-public class Gaussian {
+public final class Gaussian {
+ private Gaussian() {
+ }
- public static ArrayList gaussian(int mat_size, ArrayList matrix) {
+ public static ArrayList gaussian(int matSize, ArrayList matrix) {
ArrayList answerArray = new ArrayList();
- int i, j = 0;
+ int i;
+ int j = 0;
- double[][] mat = new double[mat_size + 1][mat_size + 1];
- double[][] x = new double[mat_size][mat_size + 1];
+ double[][] mat = new double[matSize + 1][matSize + 1];
+ double[][] x = new double[matSize][matSize + 1];
// Values from arraylist to matrix
- for (i = 0; i < mat_size; i++) {
- for (j = 0; j <= mat_size; j++) {
+ for (i = 0; i < matSize; i++) {
+ for (j = 0; j <= matSize; j++) {
mat[i][j] = matrix.get(i);
}
}
- mat = gaussianElimination(mat_size, i, mat);
- answerArray = valueOfGaussian(mat_size, x, mat);
+ mat = gaussianElimination(matSize, i, mat);
+ answerArray = valueOfGaussian(matSize, x, mat);
return answerArray;
}
// Perform Gaussian elimination
- public static double[][] gaussianElimination(int mat_size, int i, double[][] mat) {
+ public static double[][] gaussianElimination(int matSize, int i, double[][] mat) {
int step = 0;
- for (step = 0; step < mat_size - 1; step++) {
- for (i = step; i < mat_size - 1; i++) {
+ for (step = 0; step < matSize - 1; step++) {
+ for (i = step; i < matSize - 1; i++) {
double a = (mat[i + 1][step] / mat[step][step]);
- for (int j = step; j <= mat_size; j++) {
+ for (int j = step; j <= matSize; j++) {
mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]);
}
}
@@ -39,26 +42,27 @@ public static double[][] gaussianElimination(int mat_size, int i, double[][] mat
}
// 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) {
+ public static ArrayList valueOfGaussian(int matSize, double[][] x, double[][] mat) {
ArrayList answerArray = new ArrayList();
- int i, j;
+ int i;
+ int j;
- for (i = 0; i < mat_size; i++) {
- for (j = 0; j <= mat_size; j++) {
+ for (i = 0; i < matSize; i++) {
+ for (j = 0; j <= matSize; j++) {
x[i][j] = mat[i][j];
}
}
- for (i = mat_size - 1; i >= 0; i--) {
+ for (i = matSize - 1; i >= 0; i--) {
double sum = 0;
- for (j = mat_size - 1; j > i; j--) {
+ for (j = matSize - 1; j > i; j--) {
x[i][j] = x[j][j] * x[i][j];
sum = x[i][j] + sum;
}
if (x[i][i] == 0) {
x[i][i] = 0;
} else {
- x[i][i] = (x[i][mat_size] - sum) / (x[i][i]);
+ x[i][i] = (x[i][matSize] - sum) / (x[i][i]);
}
answerArray.add(x[i][j]);
}
diff --git a/src/main/java/com/thealgorithms/maths/HarshadNumber.java b/src/main/java/com/thealgorithms/maths/HarshadNumber.java
index 4778dc81b664..0b1ba1285c4d 100644
--- a/src/main/java/com/thealgorithms/maths/HarshadNumber.java
+++ b/src/main/java/com/thealgorithms/maths/HarshadNumber.java
@@ -2,7 +2,9 @@
// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
-public class HarshadNumber {
+public final class HarshadNumber {
+ private HarshadNumber() {
+ }
/**
* A function to check if a number is Harshad number or not
@@ -32,7 +34,7 @@ public static boolean isHarshad(long n) {
* {@code false}
*/
public static boolean isHarshad(String s) {
- long n = Long.valueOf(s);
+ final Long n = Long.valueOf(s);
if (n <= 0) return false;
int sumOfDigits = 0;
diff --git a/src/main/java/com/thealgorithms/maths/JosephusProblem.java b/src/main/java/com/thealgorithms/maths/JosephusProblem.java
index b878eff2b291..7d19623b3ed0 100644
--- a/src/main/java/com/thealgorithms/maths/JosephusProblem.java
+++ b/src/main/java/com/thealgorithms/maths/JosephusProblem.java
@@ -20,7 +20,9 @@
@author Kunal
*/
-public class JosephusProblem {
+public final class JosephusProblem {
+ private JosephusProblem() {
+ }
/**
* Find the Winner of the Circular Game.
diff --git a/src/main/java/com/thealgorithms/maths/JugglerSequence.java b/src/main/java/com/thealgorithms/maths/JugglerSequence.java
index 216098fc926f..702310a1f295 100644
--- a/src/main/java/com/thealgorithms/maths/JugglerSequence.java
+++ b/src/main/java/com/thealgorithms/maths/JugglerSequence.java
@@ -11,7 +11,9 @@
*
* */
-public class JugglerSequence {
+public final class JugglerSequence {
+ private JugglerSequence() {
+ }
/**
* This method prints juggler sequence starting with the number in the parameter
diff --git a/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java b/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
index d2283dc10214..f025f86682a2 100644
--- a/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
+++ b/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
@@ -1,9 +1,12 @@
package com.thealgorithms.maths;
import java.math.BigInteger;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
-public class KaprekarNumbers {
+public final class KaprekarNumbers {
+ private KaprekarNumbers() {
+ }
/* This program demonstrates if a given number is Kaprekar Number or not.
Kaprekar Number: A Kaprekar number is an n-digit number which its square can be split into
diff --git a/src/main/java/com/thealgorithms/maths/KeithNumber.java b/src/main/java/com/thealgorithms/maths/KeithNumber.java
index 1db9f9500ed1..1756cfbae91b 100644
--- a/src/main/java/com/thealgorithms/maths/KeithNumber.java
+++ b/src/main/java/com/thealgorithms/maths/KeithNumber.java
@@ -1,15 +1,20 @@
package com.thealgorithms.maths;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Scanner;
-class KeithNumber {
+final class KeithNumber {
+ private KeithNumber() {
+ }
// user-defined function that checks if the given number is Keith or not
static boolean isKeith(int x) {
// List stores all the digits of the X
- ArrayList terms = new ArrayList();
+ ArrayList terms = new ArrayList<>();
// n denotes the number of digits
- int temp = x, n = 0;
+ int temp = x;
+ int n = 0;
// executes until the condition becomes false
while (temp > 0) {
// determines the last digit of the number and add it to the List
@@ -21,23 +26,24 @@ static boolean isKeith(int x) {
}
// reverse the List
Collections.reverse(terms);
- int next_term = 0, i = n;
+ int nextTerm = 0;
+ int i = n;
// finds next term for the series
// loop executes until the condition returns true
- while (next_term < x) {
- next_term = 0;
+ while (nextTerm < x) {
+ nextTerm = 0;
// next term is the sum of previous n terms (it depends on number of digits the number
// has)
for (int j = 1; j <= n; j++) {
- next_term = next_term + terms.get(i - j);
+ nextTerm = nextTerm + terms.get(i - j);
}
- terms.add(next_term);
+ terms.add(nextTerm);
i++;
}
// when the control comes out of the while loop, there will be two conditions:
- // either next_term will be equal to x or greater than x
+ // either nextTerm will be equal to x or greater than x
// if equal, the given number is Keith, else not
- return (next_term == x);
+ return (nextTerm == x);
}
// driver code
diff --git a/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java b/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java
index eacc75c23058..f5ff50337bc7 100644
--- a/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java
+++ b/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java
@@ -5,9 +5,13 @@
to the number itself. For example, 1, 2 and 145 are Krishnamurthy numbers. Krishnamurthy number is
also referred to as a Strong number.
*/
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
-public class KrishnamurthyNumber {
+public final class KrishnamurthyNumber {
+ private KrishnamurthyNumber() {
+ }
// returns True if the number is a Krishnamurthy number and False if it is not.
diff --git a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java
index f4f7c94aa3e2..db79340f0a99 100644
--- a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java
+++ b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java
@@ -1,6 +1,6 @@
package com.thealgorithms.maths;
-import java.util.*;
+import java.util.Scanner;
/**
* Is a common mathematics concept to find the smallest value number
@@ -9,7 +9,9 @@
* @author LauKinHoong
*/
-public class LeastCommonMultiple {
+public final class LeastCommonMultiple {
+ private LeastCommonMultiple() {
+ }
/**
* Driver Code
@@ -28,15 +30,18 @@ public static void main(String[] args) {
* get least common multiple from two number
*/
public static int lcm(int num1, int num2) {
- int high, num3;
+ int high;
+ int num3;
int cmv = 0;
/*
* value selection for the numerator
*/
if (num1 > num2) {
- high = num3 = num1;
+ high = num1;
+ num3 = num1;
} else {
- high = num3 = num2;
+ high = num2;
+ num3 = num2;
}
while (num1 != 0) {
diff --git a/src/main/java/com/thealgorithms/maths/LeonardoNumber.java b/src/main/java/com/thealgorithms/maths/LeonardoNumber.java
index 1ca5c8bd0fa6..bbeec052777f 100644
--- a/src/main/java/com/thealgorithms/maths/LeonardoNumber.java
+++ b/src/main/java/com/thealgorithms/maths/LeonardoNumber.java
@@ -3,7 +3,9 @@
/**
* https://en.wikipedia.org/wiki/Leonardo_number
*/
-public class LeonardoNumber {
+public final class LeonardoNumber {
+ private LeonardoNumber() {
+ }
/**
* Calculate nth Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
diff --git a/src/main/java/com/thealgorithms/maths/LinearDiophantineEquationsSolver.java b/src/main/java/com/thealgorithms/maths/LinearDiophantineEquationsSolver.java
index 2d00cc5561e8..a50cfb218283 100644
--- a/src/main/java/com/thealgorithms/maths/LinearDiophantineEquationsSolver.java
+++ b/src/main/java/com/thealgorithms/maths/LinearDiophantineEquationsSolver.java
@@ -3,6 +3,8 @@
import java.util.Objects;
public final class LinearDiophantineEquationsSolver {
+ private LinearDiophantineEquationsSolver() {
+ }
public static void main(String[] args) {
// 3x + 4y = 7
diff --git a/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java b/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java
index 89acbf6a14e7..c0f55f5e3485 100644
--- a/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java
+++ b/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java
@@ -12,7 +12,9 @@
*
* */
-public class LiouvilleLambdaFunction {
+public final class LiouvilleLambdaFunction {
+ private LiouvilleLambdaFunction() {
+ }
/**
* This method returns λ(n) of given number n
diff --git a/src/main/java/com/thealgorithms/maths/LongDivision.java b/src/main/java/com/thealgorithms/maths/LongDivision.java
index b1c7b1901701..45e97b1c14c3 100644
--- a/src/main/java/com/thealgorithms/maths/LongDivision.java
+++ b/src/main/java/com/thealgorithms/maths/LongDivision.java
@@ -8,61 +8,63 @@
package com.thealgorithms.maths;
-public class LongDivision {
+public final class LongDivision {
+ private LongDivision() {
+ }
public static int divide(int dividend, int divisor) {
- long new_dividend_1 = dividend;
- long new_divisor_1 = divisor;
+ long newDividend1 = dividend;
+ long newDivisor1 = divisor;
if (divisor == 0) {
return 0;
}
if (dividend < 0) {
- new_dividend_1 = new_dividend_1 * -1;
+ newDividend1 = newDividend1 * -1;
}
if (divisor < 0) {
- new_divisor_1 = new_divisor_1 * -1;
+ newDivisor1 = newDivisor1 * -1;
}
- if (dividend == 0 || new_dividend_1 < new_divisor_1) {
+ if (dividend == 0 || newDividend1 < newDivisor1) {
return 0;
}
StringBuilder answer = new StringBuilder();
- String dividend_string = "" + new_dividend_1;
- int last_index = 0;
+ String dividendString = "" + newDividend1;
+ int lastIndex = 0;
String remainder = "";
- for (int i = 0; i < dividend_string.length(); i++) {
- String part_v1 = remainder + "" + dividend_string.substring(last_index, i + 1);
- long part_1 = Long.parseLong(part_v1);
- if (part_1 > new_divisor_1) {
+ for (int i = 0; i < dividendString.length(); i++) {
+ String partV1 = remainder + "" + dividendString.substring(lastIndex, i + 1);
+ long part1 = Long.parseLong(partV1);
+ if (part1 > newDivisor1) {
int quotient = 0;
- while (part_1 >= new_divisor_1) {
- part_1 = part_1 - new_divisor_1;
+ while (part1 >= newDivisor1) {
+ part1 = part1 - newDivisor1;
quotient++;
}
answer.append(quotient);
- } else if (part_1 == new_divisor_1) {
+ } else if (part1 == newDivisor1) {
int quotient = 0;
- while (part_1 >= new_divisor_1) {
- part_1 = part_1 - new_divisor_1;
+ while (part1 >= newDivisor1) {
+ part1 = part1 - newDivisor1;
quotient++;
}
answer.append(quotient);
- } else if (part_1 == 0) {
+ } else if (part1 == 0) {
answer.append(0);
- } else if (part_1 < new_divisor_1) {
+ } else if (part1 < newDivisor1) {
answer.append(0);
}
- if (!(part_1 == 0)) {
- remainder = String.valueOf(part_1);
+ if (!(part1 == 0)) {
+ remainder = String.valueOf(part1);
} else {
remainder = "";
}
- last_index++;
+ lastIndex++;
}
if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
diff --git a/src/main/java/com/thealgorithms/maths/LucasSeries.java b/src/main/java/com/thealgorithms/maths/LucasSeries.java
index ebeb2715fe2d..e277c511f317 100644
--- a/src/main/java/com/thealgorithms/maths/LucasSeries.java
+++ b/src/main/java/com/thealgorithms/maths/LucasSeries.java
@@ -3,7 +3,9 @@
/**
* https://en.wikipedia.org/wiki/Lucas_number
*/
-public class LucasSeries {
+public final class LucasSeries {
+ private LucasSeries() {
+ }
/**
* Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
diff --git a/src/main/java/com/thealgorithms/maths/MagicSquare.java b/src/main/java/com/thealgorithms/maths/MagicSquare.java
index 3bcede960346..de0afc148982 100644
--- a/src/main/java/com/thealgorithms/maths/MagicSquare.java
+++ b/src/main/java/com/thealgorithms/maths/MagicSquare.java
@@ -1,11 +1,13 @@
package com.thealgorithms.maths;
-import java.util.*;
+import java.util.Scanner;
/*A magic square of order n is an arrangement of distinct n^2 integers,in a square, such that the n
numbers in all rows, all columns, and both diagonals sum to the same constant. A magic square
contains the integers from 1 to n^2.*/
-public class MagicSquare {
+public final class MagicSquare {
+ private MagicSquare() {
+ }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
@@ -16,32 +18,32 @@ public static void main(String[] args) {
System.exit(0);
}
- int[][] magic_square = new int[num][num];
+ int[][] magicSquare = new int[num][num];
- int row_num = num / 2;
- int col_num = num - 1;
- magic_square[row_num][col_num] = 1;
+ int rowNum = num / 2;
+ int colNum = num - 1;
+ magicSquare[rowNum][colNum] = 1;
for (int i = 2; i <= num * num; i++) {
- if (magic_square[(row_num - 1 + num) % num][(col_num + 1) % num] == 0) {
- row_num = (row_num - 1 + num) % num;
- col_num = (col_num + 1) % num;
+ if (magicSquare[(rowNum - 1 + num) % num][(colNum + 1) % num] == 0) {
+ rowNum = (rowNum - 1 + num) % num;
+ colNum = (colNum + 1) % num;
} else {
- col_num = (col_num - 1 + num) % num;
+ colNum = (colNum - 1 + num) % num;
}
- magic_square[row_num][col_num] = i;
+ magicSquare[rowNum][colNum] = i;
}
// print the square
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
- if (magic_square[i][j] < 10) {
+ if (magicSquare[i][j] < 10) {
System.out.print(" ");
}
- if (magic_square[i][j] < 100) {
+ if (magicSquare[i][j] < 100) {
System.out.print(" ");
}
- System.out.print(magic_square[i][j] + " ");
+ System.out.print(magicSquare[i][j] + " ");
}
System.out.println();
}
diff --git a/src/main/java/com/thealgorithms/maths/MatrixUtil.java b/src/main/java/com/thealgorithms/maths/MatrixUtil.java
index f3d0efeacc45..0759853d61a9 100644
--- a/src/main/java/com/thealgorithms/maths/MatrixUtil.java
+++ b/src/main/java/com/thealgorithms/maths/MatrixUtil.java
@@ -11,7 +11,9 @@
* @author: caos321
* @date: 31 October 2021 (Sunday)
*/
-public class MatrixUtil {
+public final class MatrixUtil {
+ private MatrixUtil() {
+ }
public static boolean isValid(final BigDecimal[][] matrix) {
return matrix != null && matrix.length > 0 && matrix[0].length > 0;
diff --git a/src/main/java/com/thealgorithms/maths/Median.java b/src/main/java/com/thealgorithms/maths/Median.java
index 89bc42254d34..e4daec8fc11a 100644
--- a/src/main/java/com/thealgorithms/maths/Median.java
+++ b/src/main/java/com/thealgorithms/maths/Median.java
@@ -5,7 +5,9 @@
/**
* Wikipedia: https://en.wikipedia.org/wiki/Median
*/
-public class Median {
+public final class Median {
+ private Median() {
+ }
/**
* Calculate average median
diff --git a/src/main/java/com/thealgorithms/maths/MillerRabinPrimalityCheck.java b/src/main/java/com/thealgorithms/maths/MillerRabinPrimalityCheck.java
index ed4f325b0710..e25836f713a9 100644
--- a/src/main/java/com/thealgorithms/maths/MillerRabinPrimalityCheck.java
+++ b/src/main/java/com/thealgorithms/maths/MillerRabinPrimalityCheck.java
@@ -2,7 +2,9 @@
import java.util.Random;
-public class MillerRabinPrimalityCheck {
+public final class MillerRabinPrimalityCheck {
+ private MillerRabinPrimalityCheck() {
+ }
/**
* Check whether the given number is prime or not
diff --git a/src/main/java/com/thealgorithms/maths/MobiusFunction.java b/src/main/java/com/thealgorithms/maths/MobiusFunction.java
index e9ead992d7a7..915d0d9a6dae 100644
--- a/src/main/java/com/thealgorithms/maths/MobiusFunction.java
+++ b/src/main/java/com/thealgorithms/maths/MobiusFunction.java
@@ -12,7 +12,9 @@
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
*
* */
-public class MobiusFunction {
+public final class MobiusFunction {
+ private MobiusFunction() {
+ }
/**
* This method returns μ(n) of given number n
diff --git a/src/main/java/com/thealgorithms/maths/Mode.java b/src/main/java/com/thealgorithms/maths/Mode.java
index 7333380b0a69..a92f404c653a 100644
--- a/src/main/java/com/thealgorithms/maths/Mode.java
+++ b/src/main/java/com/thealgorithms/maths/Mode.java
@@ -1,7 +1,6 @@
package com.thealgorithms.maths;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -11,15 +10,8 @@
* The mode of an array of numbers is the most frequently occurring number in the array,
* or the most frequently occurring numbers if there are multiple numbers with the same frequency
*/
-public class Mode {
-
- public static void main(String[] args) {
- /* Test array of integers */
- assert (mode(new int[] {})) == null;
- assert Arrays.equals(mode(new int[] {5}), new int[] {5});
- assert Arrays.equals(mode(new int[] {1, 2, 3, 4, 5}), new int[] {1, 2, 3, 4, 5});
- assert Arrays.equals(mode(new int[] {7, 9, 9, 4, 5, 6, 7, 7, 8}), new int[] {7});
- assert Arrays.equals(mode(new int[] {7, 9, 9, 4, 5, 6, 7, 7, 9}), new int[] {7, 9});
+public final class Mode {
+ private Mode() {
}
/*
@@ -28,7 +20,7 @@ public static void main(String[] args) {
* @param numbers array of integers
* @return mode of the array
*/
- public static int[] mode(int[] numbers) {
+ public static int[] mode(final int[] numbers) {
if (numbers.length == 0) {
return null;
}
diff --git a/src/main/java/com/thealgorithms/maths/NonRepeatingElement.java b/src/main/java/com/thealgorithms/maths/NonRepeatingElement.java
index 01fdd5a6a5a5..5f1190d67de0 100644
--- a/src/main/java/com/thealgorithms/maths/NonRepeatingElement.java
+++ b/src/main/java/com/thealgorithms/maths/NonRepeatingElement.java
@@ -7,11 +7,14 @@
* Reason to use bitwise operator: It makes our program faster as we are operating on bits and not
* on actual numbers.
*/
-public class NonRepeatingElement {
+public final class NonRepeatingElement {
+ private NonRepeatingElement() {
+ }
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
- int i, res = 0;
+ int i;
+ int res = 0;
System.out.println("Enter the number of elements in the array");
int n = sc.nextInt();
if ((n & 1) == 1) {
@@ -27,12 +30,6 @@ public static void main(String[] args) {
arr[i] = sc.nextInt();
}
- try {
- sc.close();
- } catch (Exception e) {
- System.out.println("Unable to close scanner" + e);
- }
-
// Find XOR of the 2 non repeating elements
for (i = 0; i < n; i++) {
res ^= arr[i];
@@ -40,7 +37,8 @@ public static void main(String[] args) {
// Finding the rightmost set bit
res = res & (-res);
- int num1 = 0, num2 = 0;
+ int num1 = 0;
+ int num2 = 0;
for (i = 0; i < n; i++) {
if ((res & arr[i]) > 0) { // Case 1 explained below
@@ -51,7 +49,6 @@ public static void main(String[] args) {
}
System.out.println("The two non repeating elements are " + num1 + " and " + num2);
- sc.close();
}
}
/*
diff --git a/src/main/java/com/thealgorithms/maths/NumberOfDigits.java b/src/main/java/com/thealgorithms/maths/NumberOfDigits.java
index 665d8ef5a98c..fc538196c7da 100644
--- a/src/main/java/com/thealgorithms/maths/NumberOfDigits.java
+++ b/src/main/java/com/thealgorithms/maths/NumberOfDigits.java
@@ -3,35 +3,16 @@
/**
* Find the number of digits in a number.
*/
-public class NumberOfDigits {
-
- public static void main(String[] args) {
- int[] numbers = {
- 0,
- 12,
- 123,
- 1234,
- -12345,
- 123456,
- 1234567,
- 12345678,
- 123456789,
- };
- for (int i = 0; i < numbers.length; ++i) {
- assert numberOfDigits(numbers[i]) == i + 1;
- assert numberOfDigitsFast(numbers[i]) == i + 1;
- assert numberOfDigitsFaster(numbers[i]) == i + 1;
- assert numberOfDigitsRecursion(numbers[i]) == i + 1;
- }
+public final class NumberOfDigits {
+ private NumberOfDigits() {
}
-
/**
* Find the number of digits in a number.
*
* @param number number to find
* @return number of digits of given number
*/
- private static int numberOfDigits(int number) {
+ public static int numberOfDigits(int number) {
int digits = 0;
do {
digits++;
@@ -46,7 +27,7 @@ private static int numberOfDigits(int number) {
* @param number number to find
* @return number of digits of given number
*/
- private static int numberOfDigitsFast(int number) {
+ public static int numberOfDigitsFast(int number) {
return number == 0 ? 1 : (int) Math.floor(Math.log10(Math.abs(number)) + 1);
}
@@ -56,7 +37,7 @@ private static int numberOfDigitsFast(int number) {
* @param number number to find
* @return number of digits of given number
*/
- private static int numberOfDigitsFaster(int number) {
+ public static int numberOfDigitsFaster(int number) {
return number < 0 ? (-number + "").length() : (number + "").length();
}
@@ -66,7 +47,7 @@ private static int numberOfDigitsFaster(int number) {
* @param number number to find
* @return number of digits of given number
*/
- private static int numberOfDigitsRecursion(int number) {
+ public static int numberOfDigitsRecursion(int number) {
return number / 10 == 0 ? 1 : 1 + numberOfDigitsRecursion(number / 10);
}
}
diff --git a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java
index 5dad99ef30e0..a22d63897b37 100644
--- a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java
+++ b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java
@@ -1,6 +1,8 @@
package com.thealgorithms.maths;
-public class PalindromeNumber {
+public final class PalindromeNumber {
+ private PalindromeNumber() {
+ }
/**
* Check if {@code n} is palindrome number or not
*
diff --git a/src/main/java/com/thealgorithms/maths/PascalTriangle.java b/src/main/java/com/thealgorithms/maths/PascalTriangle.java
index beb9a1e4937e..ef6aa41d6e53 100644
--- a/src/main/java/com/thealgorithms/maths/PascalTriangle.java
+++ b/src/main/java/com/thealgorithms/maths/PascalTriangle.java
@@ -1,6 +1,8 @@
package com.thealgorithms.maths;
-public class PascalTriangle {
+public final class PascalTriangle {
+ private PascalTriangle() {
+ }
/**
*In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that
diff --git a/src/main/java/com/thealgorithms/maths/PerfectCube.java b/src/main/java/com/thealgorithms/maths/PerfectCube.java
index f9ed0558b8d5..4104c6238580 100644
--- a/src/main/java/com/thealgorithms/maths/PerfectCube.java
+++ b/src/main/java/com/thealgorithms/maths/PerfectCube.java
@@ -3,7 +3,9 @@
/**
* https://en.wikipedia.org/wiki/Cube_(algebra)
*/
-public class PerfectCube {
+public final class PerfectCube {
+ private PerfectCube() {
+ }
/**
* Check if a number is perfect cube or not
diff --git a/src/main/java/com/thealgorithms/maths/PerfectNumber.java b/src/main/java/com/thealgorithms/maths/PerfectNumber.java
index 7d6a045166e5..49afd23f91bf 100644
--- a/src/main/java/com/thealgorithms/maths/PerfectNumber.java
+++ b/src/main/java/com/thealgorithms/maths/PerfectNumber.java
@@ -8,7 +8,9 @@
*
* link:https://en.wikipedia.org/wiki/Perfect_number
*/
-public class PerfectNumber {
+public final class PerfectNumber {
+ private PerfectNumber() {
+ }
/**
* Check if {@code number} is perfect number or not
diff --git a/src/main/java/com/thealgorithms/maths/Perimeter.java b/src/main/java/com/thealgorithms/maths/Perimeter.java
index e3476afaf0b3..f8aa1876d388 100644
--- a/src/main/java/com/thealgorithms/maths/Perimeter.java
+++ b/src/main/java/com/thealgorithms/maths/Perimeter.java
@@ -1,7 +1,9 @@
package com.thealgorithms.maths;
// Perimeter of different 2D geometrical shapes
-public class Perimeter {
+public final class Perimeter {
+ private Perimeter() {
+ }
/**
* Calculate the Perimeter of regular polygon (equals sides)
diff --git a/src/main/java/com/thealgorithms/maths/PiNilakantha.java b/src/main/java/com/thealgorithms/maths/PiNilakantha.java
index d00240317997..6d43f134c94c 100644
--- a/src/main/java/com/thealgorithms/maths/PiNilakantha.java
+++ b/src/main/java/com/thealgorithms/maths/PiNilakantha.java
@@ -1,6 +1,8 @@
package com.thealgorithms.maths;
-public class PiNilakantha {
+public final class PiNilakantha {
+ private PiNilakantha() {
+ }
// Calculates Pi using Nilakantha's infinite series
// Method 2 in the following link explains the algorithm
diff --git a/src/main/java/com/thealgorithms/maths/PollardRho.java b/src/main/java/com/thealgorithms/maths/PollardRho.java
index 8ce62336061e..7fa913b21b7e 100644
--- a/src/main/java/com/thealgorithms/maths/PollardRho.java
+++ b/src/main/java/com/thealgorithms/maths/PollardRho.java
@@ -35,7 +35,9 @@
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
*
* */
-public class PollardRho {
+public final class PollardRho {
+ private PollardRho() {
+ }
/**
* This method returns a polynomial in x computed modulo n
@@ -57,7 +59,9 @@ static int g(int base, int modulus) {
* @throws RuntimeException object if GCD of given number cannot be found
*/
static int pollardRho(int number) {
- int x = 2, y = 2, d = 1;
+ int x = 2;
+ int y = 2;
+ int d = 1;
while (d == 1) {
// tortoise move
x = g(x, number);
diff --git a/src/main/java/com/thealgorithms/maths/Pow.java b/src/main/java/com/thealgorithms/maths/Pow.java
index 1d8ff2931ee0..3f362fe88d30 100644
--- a/src/main/java/com/thealgorithms/maths/Pow.java
+++ b/src/main/java/com/thealgorithms/maths/Pow.java
@@ -1,7 +1,9 @@
package com.thealgorithms.maths;
// POWER (exponentials) Examples (a^b)
-public class Pow {
+public final class Pow {
+ private Pow() {
+ }
public static void main(String[] args) {
assert pow(2, 0) == Math.pow(2, 0); // == 1
diff --git a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java
index cbb2d6132366..93c8252ab929 100644
--- a/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java
+++ b/src/main/java/com/thealgorithms/maths/PowerUsingRecursion.java
@@ -5,7 +5,9 @@
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/
-public class PowerUsingRecursion {
+public final class PowerUsingRecursion {
+ private PowerUsingRecursion() {
+ }
public static double power(double base, int exponent) {
// Base case: anything raised to the power of 0 is 1
diff --git a/src/main/java/com/thealgorithms/maths/PrimeCheck.java b/src/main/java/com/thealgorithms/maths/PrimeCheck.java
index 2c9714f1335b..628a819aeba4 100644
--- a/src/main/java/com/thealgorithms/maths/PrimeCheck.java
+++ b/src/main/java/com/thealgorithms/maths/PrimeCheck.java
@@ -2,7 +2,9 @@
import java.util.Scanner;
-public class PrimeCheck {
+public final class PrimeCheck {
+ private PrimeCheck() {
+ }
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
@@ -54,7 +56,8 @@ public static boolean isPrime(int n) {
*/
public static boolean fermatPrimeChecking(int n, int iteration) {
long a;
- int up = n - 2, down = 2;
+ int up = n - 2;
+ int down = 2;
for (int i = 0; i < iteration; i++) {
a = (long) Math.floor(Math.random() * (up - down + 1) + down);
if (modPow(a, n - 1, n) != 1) {
diff --git a/src/main/java/com/thealgorithms/maths/PrimeFactorization.java b/src/main/java/com/thealgorithms/maths/PrimeFactorization.java
index 7e13cc673cd4..9ac50fd9043b 100644
--- a/src/main/java/com/thealgorithms/maths/PrimeFactorization.java
+++ b/src/main/java/com/thealgorithms/maths/PrimeFactorization.java
@@ -9,7 +9,9 @@
import java.util.ArrayList;
import java.util.List;
-public class PrimeFactorization {
+public final class PrimeFactorization {
+ private PrimeFactorization() {
+ }
public static List pfactors(int n) {
List primeFactors = new ArrayList<>();
diff --git a/src/main/java/com/thealgorithms/maths/PronicNumber.java b/src/main/java/com/thealgorithms/maths/PronicNumber.java
index 312af21ea2ba..4891cf3c63b3 100644
--- a/src/main/java/com/thealgorithms/maths/PronicNumber.java
+++ b/src/main/java/com/thealgorithms/maths/PronicNumber.java
@@ -10,19 +10,21 @@
*
* */
-public class PronicNumber {
+public final class PronicNumber {
+ private PronicNumber() {
+ }
/**
* This method checks if the given number is pronic number or non-pronic number
*
- * @param input_number Integer value which is to be checked if is a pronic number or not
+ * @param inputNumber Integer value which is to be checked if is a pronic number or not
* @return true if input number is a pronic number, false otherwise
*/
- static boolean isPronic(int input_number) {
+ static boolean isPronic(int inputNumber) {
// Iterating from 0 to input_number
- for (int i = 0; i <= input_number; i++) {
+ for (int i = 0; i <= inputNumber; i++) {
// Checking if product of i and (i+1) is equals input_number
- if (i * (i + 1) == input_number && i != input_number) {
+ if (i * (i + 1) == inputNumber && i != inputNumber) {
// return true if product of i and (i+1) is equals input_number
return true;
}
diff --git a/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java b/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java
index 68932c0b76bd..f535e9e6929b 100644
--- a/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java
+++ b/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java
@@ -3,7 +3,9 @@
/**
* https://en.wikipedia.org/wiki/Pythagorean_triple
*/
-public class PythagoreanTriple {
+public final class PythagoreanTriple {
+ private PythagoreanTriple() {
+ }
public static void main(String[] args) {
assert isPythagTriple(3, 4, 5);
diff --git a/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java b/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java
index 0caa286231b6..8f5d44dbe146 100644
--- a/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java
+++ b/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java
@@ -8,7 +8,9 @@
* @author Sokratis Fotkatzikis
* @version 1.0
*/
-public class RomanNumeralUtil {
+public final class RomanNumeralUtil {
+ private RomanNumeralUtil() {
+ }
private static final int MIN_VALUE = 1;
private static final int MAX_VALUE = 5999;
diff --git a/src/main/java/com/thealgorithms/maths/SimpsonIntegration.java b/src/main/java/com/thealgorithms/maths/SimpsonIntegration.java
index ef02c5759c03..79bc112902c4 100644
--- a/src/main/java/com/thealgorithms/maths/SimpsonIntegration.java
+++ b/src/main/java/com/thealgorithms/maths/SimpsonIntegration.java
@@ -19,19 +19,19 @@ public static void main(String[] args) {
SimpsonIntegration integration = new SimpsonIntegration();
// Give random data for the example purposes
- int N = 16;
+ int n = 16;
double a = 1;
double b = 3;
- // Check so that N is even
- if (N % 2 != 0) {
- System.out.println("N must be even number for Simpsons method. Aborted");
+ // Check so that n is even
+ if (n % 2 != 0) {
+ System.out.println("n must be even number for Simpsons method. Aborted");
System.exit(1);
}
// Calculate step h and evaluate the integral
- double h = (b - a) / (double) N;
- double integralEvaluation = integration.simpsonsMethod(N, h, a);
+ double h = (b - a) / (double) n;
+ double integralEvaluation = integration.simpsonsMethod(n, h, a);
System.out.println("The integral is equal to: " + integralEvaluation);
}
@@ -45,13 +45,13 @@ public static void main(String[] args) {
*
* @return result of the integral evaluation
*/
- public double simpsonsMethod(int N, double h, double a) {
+ public double simpsonsMethod(int n, double h, double a) {
TreeMap data = new TreeMap<>(); // Key: i, Value: f(xi)
double temp;
double xi = a; // Initialize the variable xi = x0 + 0*h
// Create the table of xi and yi points
- for (int i = 0; i <= N; i++) {
+ for (int i = 0; i <= n; i++) {
temp = f(xi); // Get the value of the function at that point
data.put(i, temp);
xi += h; // Increase the xi to the next point
diff --git a/src/main/java/com/thealgorithms/maths/SquareFreeInteger.java b/src/main/java/com/thealgorithms/maths/SquareFreeInteger.java
index c988bb70808c..22e9fee00605 100644
--- a/src/main/java/com/thealgorithms/maths/SquareFreeInteger.java
+++ b/src/main/java/com/thealgorithms/maths/SquareFreeInteger.java
@@ -14,7 +14,9 @@
import java.util.HashSet;
import java.util.List;
-public class SquareFreeInteger {
+public final class SquareFreeInteger {
+ private SquareFreeInteger() {
+ }
/**
* This method returns whether an integer is square free
*
diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java
index 2f8fa9a83885..2b071307e2bc 100644
--- a/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java
+++ b/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java
@@ -1,6 +1,8 @@
package com.thealgorithms.maths;
-public class SquareRootWithBabylonianMethod {
+public final class SquareRootWithBabylonianMethod {
+ private SquareRootWithBabylonianMethod() {
+ }
/**
* get the value, return the square root
@@ -8,7 +10,7 @@ public class SquareRootWithBabylonianMethod {
* @param num contains elements
* @return the square root of num
*/
- public static float square_Root(float num) {
+ public static float squareRoot(float num) {
float a = num;
float b = 1;
double e = 0.000001;
diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java
index eb116d21ac36..80d185c93785 100644
--- a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java
+++ b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java
@@ -14,7 +14,9 @@
* be changed according to the user preference.
*/
-public class SquareRootWithNewtonRaphsonMethod {
+public final class SquareRootWithNewtonRaphsonMethod {
+ private SquareRootWithNewtonRaphsonMethod() {
+ }
public static double squareRoot(int n) {
double x = n; // initially taking a guess that x = n.
diff --git a/src/main/java/com/thealgorithms/maths/StandardDeviation.java b/src/main/java/com/thealgorithms/maths/StandardDeviation.java
index 84d21f3082f0..a8e88d930a9c 100644
--- a/src/main/java/com/thealgorithms/maths/StandardDeviation.java
+++ b/src/main/java/com/thealgorithms/maths/StandardDeviation.java
@@ -1,18 +1,20 @@
package com.thealgorithms.maths;
-public class StandardDeviation {
+public final class StandardDeviation {
+ private StandardDeviation() {
+ }
public static double stdDev(double[] data) {
- double var = 0;
+ double variance = 0;
double avg = 0;
for (int i = 0; i < data.length; i++) {
avg += data[i];
}
avg /= data.length;
for (int j = 0; j < data.length; j++) {
- var += Math.pow((data[j] - avg), 2);
+ variance += Math.pow((data[j] - avg), 2);
}
- var /= data.length;
- return Math.sqrt(var);
+ variance /= data.length;
+ return Math.sqrt(variance);
}
}
diff --git a/src/main/java/com/thealgorithms/maths/StandardScore.java b/src/main/java/com/thealgorithms/maths/StandardScore.java
index dcedf458b09e..22a9f550e114 100644
--- a/src/main/java/com/thealgorithms/maths/StandardScore.java
+++ b/src/main/java/com/thealgorithms/maths/StandardScore.java
@@ -1,6 +1,8 @@
package com.thealgorithms.maths;
-public class StandardScore {
+public final class StandardScore {
+ private StandardScore() {
+ }
public static double zScore(double num, double mean, double stdDev) {
return (num - mean) / stdDev;
diff --git a/src/main/java/com/thealgorithms/maths/TrinomialTriangle.java b/src/main/java/com/thealgorithms/maths/TrinomialTriangle.java
index 0e2fe9e7a46a..877ef4227afc 100644
--- a/src/main/java/com/thealgorithms/maths/TrinomialTriangle.java
+++ b/src/main/java/com/thealgorithms/maths/TrinomialTriangle.java
@@ -7,9 +7,11 @@
*
* Example Input: n = 4 Output 1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 1
*/
-public class TrinomialTriangle {
+public final class TrinomialTriangle {
+ private TrinomialTriangle() {
+ }
- public static int TrinomialValue(int n, int k) {
+ public static int trinomialValue(int n, int k) {
if (n == 0 && k == 0) {
return 1;
}
@@ -18,17 +20,17 @@ public static int TrinomialValue(int n, int k) {
return 0;
}
- return (TrinomialValue(n - 1, k - 1) + TrinomialValue(n - 1, k) + TrinomialValue(n - 1, k + 1));
+ return (trinomialValue(n - 1, k - 1) + trinomialValue(n - 1, k) + trinomialValue(n - 1, k + 1));
}
public static void printTrinomial(int n) {
for (int i = 0; i < n; i++) {
for (int j = -i; j <= 0; j++) {
- System.out.print(TrinomialValue(i, j) + " ");
+ System.out.print(trinomialValue(i, j) + " ");
}
for (int j = 1; j <= i; j++) {
- System.out.print(TrinomialValue(i, j) + " ");
+ System.out.print(trinomialValue(i, j) + " ");
}
System.out.println();
diff --git a/src/main/java/com/thealgorithms/maths/TwinPrime.java b/src/main/java/com/thealgorithms/maths/TwinPrime.java
index 867a1d23f220..81731376b37a 100644
--- a/src/main/java/com/thealgorithms/maths/TwinPrime.java
+++ b/src/main/java/com/thealgorithms/maths/TwinPrime.java
@@ -9,7 +9,9 @@
*
* */
-public class TwinPrime {
+public final class TwinPrime {
+ private TwinPrime() {
+ }
/**
* This method returns twin prime of the integer value passed as argument
diff --git a/src/main/java/com/thealgorithms/maths/VampireNumber.java b/src/main/java/com/thealgorithms/maths/VampireNumber.java
index d9aa4f203550..d64c82c6e68e 100644
--- a/src/main/java/com/thealgorithms/maths/VampireNumber.java
+++ b/src/main/java/com/thealgorithms/maths/VampireNumber.java
@@ -16,7 +16,9 @@
*
*
*/
-public class VampireNumber {
+public final class VampireNumber {
+ private VampireNumber() {
+ }
public static void main(String[] args) {
test(10, 1000);
diff --git a/src/main/java/com/thealgorithms/maths/VectorCrossProduct.java b/src/main/java/com/thealgorithms/maths/VectorCrossProduct.java
index 8a1bb1488eab..e2769744bcda 100644
--- a/src/main/java/com/thealgorithms/maths/VectorCrossProduct.java
+++ b/src/main/java/com/thealgorithms/maths/VectorCrossProduct.java
@@ -55,14 +55,14 @@ public class VectorCrossProduct {
/**
* constructor, initialises Vector with given Direction Ratios
*
- * @param _x set to x
- * @param _y set to y
- * @param _z set to z
+ * @param vectorX set to x
+ * @param vectorY set to y
+ * @param vectorZ set to z
*/
- VectorCrossProduct(int _x, int _y, int _z) {
- x = _x;
- y = _y;
- z = _z;
+ VectorCrossProduct(int vectorX, int vectorY, int vectorZ) {
+ x = vectorX;
+ y = vectorY;
+ z = vectorZ;
}
/**
@@ -111,15 +111,15 @@ public static void main(String[] args) {
static void test() {
// Create two vectors
- VectorCrossProduct A = new VectorCrossProduct(1, -2, 3);
- VectorCrossProduct B = new VectorCrossProduct(2, 0, 3);
+ VectorCrossProduct a = new VectorCrossProduct(1, -2, 3);
+ VectorCrossProduct b = new VectorCrossProduct(2, 0, 3);
// Determine cross product
- VectorCrossProduct crossProd = A.crossProduct(B);
+ VectorCrossProduct crossProd = a.crossProduct(b);
crossProd.displayVector();
// Determine dot product
- int dotProd = A.dotProduct(B);
- System.out.println("Dot Product of A and B: " + dotProd);
+ int dotProd = a.dotProduct(b);
+ System.out.println("Dot Product of a and b: " + dotProd);
}
}
diff --git a/src/main/java/com/thealgorithms/maths/Volume.java b/src/main/java/com/thealgorithms/maths/Volume.java
index edc3575dfda6..4b73f849bb81 100644
--- a/src/main/java/com/thealgorithms/maths/Volume.java
+++ b/src/main/java/com/thealgorithms/maths/Volume.java
@@ -1,7 +1,9 @@
package com.thealgorithms.maths;
/* Calculate the volume of various shapes.*/
-public class Volume {
+public final class Volume {
+ private Volume() {
+ }
/**
* Calculate the volume of a cube.
diff --git a/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java b/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java
index aa255832f47f..afd34933047a 100644
--- a/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java
+++ b/src/main/java/com/thealgorithms/matrixexponentiation/Fibonacci.java
@@ -7,13 +7,15 @@
* see https://www.geeksforgeeks.org/matrix-exponentiation/
*
*/
-public class Fibonacci {
+public final class Fibonacci {
+ private Fibonacci() {
+ }
// Exponentiation matrix for Fibonacci sequence
- private static final int[][] fibMatrix = {{1, 1}, {1, 0}};
- private static final int[][] identityMatrix = {{1, 0}, {0, 1}};
+ private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
+ private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
// First 2 fibonacci numbers
- private static final int[][] baseFibNumbers = {{1}, {0}};
+ private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}};
/**
* Performs multiplication of 2 matrices
@@ -53,14 +55,14 @@ private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
*/
public static int[][] fib(int n) {
if (n == 0) {
- return Fibonacci.identityMatrix;
+ return Fibonacci.IDENTITY_MATRIX;
} else {
int[][] cachedResult = fib(n / 2);
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
if (n % 2 == 0) {
return matrixExpResult;
} else {
- return matrixMultiplication(Fibonacci.fibMatrix, matrixExpResult);
+ return matrixMultiplication(Fibonacci.FIB_MATRIX, matrixExpResult);
}
}
}
@@ -69,7 +71,7 @@ public static void main(String[] args) {
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
- int[][] result = matrixMultiplication(fib(n), baseFibNumbers);
+ int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
System.out.println("Fib(" + n + ") = " + result[1][0]);
sc.close();
}
diff --git a/src/main/java/com/thealgorithms/misc/InverseOfMatrix.java b/src/main/java/com/thealgorithms/misc/InverseOfMatrix.java
index 0fb5b6f17b97..5543463e9749 100644
--- a/src/main/java/com/thealgorithms/misc/InverseOfMatrix.java
+++ b/src/main/java/com/thealgorithms/misc/InverseOfMatrix.java
@@ -11,7 +11,9 @@
*
* We can also find the inverse of a matrix
*/
-public class InverseOfMatrix {
+public final class InverseOfMatrix {
+ private InverseOfMatrix() {
+ }
public static void main(String[] argv) {
Scanner input = new Scanner(System.in);
diff --git a/src/main/java/com/thealgorithms/misc/MapReduce.java b/src/main/java/com/thealgorithms/misc/MapReduce.java
index baf960f8ecef..c076957344f9 100644
--- a/src/main/java/com/thealgorithms/misc/MapReduce.java
+++ b/src/main/java/com/thealgorithms/misc/MapReduce.java
@@ -15,7 +15,9 @@
* Wikipedia link : https://en.wikipedia.org/wiki/MapReduce
*/
-public class MapReduce {
+public final class MapReduce {
+ private MapReduce() {
+ }
/*
*Counting all the words frequency within a sentence.
*/
diff --git a/src/main/java/com/thealgorithms/misc/MedianOfMatrix.java b/src/main/java/com/thealgorithms/misc/MedianOfMatrix.java
index 4d8b980f2409..d4ddffe8ddd7 100644
--- a/src/main/java/com/thealgorithms/misc/MedianOfMatrix.java
+++ b/src/main/java/com/thealgorithms/misc/MedianOfMatrix.java
@@ -10,6 +10,8 @@
*/
public final class MedianOfMatrix {
+ private MedianOfMatrix() {
+ }
public static int median(List> matrix) {
// Flatten the matrix into a 1D list
diff --git a/src/main/java/com/thealgorithms/misc/PalindromePrime.java b/src/main/java/com/thealgorithms/misc/PalindromePrime.java
index 58de938394af..6b01cdced23c 100644
--- a/src/main/java/com/thealgorithms/misc/PalindromePrime.java
+++ b/src/main/java/com/thealgorithms/misc/PalindromePrime.java
@@ -2,7 +2,9 @@
import java.util.Scanner;
-public class PalindromePrime {
+public final class PalindromePrime {
+ private PalindromePrime() {
+ }
public static void main(String[] args) { // Main funtion
Scanner in = new Scanner(System.in);
diff --git a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java
index af2ca4dd5324..0dfc8ac32a6f 100644
--- a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java
+++ b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java
@@ -1,8 +1,10 @@
package com.thealgorithms.misc;
-import java.util.*;
+import java.util.Arrays;
-public class RangeInSortedArray {
+public final class RangeInSortedArray {
+ private RangeInSortedArray() {
+ }
public static void main(String[] args) {
// Testcases
@@ -26,7 +28,7 @@ public static void alteredBinSearch(int[] nums, int key, int left, int right, in
if (left > right) {
return;
}
- int mid = (left + right) / 2;
+ int mid = (left + right) >>> 1;
if (nums[mid] > key) {
alteredBinSearch(nums, key, left, mid - 1, range, goLeft);
} else if (nums[mid] < key) {
@@ -52,7 +54,7 @@ public static void alteredBinSearch(int[] nums, int key, int left, int right, in
// of 'key'
public static void alteredBinSearchIter(int[] nums, int key, int left, int right, int[] range, boolean goLeft) {
while (left <= right) {
- int mid = (left + right) / 2;
+ final int mid = (left + right) >>> 1;
if (nums[mid] > key) {
right = mid - 1;
} else if (nums[mid] < key) {
@@ -84,7 +86,7 @@ public static int getCountLessThan(int[] nums, int key) {
public static int getLessThan(int[] nums, int key, int left, int right) {
int count = 0;
while (left <= right) {
- int mid = (left + right) / 2;
+ final int mid = (left + right) >>> 1;
if (nums[mid] > key) {
right = mid - 1;
} else if (nums[mid] <= key) {
diff --git a/src/main/java/com/thealgorithms/misc/Sort012D.java b/src/main/java/com/thealgorithms/misc/Sort012D.java
index 2ffe31b9ddd8..febe13f4fec3 100644
--- a/src/main/java/com/thealgorithms/misc/Sort012D.java
+++ b/src/main/java/com/thealgorithms/misc/Sort012D.java
@@ -1,6 +1,6 @@
package com.thealgorithms.misc;
-import java.util.*;
+import java.util.Scanner;
/**
* The array is divided into four sections: a[1..Lo-1] zeroes a[Lo..Mid-1] ones
@@ -11,7 +11,9 @@
* For more information on the Dutch national flag algorithm refer
* https://en.wikipedia.org/wiki/Dutch_national_flag_problem
*/
-public class Sort012D {
+public final class Sort012D {
+ private Sort012D() {
+ }
public static void main(String[] args) {
Scanner np = new Scanner(System.in);
@@ -49,6 +51,9 @@ public static void sort012(int[] a) {
h--;
break;
}
+ default: {
+ throw new IllegalArgumentException("Unexpected value: " + a[mid]);
+ }
}
}
System.out.println("the Sorted array is ");
diff --git a/src/main/java/com/thealgorithms/misc/Sparcity.java b/src/main/java/com/thealgorithms/misc/Sparsity.java
similarity index 81%
rename from src/main/java/com/thealgorithms/misc/Sparcity.java
rename to src/main/java/com/thealgorithms/misc/Sparsity.java
index 7eb5e5896308..cae2fbdead94 100644
--- a/src/main/java/com/thealgorithms/misc/Sparcity.java
+++ b/src/main/java/com/thealgorithms/misc/Sparsity.java
@@ -1,6 +1,6 @@
package com.thealgorithms.misc;
-import java.util.*;
+import java.util.Scanner;
/*
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements
@@ -11,15 +11,17 @@
* @author Ojasva Jain
*/
-class Sparcity {
+final class Sparsity {
+ private Sparsity() {
+ }
/*
- * @return Sparcity of matrix
+ * @return Sparsity of matrix
*
- * where sparcity = number of zeroes/total elements in matrix
+ * where sparsity = number of zeroes/total elements in matrix
*
*/
- static double sparcity(double[][] mat) {
+ static double sparsity(double[][] mat) {
int zero = 0;
// Traversing the matrix to count number of zeroes
for (int i = 0; i < mat.length; i++) {
@@ -29,7 +31,7 @@ static double sparcity(double[][] mat) {
}
}
}
- // return sparcity
+ // return sparsity
return ((double) zero / (mat.length * mat[1].length));
}
@@ -48,7 +50,7 @@ public static void main(String[] args) {
mat[i][j] = in.nextDouble();
}
}
- System.out.println("Sparcity of matrix is: " + sparcity(mat));
+ System.out.println("Sparsity of matrix is: " + sparsity(mat));
in.close();
}
}
diff --git a/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java b/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java
index a232ad986970..1c5f4a440532 100644
--- a/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java
+++ b/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java
@@ -1,6 +1,14 @@
package com.thealgorithms.misc;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Scanner;
+import java.util.Set;
public class ThreeSumProblem {
@@ -16,13 +24,13 @@ public static void main(String[] args) {
arr[i] = scan.nextInt();
}
ThreeSumProblem th = new ThreeSumProblem();
- System.out.println("Brute Force Approach\n" + (th.BruteForce(arr, ts)) + "\n");
- System.out.println("Two Pointer Approach\n" + (th.TwoPointer(arr, ts)) + "\n");
- System.out.println("Hashmap Approach\n" + (th.Hashmap(arr, ts)));
+ System.out.println("Brute Force Approach\n" + (th.bruteForce(arr, ts)) + "\n");
+ System.out.println("Two Pointer Approach\n" + (th.twoPointer(arr, ts)) + "\n");
+ System.out.println("Hashmap Approach\n" + (th.hashMap(arr, ts)));
scan.close();
}
- public List> BruteForce(int[] nums, int target) {
+ public List> bruteForce(int[] nums, int target) {
List> arr = new ArrayList>();
for (int i = 0; i < nums.length; i++) {
@@ -43,7 +51,7 @@ public List> BruteForce(int[] nums, int target) {
return arr;
}
- public List> TwoPointer(int[] nums, int target) {
+ public List> twoPointer(int[] nums, int target) {
Arrays.sort(nums);
List> arr = new ArrayList>();
int start = 0;
@@ -73,7 +81,7 @@ public List> TwoPointer(int[] nums, int target) {
return new ArrayList>(set);
}
- public List> Hashmap(int[] nums, int target) {
+ public List> hashMap(int[] nums, int target) {
Arrays.sort(nums);
Set> ts = new HashSet<>();
HashMap hm = new HashMap<>();
diff --git a/src/main/java/com/thealgorithms/misc/WordBoggle.java b/src/main/java/com/thealgorithms/misc/WordBoggle.java
index 0dfbd89def99..3eb0dc95ffb5 100644
--- a/src/main/java/com/thealgorithms/misc/WordBoggle.java
+++ b/src/main/java/com/thealgorithms/misc/WordBoggle.java
@@ -1,8 +1,16 @@
package com.thealgorithms.misc;
-import java.util.*;
-
-public class WordBoggle {
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public final class WordBoggle {
+ private WordBoggle() {
+ }
/**
* O(nm * 8^s + ws) time where n = width of boggle board, m = height of
@@ -127,7 +135,7 @@ class Trie {
TrieNode root;
char endSymbol;
- public Trie() {
+ Trie() {
this.root = new TrieNode();
this.endSymbol = '*';
}
diff --git a/src/main/java/com/thealgorithms/misc/matrixTranspose.java b/src/main/java/com/thealgorithms/misc/matrixTranspose.java
index b5ad02184a00..40634f18b5f6 100644
--- a/src/main/java/com/thealgorithms/misc/matrixTranspose.java
+++ b/src/main/java/com/thealgorithms/misc/matrixTranspose.java
@@ -18,7 +18,9 @@
* @version 11.0.9
* @since 2014-03-31
*/
-public class matrixTranspose {
+public final class matrixTranspose {
+ private matrixTranspose() {
+ }
public static void main(String[] args) {
/*
@@ -29,7 +31,10 @@ public static void main(String[] args) {
* @return Nothing.
*/
Scanner sc = new Scanner(System.in);
- int i, j, row, column;
+ int i;
+ int j;
+ int row;
+ int column;
System.out.println("Enter the number of rows in the 2D matrix:");
/*
diff --git a/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java b/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
index 4ee54bbdacf8..f43841f1f184 100644
--- a/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
+++ b/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
@@ -8,7 +8,9 @@
* @author sangin-lee
*/
-public class ArrayLeftRotation {
+public final class ArrayLeftRotation {
+ private ArrayLeftRotation() {
+ }
/*
* Returns the result of left rotation of given array arr and integer n
diff --git a/src/main/java/com/thealgorithms/others/BFPRT.java b/src/main/java/com/thealgorithms/others/BFPRT.java
index 29f47cd68ed6..1a5b44180651 100644
--- a/src/main/java/com/thealgorithms/others/BFPRT.java
+++ b/src/main/java/com/thealgorithms/others/BFPRT.java
@@ -5,7 +5,9 @@
/**
* BFPRT algorithm.
*/
-public class BFPRT {
+public final class BFPRT {
+ private BFPRT() {
+ }
public static int[] getMinKNumsByBFPRT(int[] arr, int k) {
if (k < 1 || k > arr.length) {
@@ -32,9 +34,7 @@ public static int getMinKthByBFPRT(int[] arr, int k) {
public static int[] copyArray(int[] arr) {
int[] copyArr = new int[arr.length];
- for (int i = 0; i < arr.length; i++) {
- copyArr[i] = arr[i];
- }
+ System.arraycopy(arr, 0, copyArr, 0, arr.length);
return copyArr;
}
diff --git a/src/main/java/com/thealgorithms/others/BankersAlgorithm.java b/src/main/java/com/thealgorithms/others/BankersAlgorithm.java
index 3fbb4f9fda68..a22d7c737415 100644
--- a/src/main/java/com/thealgorithms/others/BankersAlgorithm.java
+++ b/src/main/java/com/thealgorithms/others/BankersAlgorithm.java
@@ -20,7 +20,9 @@
*/
import java.util.Scanner;
-public class BankersAlgorithm {
+public final class BankersAlgorithm {
+ private BankersAlgorithm() {
+ }
/**
* This method finds the need of each process
@@ -58,10 +60,7 @@ static boolean checkSafeSystem(int[] processes, int[] availableArray, int[][] ma
int[] safeSequenceArray = new int[totalProcess];
int[] workArray = new int[totalResources];
-
- for (int i = 0; i < totalResources; i++) {
- workArray[i] = availableArray[i];
- }
+ System.arraycopy(availableArray, 0, workArray, 0, totalResources);
int count = 0;
@@ -111,7 +110,8 @@ static boolean checkSafeSystem(int[] processes, int[] availableArray, int[][] ma
* This is main method of Banker's Algorithm
*/
public static void main(String[] args) {
- int numberOfProcesses, numberOfResources;
+ int numberOfProcesses;
+ int numberOfResources;
Scanner sc = new Scanner(System.in);
diff --git a/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java b/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java
index a1983feccb2e..b70fffe82c5b 100644
--- a/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java
+++ b/src/main/java/com/thealgorithms/others/BrianKernighanAlgorithm.java
@@ -20,7 +20,9 @@
*
* Time Complexity: O(logn)
*/
-public class BrianKernighanAlgorithm {
+public final class BrianKernighanAlgorithm {
+ private BrianKernighanAlgorithm() {
+ }
/**
* @param num: number in which we count the set bits
diff --git a/src/main/java/com/thealgorithms/others/CRC16.java b/src/main/java/com/thealgorithms/others/CRC16.java
index c5c3b1f35a7d..85e5cd2c13ae 100644
--- a/src/main/java/com/thealgorithms/others/CRC16.java
+++ b/src/main/java/com/thealgorithms/others/CRC16.java
@@ -3,7 +3,9 @@
/**
* Generates a crc16 checksum for a given string
*/
-public class CRC16 {
+public final class CRC16 {
+ private CRC16() {
+ }
public static void main(String[] args) {
System.out.println(crc16("Hello World!"));
diff --git a/src/main/java/com/thealgorithms/others/CRC32.java b/src/main/java/com/thealgorithms/others/CRC32.java
index 561a33f4dae9..180936ed46c1 100644
--- a/src/main/java/com/thealgorithms/others/CRC32.java
+++ b/src/main/java/com/thealgorithms/others/CRC32.java
@@ -5,7 +5,9 @@
/**
* Generates a crc32 checksum for a given string or byte array
*/
-public class CRC32 {
+public final class CRC32 {
+ private CRC32() {
+ }
public static void main(String[] args) {
System.out.println(Integer.toHexString(crc32("Hello World")));
diff --git a/src/main/java/com/thealgorithms/others/Conway.java b/src/main/java/com/thealgorithms/others/Conway.java
index b1d54e61094f..ab39890c9ece 100644
--- a/src/main/java/com/thealgorithms/others/Conway.java
+++ b/src/main/java/com/thealgorithms/others/Conway.java
@@ -1,8 +1,12 @@
package com.thealgorithms.others;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
-public class Conway {
+public final class Conway {
+ private Conway() {
+ }
/*
* This class will generate the conway sequence also known as the look and say sequence.
@@ -13,7 +17,7 @@ public class Conway {
*1s, two 2s, one 1" or 312211. https://en.wikipedia.org/wiki/Look-and-say_sequence
* */
- private static final StringBuilder builder = new StringBuilder();
+ private static final StringBuilder BUILDER = new StringBuilder();
protected static List generateList(String originalString, int maxIteration) {
List numbers = new ArrayList<>();
@@ -25,9 +29,9 @@ protected static List generateList(String originalString, int maxIterati
}
public static String generateNextElement(String originalString) {
- builder.setLength(0);
+ BUILDER.setLength(0);
String[] stp = originalString.split("(?<=(.))(?!\\1)");
- Arrays.stream(stp).forEach(s -> builder.append(s.length()).append(s.charAt(0)));
- return builder.toString();
+ Arrays.stream(stp).forEach(s -> BUILDER.append(s.length()).append(s.charAt(0)));
+ return BUILDER.toString();
}
}
diff --git a/src/main/java/com/thealgorithms/others/CountChar.java b/src/main/java/com/thealgorithms/others/CountChar.java
index 5a78c0c17412..ad858137726b 100644
--- a/src/main/java/com/thealgorithms/others/CountChar.java
+++ b/src/main/java/com/thealgorithms/others/CountChar.java
@@ -1,6 +1,8 @@
package com.thealgorithms.others;
-public class CountChar {
+public final class CountChar {
+ private CountChar() {
+ }
/**
* Count non space character in string
@@ -9,7 +11,7 @@ public class CountChar {
* @return number of character in the specified string
*/
- public static int CountCharacters(String str) {
+ public static int countCharacters(String str) {
return str.replaceAll("\\s", "").length();
}
}
diff --git a/src/main/java/com/thealgorithms/others/CountWords.java b/src/main/java/com/thealgorithms/others/CountWords.java
index 1defde2cdd8b..26b9c50d928c 100644
--- a/src/main/java/com/thealgorithms/others/CountWords.java
+++ b/src/main/java/com/thealgorithms/others/CountWords.java
@@ -3,7 +3,7 @@
/**
* @author Marcus
*/
-final public class CountWords {
+public final class CountWords {
private CountWords() {
}
diff --git a/src/main/java/com/thealgorithms/others/Damm.java b/src/main/java/com/thealgorithms/others/Damm.java
index a32ae246a2ec..55a4c5b81a89 100644
--- a/src/main/java/com/thealgorithms/others/Damm.java
+++ b/src/main/java/com/thealgorithms/others/Damm.java
@@ -14,7 +14,9 @@
* @see Wiki. Damm
* algorithm
*/
-public class Damm {
+public final class Damm {
+ private Damm() {
+ }
/**
* Weakly totally anti-symmetric quasigroup of order 10. This table is not
diff --git a/src/main/java/com/thealgorithms/others/Dijkstra.java b/src/main/java/com/thealgorithms/others/Dijkstra.java
index 4886dc4ce7a4..e2a778f8d6c3 100644
--- a/src/main/java/com/thealgorithms/others/Dijkstra.java
+++ b/src/main/java/com/thealgorithms/others/Dijkstra.java
@@ -15,9 +15,14 @@
* https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Java Also most of the
* comments are from RosettaCode.
*/
-import java.util.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.NavigableSet;
+import java.util.TreeSet;
-public class Dijkstra {
+public final class Dijkstra {
+ private Dijkstra() {
+ }
private static final Graph.Edge[] GRAPH = {
// Distance from node "a" to node "b" is 7.
@@ -58,10 +63,11 @@ class Graph {
*/
public static class Edge {
- public final String v1, v2;
+ public final String v1;
+ public final String v2;
public final int dist;
- public Edge(String v1, String v2, int dist) {
+ Edge(String v1, String v2, int dist) {
this.v1 = v1;
this.v2 = v2;
this.dist = dist;
@@ -79,7 +85,7 @@ public static class Vertex implements Comparable {
public Vertex previous = null;
public final Map neighbours = new HashMap<>();
- public Vertex(String name) {
+ Vertex(String name) {
this.name = name;
}
@@ -147,7 +153,7 @@ public String toString() {
/**
* Builds a graph from a set of edges
*/
- public Graph(Edge[] edges) {
+ Graph(Edge[] edges) {
graph = new HashMap<>(edges.length);
// one pass to find all vertices
@@ -193,7 +199,8 @@ public void dijkstra(String startName) {
* Implementation of dijkstra's algorithm using a binary heap.
*/
private void dijkstra(final NavigableSet q) {
- Vertex u, v;
+ Vertex u;
+ Vertex v;
while (!q.isEmpty()) {
// vertex with shortest distance (first iteration will return source)
u = q.pollFirst();
diff --git a/src/main/java/com/thealgorithms/others/EulersFunction.java b/src/main/java/com/thealgorithms/others/EulersFunction.java
index 27c9aed8b620..f08e5e4fa395 100644
--- a/src/main/java/com/thealgorithms/others/EulersFunction.java
+++ b/src/main/java/com/thealgorithms/others/EulersFunction.java
@@ -3,7 +3,7 @@
/**
* @brief utility class for Euler's totient function
*/
-final public class EulersFunction {
+public final class EulersFunction {
private EulersFunction() {
}
diff --git a/src/main/java/com/thealgorithms/others/FibbonaciSeries.java b/src/main/java/com/thealgorithms/others/FibbonaciSeries.java
index 103e943743c7..a4815296e547 100644
--- a/src/main/java/com/thealgorithms/others/FibbonaciSeries.java
+++ b/src/main/java/com/thealgorithms/others/FibbonaciSeries.java
@@ -15,13 +15,16 @@
* Problem Statement: print all Fibonacci numbers that are smaller than your
* given input N
*/
-public class FibbonaciSeries {
+public final class FibbonaciSeries {
+ private FibbonaciSeries() {
+ }
public static void main(String[] args) {
// Get input from the user
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
- int first = 0, second = 1;
+ int first = 0;
+ int second = 1;
scan.close();
while (first <= n) {
// print first fibo 0 then add second fibo into it while updating second as well
diff --git a/src/main/java/com/thealgorithms/others/FloydTriangle.java b/src/main/java/com/thealgorithms/others/FloydTriangle.java
index d25ab303e3ed..fbeaec339248 100644
--- a/src/main/java/com/thealgorithms/others/FloydTriangle.java
+++ b/src/main/java/com/thealgorithms/others/FloydTriangle.java
@@ -2,12 +2,15 @@
import java.util.Scanner;
-class FloydTriangle {
+final class FloydTriangle {
+ private FloydTriangle() {
+ }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
- int r = sc.nextInt(), n = 0;
+ int r = sc.nextInt();
+ int n = 0;
sc.close();
for (int i = 0; i < r; i++) {
for (int j = 0; j <= i; j++) {
diff --git a/src/main/java/com/thealgorithms/others/GuassLegendre.java b/src/main/java/com/thealgorithms/others/GuassLegendre.java
index 8b15739a6d91..5ecfdf2b84cc 100644
--- a/src/main/java/com/thealgorithms/others/GuassLegendre.java
+++ b/src/main/java/com/thealgorithms/others/GuassLegendre.java
@@ -6,7 +6,9 @@
*
* @author AKS1996
*/
-public class GuassLegendre {
+public final class GuassLegendre {
+ private GuassLegendre() {
+ }
public static void main(String[] args) {
for (int i = 1; i <= 3; ++i) {
@@ -19,7 +21,10 @@ static double pi(int l) {
* l: No of loops to run
*/
- double a = 1, b = Math.pow(2, -0.5), t = 0.25, p = 1;
+ double a = 1;
+ double b = Math.pow(2, -0.5);
+ double t = 0.25;
+ double p = 1;
for (int i = 0; i < l; ++i) {
double[] temp = update(a, b, t, p);
a = temp[0];
diff --git a/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java b/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java
index 57ff204c39b1..0ae1e451bc6a 100644
--- a/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java
+++ b/src/main/java/com/thealgorithms/others/HappyNumbersSeq.java
@@ -5,7 +5,9 @@
import java.util.Scanner;
import java.util.Set;
-public class HappyNumbersSeq {
+public final class HappyNumbersSeq {
+ private HappyNumbersSeq() {
+ }
private static final Set CYCLE_NUMS = new HashSet<>(Arrays.asList(4, 16, 20, 37, 58, 145));
diff --git a/src/main/java/com/thealgorithms/others/Huffman.java b/src/main/java/com/thealgorithms/others/Huffman.java
index cc8c8d09864f..4fdee5d5e70e 100644
--- a/src/main/java/com/thealgorithms/others/Huffman.java
+++ b/src/main/java/com/thealgorithms/others/Huffman.java
@@ -26,7 +26,9 @@ public int compare(HuffmanNode x, HuffmanNode y) {
}
}
-public class Huffman {
+public final class Huffman {
+ private Huffman() {
+ }
// recursive function to print the
// huffman-code through the tree traversal.
diff --git a/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java b/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java
index 201c0ad2dd80..15093549871b 100644
--- a/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java
+++ b/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java
@@ -1,8 +1,10 @@
package com.thealgorithms.others;
-import java.util.*;
+import java.util.Scanner;
-public class InsertDeleteInArray {
+public final class InsertDeleteInArray {
+ private InsertDeleteInArray() {
+ }
public static void main(String[] args) {
try (Scanner s = new Scanner(System.in)) {
@@ -19,33 +21,32 @@ public static void main(String[] args) {
// To insert a new element(we are creating a new array)
System.out.println("Enter the index at which the element should be inserted");
- int insert_pos = s.nextInt();
+ int insertPos = s.nextInt();
System.out.println("Enter the element to be inserted");
int ins = s.nextInt();
int size2 = size + 1;
int[] b = new int[size2];
for (i = 0; i < size2; i++) {
- if (i <= insert_pos) {
+ if (i <= insertPos) {
b[i] = a[i];
} else {
b[i] = a[i - 1];
}
}
- b[insert_pos] = ins;
+ b[insertPos] = ins;
for (i = 0; i < size2; i++) {
System.out.println(b[i]);
}
// To delete an element given the index
System.out.println("Enter the index at which element is to be deleted");
- int del_pos = s.nextInt();
- for (i = del_pos; i < size2 - 1; i++) {
+ int delPos = s.nextInt();
+ for (i = delPos; i < size2 - 1; i++) {
b[i] = b[i + 1];
}
for (i = 0; i < size2 - 1; i++) {
System.out.println(b[i]);
}
- s.close();
}
}
}
diff --git a/src/main/java/com/thealgorithms/others/KMP.java b/src/main/java/com/thealgorithms/others/KMP.java
index 7fb5645a2726..73eaf2fc9beb 100644
--- a/src/main/java/com/thealgorithms/others/KMP.java
+++ b/src/main/java/com/thealgorithms/others/KMP.java
@@ -4,18 +4,20 @@
* Implementation of Knuth–Morris–Pratt algorithm Usage: see the main function
* for an example
*/
-public class KMP {
+public final class KMP {
+ private KMP() {
+ }
// a working example
public static void main(String[] args) {
final String haystack = "AAAAABAAABA"; // This is the full string
final String needle = "AAAA"; // This is the substring that we want to find
- KMPmatcher(haystack, needle);
+ kmpMatcher(haystack, needle);
}
// find the starting index in string haystack[] that matches the search word P[]
- public static void KMPmatcher(final String haystack, final String needle) {
+ public static void kmpMatcher(final String haystack, final String needle) {
final int m = haystack.length();
final int n = needle.length();
final int[] pi = computePrefixFunction(needle);
@@ -37,17 +39,17 @@ public static void KMPmatcher(final String haystack, final String needle) {
}
// return the prefix function
- private static int[] computePrefixFunction(final String P) {
- final int n = P.length();
+ private static int[] computePrefixFunction(final String p) {
+ final int n = p.length();
final int[] pi = new int[n];
pi[0] = 0;
int q = 0;
for (int i = 1; i < n; i++) {
- while (q > 0 && P.charAt(q) != P.charAt(i)) {
+ while (q > 0 && p.charAt(q) != p.charAt(i)) {
q = pi[q - 1];
}
- if (P.charAt(q) == P.charAt(i)) {
+ if (p.charAt(q) == p.charAt(i)) {
q++;
}
diff --git a/src/main/java/com/thealgorithms/others/KochSnowflake.java b/src/main/java/com/thealgorithms/others/KochSnowflake.java
index 8395b1f486a7..0e2600a7d72f 100644
--- a/src/main/java/com/thealgorithms/others/KochSnowflake.java
+++ b/src/main/java/com/thealgorithms/others/KochSnowflake.java
@@ -1,6 +1,8 @@
package com.thealgorithms.others;
-import java.awt.*;
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@@ -22,14 +24,16 @@
* https://natureofcode.com/book/chapter-8-fractals/
* #84-the-koch-curve-and-the-arraylist-technique ).
*/
-public class KochSnowflake {
+public final class KochSnowflake {
+ private KochSnowflake() {
+ }
public static void main(String[] args) {
// Test Iterate-method
ArrayList vectors = new ArrayList();
vectors.add(new Vector2(0, 0));
vectors.add(new Vector2(1, 0));
- ArrayList result = Iterate(vectors, 1);
+ ArrayList result = iterate(vectors, 1);
assert result.get(0).x == 0;
assert result.get(0).y == 0;
@@ -50,7 +54,7 @@ public static void main(String[] args) {
int imageWidth = 600;
double offsetX = imageWidth / 10.;
double offsetY = imageWidth / 3.7;
- BufferedImage image = GetKochSnowflake(imageWidth, 5);
+ BufferedImage image = getKochSnowflake(imageWidth, 5);
// The background should be white
assert image.getRGB(0, 0) == new Color(255, 255, 255).getRGB();
@@ -76,10 +80,10 @@ public static void main(String[] args) {
* @param steps The number of iterations.
* @return The transformed vectors after the iteration-steps.
*/
- public static ArrayList Iterate(ArrayList initialVectors, int steps) {
+ public static ArrayList iterate(ArrayList initialVectors, int steps) {
ArrayList vectors = initialVectors;
for (int i = 0; i < steps; i++) {
- vectors = IterationStep(vectors);
+ vectors = iterationStep(vectors);
}
return vectors;
@@ -92,7 +96,7 @@ public static ArrayList Iterate(ArrayList initialVectors, int
* @param steps The number of iterations.
* @return The image of the rendered Koch snowflake.
*/
- public static BufferedImage GetKochSnowflake(int imageWidth, int steps) {
+ public static BufferedImage getKochSnowflake(int imageWidth, int steps) {
if (imageWidth <= 0) {
throw new IllegalArgumentException("imageWidth should be greater than zero");
}
@@ -107,8 +111,8 @@ public static BufferedImage GetKochSnowflake(int imageWidth, int steps) {
initialVectors.add(vector2);
initialVectors.add(vector3);
initialVectors.add(vector1);
- ArrayList vectors = Iterate(initialVectors, steps);
- return GetImage(vectors, imageWidth, imageWidth);
+ ArrayList vectors = iterate(initialVectors, steps);
+ return getImage(vectors, imageWidth, imageWidth);
}
/**
@@ -121,7 +125,7 @@ public static BufferedImage GetKochSnowflake(int imageWidth, int steps) {
* applied.
* @return The transformed vectors after the iteration-step.
*/
- private static ArrayList IterationStep(ArrayList