Skip to content

Refactor and optimize repository #6159

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.backtracking;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -17,10 +19,9 @@ private ParenthesesGenerator() {
* @return A list of strings representing valid combinations of parentheses.
* @throws IllegalArgumentException if n is less than 0.
*/
public static List<String> generateParentheses(final int n) {
if (n < 0) {
throw new IllegalArgumentException("The number of pairs of parentheses cannot be negative");
}
public static List<String> generateParentheses(final int n) {
checkInputIsPositive(n, "The number of pairs of parentheses cannot be negative.");

List<String> result = new ArrayList<>();
generateParenthesesHelper(result, "", 0, 0, n);
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.bitmanipulation;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

import java.util.Optional;

/**
Expand Down Expand Up @@ -34,10 +36,8 @@ private HighestSetBit() {
* Returns {@link Optional#empty()} if the number is 0.
* @throws IllegalArgumentException if the input number is negative.
*/
public static Optional<Integer> findHighestSetBit(int num) {
if (num < 0) {
throw new IllegalArgumentException("Input cannot be negative");
}
public static Optional<Integer> findHighestSetBit(int num) {
checkInputIsPositive(num, "Input cannot be negative.");

if (num == 0) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.datastructures.buffers;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

import java.util.concurrent.atomic.AtomicInteger;

/**
Expand All @@ -23,9 +25,8 @@ public class CircularBuffer<Item> {
* @throws IllegalArgumentException if the size is zero or negative.
*/
public CircularBuffer(int size) {
if (size <= 0) {
throw new IllegalArgumentException("Buffer size must be positive");
}
checkInputIsPositive(size, "Buffer size must be positive.");

// noinspection unchecked
this.buffer = (Item[]) new Object[size];
this.putPointer = new CircularPointer(0, size);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.datastructures.graphs;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -43,9 +45,8 @@ static class Graph {
* @param edges list of edges
*/
Graph(final int vertex, final List<Edge> edges) {
if (vertex < 0) {
throw new IllegalArgumentException("Number of vertices must be positive");
}
checkInputIsPositive(vertex, "Number of vertices must be positive.");

if (edges == null || edges.isEmpty()) {
throw new IllegalArgumentException("Edges list must not be null or empty");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.thealgorithms.maths;
package com.thealgorithms.geometry;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

/**
* Find the area of various geometric shapes
Expand Down Expand Up @@ -89,9 +91,8 @@ public static double surfaceAreaCylinder(final double radius, final double heigh
* @return area of given square
*/
public static double surfaceAreaSquare(final double sideLength) {
if (sideLength <= 0) {
throw new IllegalArgumentException("Must be a positive sideLength");
}
checkInputIsPositive(sideLength, "Must be a positive sideLength.");

return sideLength * sideLength;
}

Expand All @@ -103,12 +104,8 @@ public static double surfaceAreaSquare(final double sideLength) {
* @return area of given triangle
*/
public static double surfaceAreaTriangle(final double base, final double height) {
if (base <= 0) {
throw new IllegalArgumentException(POSITIVE_BASE);
}
if (height <= 0) {
throw new IllegalArgumentException(POSITIVE_HEIGHT);
}
checkInputIsPositive(base, POSITIVE_BASE);
checkInputIsPositive(height, POSITIVE_HEIGHT);
return base * height / 2;
}

Expand All @@ -120,12 +117,8 @@ public static double surfaceAreaTriangle(final double base, final double height)
* @return area of given parallelogram
*/
public static double surfaceAreaParallelogram(final double base, final double height) {
if (base <= 0) {
throw new IllegalArgumentException(POSITIVE_BASE);
}
if (height <= 0) {
throw new IllegalArgumentException(POSITIVE_HEIGHT);
}
checkInputIsPositive(base, POSITIVE_BASE);
checkInputIsPositive(height, POSITIVE_HEIGHT);
return base * height;
}

Expand All @@ -144,9 +137,7 @@ public static double surfaceAreaTrapezium(final double base1, final double base2
if (base2 <= 0) {
throw new IllegalArgumentException(POSITIVE_BASE + 2);
}
if (height <= 0) {
throw new IllegalArgumentException(POSITIVE_HEIGHT);
}
checkInputIsPositive(height, POSITIVE_HEIGHT);
return (base1 + base2) * height / 2;
}

Expand All @@ -157,9 +148,8 @@ public static double surfaceAreaTrapezium(final double base1, final double base2
* @return area of given circle
*/
public static double surfaceAreaCircle(final double radius) {
if (radius <= 0) {
throw new IllegalArgumentException(POSITIVE_RADIUS);
}
checkInputIsPositive(radius, POSITIVE_RADIUS);

return Math.PI * radius * radius;
}

Expand All @@ -170,9 +160,8 @@ public static double surfaceAreaCircle(final double radius) {
* @return surface area of given hemisphere
*/
public static double surfaceAreaHemisphere(final double radius) {
if (radius <= 0) {
throw new IllegalArgumentException(POSITIVE_RADIUS);
}
checkInputIsPositive(radius, POSITIVE_RADIUS);

return 3 * Math.PI * radius * radius;
}

Expand All @@ -184,12 +173,9 @@ public static double surfaceAreaHemisphere(final double radius) {
* @return surface area of given cone.
*/
public static double surfaceAreaCone(final double radius, final double height) {
if (radius <= 0) {
throw new IllegalArgumentException(POSITIVE_RADIUS);
}
if (height <= 0) {
throw new IllegalArgumentException(POSITIVE_HEIGHT);
}
checkInputIsPositive(radius, POSITIVE_RADIUS);
checkInputIsPositive(height, POSITIVE_HEIGHT);

return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithms.maths;
package com.thealgorithms.geometry;

/* Calculate the volume of various shapes.*/
public final class Volume {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/thealgorithms/maths/Armstrong.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.maths;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

/**
* This class checks whether a given number is an Armstrong number or not.
* An Armstrong number is a number that is equal to the sum of its own digits,
Expand All @@ -21,9 +23,8 @@ public class Armstrong {
* @return {@code true} if the given number is an Armstrong number, {@code false} otherwise
*/
public boolean isArmstrong(int number) {
if (number < 0) {
return false; // Negative numbers cannot be Armstrong numbers
}
checkInputIsPositive(number, "Negative numbers cannot be Armstrong numbers.");

long sum = 0;
int totalDigits = (int) Math.log10(number) + 1; // get the length of the number (number of digits)
long originalNumber = number;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/thealgorithms/maths/Combinations.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.maths;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

/**
* @see <a href="https://en.wikipedia.org/wiki/Combination">Combination</a>
*/
Expand All @@ -14,9 +16,8 @@ private Combinations() {
* @return factorial of given number
*/
public static long factorial(int n) {
if (n < 0) {
throw new IllegalArgumentException("number is negative");
}
checkInputIsPositive(n, "Input number cannot be negative.");

return n == 0 || n == 1 ? 1 : n * factorial(n - 1);
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/thealgorithms/maths/DudeneyNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
*/
package com.thealgorithms.maths;

import static com.thealgorithms.maths.PerfectCube.getCubeRoot;
import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

public final class DudeneyNumber {
private DudeneyNumber() {
}

// returns True if the number is a Dudeney number and False if it is not a Dudeney number.
public static boolean isDudeney(final int n) {
if (n <= 0) {
throw new IllegalArgumentException("Input must me positive.");
}
checkInputIsPositive(n, "Input number should be positive.");
// Calculating Cube Root
final int cubeRoot = (int) Math.round(Math.pow(n, 1.0 / 3.0));
final int cubeRoot = getCubeRoot(n);
// If the number is not a perfect cube the method returns false.
if (cubeRoot * cubeRoot * cubeRoot != n) {
return false;
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/com/thealgorithms/maths/EulersFunction.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package com.thealgorithms.maths;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

/**
* Utility class for computing
* <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>.
*/
public final class EulersFunction {
private EulersFunction() {
}

/**
* Validates that the input is a positive integer.
*
* @param n the input number to validate
* @throws IllegalArgumentException if {@code n} is non-positive
*/
private static void checkInput(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive.");
}
}
}

/**
* Computes the value of Euler's totient function for a given input.
Expand All @@ -29,7 +19,7 @@ private static void checkInput(int n) {
* @throws IllegalArgumentException if {@code n} is non-positive
*/
public static int getEuler(int n) {
checkInput(n);
checkInputIsPositive(n, "Input number should be positive.");
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/thealgorithms/maths/Factorial.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.maths;

import static com.thealgorithms.maths.utils.MathsUtil.checkInputIsPositive;

public final class Factorial {
private Factorial() {
}
Expand All @@ -11,9 +13,7 @@ private Factorial() {
* @return the factorial of {@code n}
*/
public static long factorial(int n) {
if (n < 0) {
throw new IllegalArgumentException("Input number cannot be negative");
}
checkInputIsPositive(n, "");
long factorial = 1;
for (int i = 1; i <= n; ++i) {
factorial *= i;
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.maths;

import static com.thealgorithms.maths.PerfectSquare.isPerfectSquare;

/**
* Fibonacci: 0 1 1 2 3 5 8 13 21 ...
* This code checks Fibonacci Numbers up to 45th number.
Expand All @@ -8,17 +10,6 @@
public final class FibonacciNumberCheck {
private FibonacciNumberCheck() {
}
/**
* Check if a number is perfect square number
*
* @param number the number to be checked
* @return <tt>true</tt> if {@code number} is a perfect square, otherwise
* <tt>false</tt>
*/
public static boolean isPerfectSquare(long number) {
long sqrt = (long) Math.sqrt(number);
return sqrt * sqrt == number;
}

/**
* Check if a number is a Fibonacci number. This is true if and only if at
Expand All @@ -29,9 +20,9 @@ public static boolean isPerfectSquare(long number) {
* <tt>false</tt>
* @link https://en.wikipedia.org/wiki/Fibonacci_number#Identification
*/
public static boolean isFibonacciNumber(long number) {
long value1 = 5 * number * number + 4;
long value2 = 5 * number * number - 4;
public static boolean isFibonacciNumber(int number) {
int value1 = 5 * number * number + 4;
int value2 = 5 * number * number - 4;
return isPerfectSquare(value1) || isPerfectSquare(value2);
}
}
13 changes: 2 additions & 11 deletions src/main/java/com/thealgorithms/maths/PalindromeNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,8 @@ private PalindromeNumber() {
* @return {@code true} if {@code n} is palindrome number, otherwise
* {@code false}
*/
public static boolean isPalindrome(int number) {
if (number < 0) {
throw new IllegalArgumentException("Input parameter must not be negative!");
}
int numberCopy = number;
int reverseNumber = 0;
while (numberCopy != 0) {
int remainder = numberCopy % 10;
reverseNumber = reverseNumber * 10 + remainder;
numberCopy /= 10;
}
public static boolean isPalindrome(int number) {
int reverseNumber = ReverseNumber.reverseNumber(number);
return number == reverseNumber;
}
}
13 changes: 11 additions & 2 deletions src/main/java/com/thealgorithms/maths/PerfectCube.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ public final class PerfectCube {
private PerfectCube() {
}

/**
* Get cuberoot of a number
*
* @param number number to get cube root of
*/
public static int getCubeRoot(int number) {
number = Math.abs(number); // converting negative number to positive number
return (int) Math.pow(number, 1.0 / 3);
}

/**
* Check if a number is perfect cube or not
*
Expand All @@ -15,8 +25,7 @@ private PerfectCube() {
* {@code false}
*/
public static boolean isPerfectCube(int number) {
number = Math.abs(number); // converting negative number to positive number
int a = (int) Math.pow(number, 1.0 / 3);
int a = getCubeRoot(number);
return a * a * a == number;
}

Expand Down
Loading
Loading