Skip to content

Commit 4c57693

Browse files
authored
Merge branch 'master' into eulerpseudoprime
2 parents e746cca + 8ca2d9f commit 4c57693

File tree

12 files changed

+570
-7
lines changed

12 files changed

+570
-7
lines changed

pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@
7171
<artifactId>maven-compiler-plugin</artifactId>
7272
<version>3.14.1</version>
7373
<configuration>
74-
<source>21</source>
75-
<target>21</target>
74+
<release>21</release>
7675
<compilerArgs>
7776
<arg>-Xlint:all</arg>
7877
<arg>-Xlint:-auxiliaryclass</arg>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* A utility class to convert between Cartesian and Polar coordinate systems.
5+
*
6+
* <p>This class provides methods to perform the following conversions:
7+
* <ul>
8+
* <li>Cartesian to Polar coordinates</li>
9+
* <li>Polar to Cartesian coordinates</li>
10+
* </ul>
11+
*
12+
* <p>The class is final and cannot be instantiated.
13+
*/
14+
public final class CoordinateConverter {
15+
16+
private CoordinateConverter() {
17+
// Prevent instantiation
18+
}
19+
20+
/**
21+
* Converts Cartesian coordinates to Polar coordinates.
22+
*
23+
* @param x the x-coordinate in the Cartesian system; must be a finite number
24+
* @param y the y-coordinate in the Cartesian system; must be a finite number
25+
* @return an array where the first element is the radius (r) and the second element is the angle (theta) in degrees
26+
* @throws IllegalArgumentException if x or y is not a finite number
27+
*/
28+
public static double[] cartesianToPolar(double x, double y) {
29+
if (!Double.isFinite(x) || !Double.isFinite(y)) {
30+
throw new IllegalArgumentException("x and y must be finite numbers.");
31+
}
32+
double r = Math.sqrt(x * x + y * y);
33+
double theta = Math.toDegrees(Math.atan2(y, x));
34+
return new double[] {r, theta};
35+
}
36+
37+
/**
38+
* Converts Polar coordinates to Cartesian coordinates.
39+
*
40+
* @param r the radius in the Polar system; must be non-negative
41+
* @param thetaDegrees the angle (theta) in degrees in the Polar system; must be a finite number
42+
* @return an array where the first element is the x-coordinate and the second element is the y-coordinate in the Cartesian system
43+
* @throws IllegalArgumentException if r is negative or thetaDegrees is not a finite number
44+
*/
45+
public static double[] polarToCartesian(double r, double thetaDegrees) {
46+
if (r < 0) {
47+
throw new IllegalArgumentException("Radius (r) must be non-negative.");
48+
}
49+
if (!Double.isFinite(thetaDegrees)) {
50+
throw new IllegalArgumentException("Theta (angle) must be a finite number.");
51+
}
52+
double theta = Math.toRadians(thetaDegrees);
53+
double x = r * Math.cos(theta);
54+
double y = r * Math.sin(theta);
55+
return new double[] {x, y};
56+
}
57+
}

src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
import java.util.Arrays;
13
/**
24
* @author Md Asif Joardar
35
*
@@ -13,11 +15,6 @@
1315
*
1416
* The time complexity of the solution is O(n × sum) and requires O(n × sum) space
1517
*/
16-
17-
package com.thealgorithms.dynamicprogramming;
18-
19-
import java.util.Arrays;
20-
2118
public final class PartitionProblem {
2219
private PartitionProblem() {
2320
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.geometry;
2+
/**
3+
* This Class implements the Haversine formula to calculate the distance between two points on a sphere (like Earth) from their latitudes and longitudes.
4+
*
5+
* The Haversine formula is used in navigation and mapping to find the great-circle distance,
6+
* which is the shortest distance between two points along the surface of a sphere. It is often
7+
* used to calculate the "as the crow flies" distance between two geographical locations.
8+
*
9+
* The formula is reliable for all distances, including small ones, and avoids issues with
10+
* numerical instability that can affect other methods.
11+
*
12+
* @see "https://en.wikipedia.org/wiki/Haversine_formula" - Wikipedia
13+
*/
14+
public final class Haversine {
15+
16+
// Average radius of Earth in kilometers
17+
private static final double EARTH_RADIUS_KM = 6371.0;
18+
19+
/**
20+
* Private constructor to prevent instantiation of this utility class.
21+
*/
22+
private Haversine() {
23+
}
24+
25+
/**
26+
* Calculates the great-circle distance between two points on the earth
27+
* (specified in decimal degrees).
28+
*
29+
* @param lat1 Latitude of the first point in decimal degrees.
30+
* @param lon1 Longitude of the first point in decimal degrees.
31+
* @param lat2 Latitude of the second point in decimal degrees.
32+
* @param lon2 Longitude of the second point in decimal degrees.
33+
* @return The distance between the two points in kilometers.
34+
*/
35+
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
36+
// Convert latitude and longitude from degrees to radians
37+
double dLat = Math.toRadians(lat2 - lat1);
38+
double dLon = Math.toRadians(lon2 - lon1);
39+
40+
double lat1Rad = Math.toRadians(lat1);
41+
double lat2Rad = Math.toRadians(lat2);
42+
43+
// Apply the Haversine formula
44+
double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1Rad) * Math.cos(lat2Rad);
45+
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
46+
47+
return EARTH_RADIUS_KM * c;
48+
}
49+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.thealgorithms.graph;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Arrays;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
/**
9+
* 0-1 BFS for shortest paths on graphs with edges weighted 0 or 1.
10+
*
11+
* <p>Time Complexity: O(V + E). Space Complexity: O(V).
12+
*
13+
* <p>References:
14+
* <ul>
15+
* <li>https://cp-algorithms.com/graph/01_bfs.html</li>
16+
* </ul>
17+
*/
18+
public final class ZeroOneBfs {
19+
20+
private ZeroOneBfs() {
21+
// Utility class; do not instantiate.
22+
}
23+
24+
/**
25+
* Computes shortest distances from {@code src} in a graph whose edges have weight 0 or 1.
26+
*
27+
* @param n the number of vertices, labeled {@code 0..n-1}
28+
* @param adj adjacency list; for each vertex u, {@code adj.get(u)} is a list of pairs
29+
* {@code (v, w)} where {@code v} is a neighbor and {@code w} is 0 or 1
30+
* @param src the source vertex
31+
* @return an array of distances; {@code Integer.MAX_VALUE} denotes unreachable
32+
* @throws IllegalArgumentException if {@code n < 0}, {@code src} is out of range,
33+
* or any edge has weight other than 0 or 1
34+
*/
35+
public static int[] shortestPaths(int n, List<List<int[]>> adj, int src) {
36+
if (n < 0 || src < 0 || src >= n) {
37+
throw new IllegalArgumentException("Invalid n or src");
38+
}
39+
int[] dist = new int[n];
40+
Arrays.fill(dist, Integer.MAX_VALUE);
41+
Deque<Integer> dq = new ArrayDeque<>();
42+
43+
dist[src] = 0;
44+
dq.addFirst(src);
45+
46+
while (!dq.isEmpty()) {
47+
int u = dq.pollFirst();
48+
List<int[]> edges = adj.get(u);
49+
if (edges == null) {
50+
continue;
51+
}
52+
for (int[] e : edges) {
53+
if (e == null || e.length < 2) {
54+
continue;
55+
}
56+
int v = e[0];
57+
int w = e[1];
58+
if (v < 0 || v >= n) {
59+
continue; // ignore bad edges
60+
}
61+
if (w != 0 && w != 1) {
62+
throw new IllegalArgumentException("Edge weight must be 0 or 1");
63+
}
64+
int nd = dist[u] + w;
65+
if (nd < dist[v]) {
66+
dist[v] = nd;
67+
if (w == 0) {
68+
dq.addFirst(v);
69+
} else {
70+
dq.addLast(v);
71+
}
72+
}
73+
}
74+
}
75+
return dist;
76+
}
77+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* A utility class for calculating the persistence of a number.
5+
*
6+
* <p>This class provides methods to calculate:
7+
* <ul>
8+
* <li>Multiplicative persistence: The number of steps required to reduce a number to a single digit by multiplying its digits.</li>
9+
* <li>Additive persistence: The number of steps required to reduce a number to a single digit by summing its digits.</li>
10+
* </ul>
11+
*
12+
* <p>This class is final and cannot be instantiated.
13+
*
14+
* @see <a href="https://en.wikipedia.org/wiki/Persistence_of_a_number">Wikipedia: Persistence of a number</a>
15+
*/
16+
public final class NumberPersistence {
17+
18+
// Private constructor to prevent instantiation
19+
private NumberPersistence() {
20+
}
21+
22+
/**
23+
* Calculates the multiplicative persistence of a given number.
24+
*
25+
* <p>Multiplicative persistence is the number of steps required to reduce a number to a single digit
26+
* by multiplying its digits repeatedly.
27+
*
28+
* @param num the number to calculate persistence for; must be non-negative
29+
* @return the multiplicative persistence of the number
30+
* @throws IllegalArgumentException if the input number is negative
31+
*/
32+
public static int multiplicativePersistence(int num) {
33+
if (num < 0) {
34+
throw new IllegalArgumentException("multiplicativePersistence() does not accept negative values");
35+
}
36+
37+
int steps = 0;
38+
while (num >= 10) {
39+
int product = 1;
40+
int temp = num;
41+
while (temp > 0) {
42+
product *= temp % 10;
43+
temp /= 10;
44+
}
45+
num = product;
46+
steps++;
47+
}
48+
return steps;
49+
}
50+
51+
/**
52+
* Calculates the additive persistence of a given number.
53+
*
54+
* <p>Additive persistence is the number of steps required to reduce a number to a single digit
55+
* by summing its digits repeatedly.
56+
*
57+
* @param num the number to calculate persistence for; must be non-negative
58+
* @return the additive persistence of the number
59+
* @throws IllegalArgumentException if the input number is negative
60+
*/
61+
public static int additivePersistence(int num) {
62+
if (num < 0) {
63+
throw new IllegalArgumentException("additivePersistence() does not accept negative values");
64+
}
65+
66+
int steps = 0;
67+
while (num >= 10) {
68+
int sum = 0;
69+
int temp = num;
70+
while (temp > 0) {
71+
sum += temp % 10;
72+
temp /= 10;
73+
}
74+
num = sum;
75+
steps++;
76+
}
77+
return steps;
78+
}
79+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.recursion;
2+
3+
import java.math.BigInteger;
4+
5+
/**
6+
* A utility class for calculating numbers in Sylvester's sequence.
7+
*
8+
* <p>Sylvester's sequence is a sequence of integers where each term is calculated
9+
* using the formula:
10+
* <pre>
11+
* a(n) = a(n-1) * (a(n-1) - 1) + 1
12+
* </pre>
13+
* with the first term being 2.
14+
*
15+
* <p>This class is final and cannot be instantiated.
16+
*
17+
* @see <a href="https://en.wikipedia.org/wiki/Sylvester_sequence">Wikipedia: Sylvester sequence</a>
18+
*/
19+
public final class SylvesterSequence {
20+
21+
// Private constructor to prevent instantiation
22+
private SylvesterSequence() {
23+
}
24+
25+
/**
26+
* Calculates the nth number in Sylvester's sequence.
27+
*
28+
* <p>The sequence is defined recursively, with the first term being 2:
29+
* <pre>
30+
* a(1) = 2
31+
* a(n) = a(n-1) * (a(n-1) - 1) + 1 for n > 1
32+
* </pre>
33+
*
34+
* @param n the position in the sequence (must be greater than 0)
35+
* @return the nth number in Sylvester's sequence
36+
* @throws IllegalArgumentException if n is less than or equal to 0
37+
*/
38+
public static BigInteger sylvester(int n) {
39+
if (n <= 0) {
40+
throw new IllegalArgumentException("sylvester() does not accept negative numbers or zero.");
41+
}
42+
if (n == 1) {
43+
return BigInteger.valueOf(2);
44+
} else {
45+
BigInteger prev = sylvester(n - 1);
46+
// Sylvester sequence formula: a(n) = a(n-1) * (a(n-1) - 1) + 1
47+
return prev.multiply(prev.subtract(BigInteger.ONE)).add(BigInteger.ONE);
48+
}
49+
}
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
9+
public class CoordinateConverterTest {
10+
11+
@ParameterizedTest
12+
@CsvSource({"0, 0, 0, 0", "1, 0, 1, 0", "0, 1, 1, 90", "-1, 0, 1, 180", "0, -1, 1, -90", "3, 4, 5, 53.13010235415599"})
13+
void testCartesianToPolar(double x, double y, double expectedR, double expectedTheta) {
14+
assertArrayEquals(new double[] {expectedR, expectedTheta}, CoordinateConverter.cartesianToPolar(x, y), 1e-9);
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource({"1, 0, 1, 0", "1, 90, 0, 1", "1, 180, -1, 0", "1, -90, 0, -1", "5, 53.13010235415599, 3, 4"})
19+
void testPolarToCartesian(double r, double theta, double expectedX, double expectedY) {
20+
assertArrayEquals(new double[] {expectedX, expectedY}, CoordinateConverter.polarToCartesian(r, theta), 1e-9);
21+
}
22+
23+
@ParameterizedTest
24+
@CsvSource({"NaN, 1", "1, NaN", "Infinity, 1", "1, Infinity", "-Infinity, 1", "1, -Infinity"})
25+
void testCartesianToPolarInvalidInputs(double x, double y) {
26+
assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.cartesianToPolar(x, y));
27+
}
28+
29+
@ParameterizedTest
30+
@CsvSource({"-1, 0", "1, NaN", "1, Infinity", "1, -Infinity"})
31+
void testPolarToCartesianInvalidInputs(double r, double theta) {
32+
assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.polarToCartesian(r, theta));
33+
}
34+
}

0 commit comments

Comments
 (0)