Skip to content

Commit c37f4a5

Browse files
authored
Merge branch 'master' into euler_add_tests
2 parents 9ea6ed7 + 9eff71b commit c37f4a5

File tree

8 files changed

+277
-140
lines changed

8 files changed

+277
-140
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@
938938
* [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java)
939939
* [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java)
940940
* [CombinationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CombinationsTest.java)
941+
* [ConvolutionFFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java)
941942
* [ConvolutionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionTest.java)
942943
* [CrossCorrelationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CrossCorrelationTest.java)
943944
* [DeterminantOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DeterminantOfMatrixTest.java)
@@ -1029,6 +1030,7 @@
10291030
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
10301031
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
10311032
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
1033+
* [PalindromePrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java)
10321034
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
10331035
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
10341036
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)

src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,62 @@
22

33
import java.util.concurrent.atomic.AtomicInteger;
44

5+
/**
6+
* The {@code CircularBuffer} class implements a generic circular (or ring) buffer.
7+
* A circular buffer is a fixed-size data structure that operates in a FIFO (First In, First Out) manner.
8+
* The buffer allows you to overwrite old data when the buffer is full and efficiently use limited memory.
9+
* When the buffer is full, adding a new item will overwrite the oldest data.
10+
*
11+
* @param <Item> The type of elements stored in the circular buffer.
12+
*/
513
public class CircularBuffer<Item> {
614
private final Item[] buffer;
715
private final CircularPointer putPointer;
816
private final CircularPointer getPointer;
917
private final AtomicInteger size = new AtomicInteger(0);
1018

19+
/**
20+
* Constructor to initialize the circular buffer with a specified size.
21+
*
22+
* @param size The size of the circular buffer.
23+
* @throws IllegalArgumentException if the size is zero or negative.
24+
*/
1125
public CircularBuffer(int size) {
26+
if (size <= 0) {
27+
throw new IllegalArgumentException("Buffer size must be positive");
28+
}
1229
// noinspection unchecked
1330
this.buffer = (Item[]) new Object[size];
1431
this.putPointer = new CircularPointer(0, size);
1532
this.getPointer = new CircularPointer(0, size);
1633
}
1734

35+
/**
36+
* Checks if the circular buffer is empty.
37+
* This method is based on the current size of the buffer.
38+
*
39+
* @return {@code true} if the buffer is empty, {@code false} otherwise.
40+
*/
1841
public boolean isEmpty() {
1942
return size.get() == 0;
2043
}
2144

45+
/**
46+
* Checks if the circular buffer is full.
47+
* The buffer is considered full when its size equals its capacity.
48+
*
49+
* @return {@code true} if the buffer is full, {@code false} otherwise.
50+
*/
2251
public boolean isFull() {
2352
return size.get() == buffer.length;
2453
}
2554

55+
/**
56+
* Retrieves and removes the item at the front of the buffer (FIFO).
57+
* This operation will move the {@code getPointer} forward.
58+
*
59+
* @return The item at the front of the buffer, or {@code null} if the buffer is empty.
60+
*/
2661
public Item get() {
2762
if (isEmpty()) {
2863
return null;
@@ -33,31 +68,64 @@ public Item get() {
3368
return item;
3469
}
3570

71+
/**
72+
* Adds an item to the end of the buffer (FIFO).
73+
* If the buffer is full, this operation will overwrite the oldest data.
74+
*
75+
* @param item The item to be added.
76+
* @throws IllegalArgumentException if the item is null.
77+
* @return {@code true} if the item was successfully added, {@code false} if the buffer was full and the item overwrote existing data.
78+
*/
3679
public boolean put(Item item) {
80+
if (item == null) {
81+
throw new IllegalArgumentException("Null items are not allowed");
82+
}
83+
84+
boolean wasEmpty = isEmpty();
3785
if (isFull()) {
38-
return false;
86+
getPointer.getAndIncrement(); // Move get pointer to discard oldest item
87+
} else {
88+
size.incrementAndGet();
3989
}
4090

4191
buffer[putPointer.getAndIncrement()] = item;
42-
size.incrementAndGet();
43-
return true;
92+
return wasEmpty;
4493
}
4594

95+
/**
96+
* The {@code CircularPointer} class is a helper class used to track the current index (pointer)
97+
* in the circular buffer.
98+
* The max value represents the capacity of the buffer.
99+
* The `CircularPointer` class ensures that the pointer automatically wraps around to 0
100+
* when it reaches the maximum index.
101+
* This is achieved in the `getAndIncrement` method, where the pointer
102+
* is incremented and then taken modulo the maximum value (`max`).
103+
* This operation ensures that the pointer always stays within the bounds of the buffer.
104+
*/
46105
private static class CircularPointer {
47106
private int pointer;
48107
private final int max;
49108

109+
/**
110+
* Constructor to initialize the circular pointer.
111+
*
112+
* @param pointer The initial position of the pointer.
113+
* @param max The maximum size (capacity) of the circular buffer.
114+
*/
50115
CircularPointer(int pointer, int max) {
51116
this.pointer = pointer;
52117
this.max = max;
53118
}
54119

120+
/**
121+
* Increments the pointer by 1 and wraps it around to 0 if it reaches the maximum value.
122+
* This ensures the pointer always stays within the buffer's bounds.
123+
*
124+
* @return The current pointer value before incrementing.
125+
*/
55126
public int getAndIncrement() {
56-
if (pointer == max) {
57-
pointer = 0;
58-
}
59127
int tmp = pointer;
60-
pointer++;
128+
pointer = (pointer + 1) % max;
61129
return tmp;
62130
}
63131
}

src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public void preOrder(Node localRoot) {
281281
}
282282

283283
/**
284-
* Prints rightChild - leftChild - root
284+
* Prints leftChild - rightChild - root
285285
*
286286
* @param localRoot The local root of the binary tree
287287
*/

src/main/java/com/thealgorithms/maths/FFT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ public Complex divide(double n) {
165165
temp.img = this.img / n;
166166
return temp;
167167
}
168+
169+
public double real() {
170+
return real;
171+
}
172+
173+
public double imaginary() {
174+
return img;
175+
}
168176
}
169177

170178
/**
Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Scanner;
3+
import java.util.ArrayList;
4+
import java.util.List;
45

56
public final class PalindromePrime {
67
private PalindromePrime() {
78
}
89

9-
public static void main(String[] args) { // Main function
10-
Scanner in = new Scanner(System.in);
11-
System.out.println("Enter the quantity of First Palindromic Primes you want");
12-
int n = in.nextInt(); // Input of how many first palindromic prime we want
13-
functioning(n); // calling function - functioning
14-
in.close();
15-
}
10+
public static boolean prime(int num) {
11+
if (num < 2) {
12+
return false; // Handle edge case for numbers < 2
13+
}
14+
if (num == 2) {
15+
return true; // 2 is prime
16+
}
17+
if (num % 2 == 0) {
18+
return false; // Even numbers > 2 are not prime
19+
}
1620

17-
public static boolean prime(int num) { // checking if number is prime or not
1821
for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {
1922
if (num % divisor == 0) {
20-
return false; // false if not prime
23+
return false;
2124
}
2225
}
23-
return true; // True if prime
26+
return true;
2427
}
2528

26-
public static int reverse(int n) { // Returns the reverse of the number
29+
public static int reverse(int n) {
2730
int reverse = 0;
2831
while (n != 0) {
29-
reverse *= 10;
30-
reverse += n % 10;
32+
reverse = reverse * 10 + (n % 10);
3133
n /= 10;
3234
}
3335
return reverse;
3436
}
3537

36-
public static void functioning(int y) {
37-
if (y == 0) {
38-
return;
38+
public static List<Integer> generatePalindromePrimes(int n) {
39+
List<Integer> palindromicPrimes = new ArrayList<>();
40+
if (n <= 0) {
41+
return palindromicPrimes; // Handle case for 0 or negative input
3942
}
40-
System.out.print(2 + "\n"); // print the first Palindromic Prime
43+
44+
palindromicPrimes.add(2); // 2 is the first palindromic prime
4145
int count = 1;
4246
int num = 3;
43-
while (count < y) {
44-
if (num == reverse(num) && prime(num)) { // number is prime and it's reverse is same
45-
count++; // counts check when to terminate while loop
46-
System.out.print(num + "\n"); // print the Palindromic Prime
47+
48+
while (count < n) {
49+
if (num == reverse(num) && prime(num)) {
50+
palindromicPrimes.add(num);
51+
count++;
4752
}
48-
num += 2; // inrease iterator value by two
53+
num += 2; // Skip even numbers
4954
}
55+
return palindromicPrimes;
5056
}
5157
}

0 commit comments

Comments
 (0)