Skip to content

Commit 770e092

Browse files
Merge branch 'master' into fix-7482-base64-padding-validation
2 parents df66ec8 + a508fd2 commit 770e092

8 files changed

Lines changed: 178 additions & 3 deletions

File tree

src/main/java/com/thealgorithms/tree/HeavyLightDecomposition.java renamed to src/main/java/com/thealgorithms/datastructures/trees/HeavyLightDecomposition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.tree;
1+
package com.thealgorithms.datastructures.trees;
22

33
import java.util.ArrayList;
44
import java.util.List;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Two numbers are Friendly if they share the same abundancy index,
5+
* which is the ratio of the sum of divisors to the number itself.
6+
* Example: 6 and 28 are friendly because sigma(6)/6 = 2 and sigma(28)/28 = 2
7+
*
8+
* @see <a href="https://en.wikipedia.org/wiki/Friendly_number">
9+
* Wikipedia: Friendly Number</a>
10+
*
11+
* @author Vraj Prajapati @Rosander0
12+
*/
13+
public final class FriendlyNumber {
14+
15+
private FriendlyNumber() {
16+
// Utility class
17+
}
18+
19+
private static int sumOfDivisors(final int number) {
20+
int sum = 0;
21+
final int root = (int) Math.sqrt(number);
22+
for (int i = 1; i <= root; i++) {
23+
if (number % i == 0) {
24+
sum += i;
25+
final int other = number / i;
26+
if (other != i) {
27+
sum += other;
28+
}
29+
}
30+
}
31+
return sum;
32+
}
33+
34+
/**
35+
* Checks whether two numbers are Friendly Numbers.
36+
*
37+
* @param a First number (must be positive)
38+
* @param b Second number (must be positive)
39+
* @return true if a and b are friendly numbers, false otherwise
40+
*/
41+
public static boolean areFriendly(final int a, final int b) {
42+
if (a <= 0 || b <= 0) {
43+
return false;
44+
}
45+
final long sigmaA = sumOfDivisors(a);
46+
final long sigmaB = sumOfDivisors(b);
47+
return sigmaA * b == sigmaB * a;
48+
}
49+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* The Padovan Sequence is a sequence of integers defined by the recurrence relation:
5+
* P(n) = P(n-2) + P(n-3) with initial values P(0) = P(1) = P(2) = 1.
6+
* Example: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37...
7+
*
8+
* @see <a href="https://en.wikipedia.org/wiki/Padovan_sequence">
9+
* Wikipedia: Padovan Sequence</a>
10+
* @author Vraj Prajapati (@Rosander0)
11+
*/
12+
public final class PadovanSequence {
13+
14+
private PadovanSequence() {
15+
// Utility class
16+
}
17+
18+
/**
19+
* Calculates the nth term of the Padovan Sequence.
20+
*
21+
* @param n the index of the sequence (must be non-negative)
22+
* @return the nth term of the Padovan Sequence
23+
*/
24+
public static long padovan(final int n) {
25+
if (n < 0) {
26+
throw new IllegalArgumentException("Input must be non-negative. Received: " + n);
27+
}
28+
if (n <= 2) {
29+
return 1;
30+
}
31+
long a = 1;
32+
long b = 1;
33+
long c = 1;
34+
long result = 0;
35+
for (int i = 3; i <= n; i++) {
36+
result = a + b;
37+
a = b;
38+
b = c;
39+
c = result;
40+
}
41+
return result;
42+
}
43+
}

src/main/java/com/thealgorithms/searches/InterpolationSearch.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public int find(int[] array, int key) {
3636
// Since array is sorted, an element present
3737
// in array must be in range defined by corner
3838
while (start <= end && key >= array[start] && key <= array[end]) {
39+
if (array[start] == array[end]) {
40+
return start;
41+
}
3942
// Probing the position with keeping
4043
// uniform distribution in mind.
41-
int pos = start + (((end - start) / (array[end] - array[start])) * (key - array[start]));
44+
int pos = start + (int) (((long) (end - start) * (key - array[start])) / ((long) array[end] - array[start]));
4245

4346
// Condition of target found
4447
if (array[pos] == key) {

src/test/java/com/thealgorithms/tree/HeavyLightDecompositionTest.java renamed to src/test/java/com/thealgorithms/datastructures/trees/HeavyLightDecompositionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.tree;
1+
package com.thealgorithms.datastructures.trees;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.maths;
2+
// author: Vraj Prajapati @Rosander0
3+
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class FriendlyNumberTest {
10+
11+
@Test
12+
public void testFriendlyNumbers() {
13+
// 6 and 28 are friendly (abundancy index = 2)
14+
assertTrue(FriendlyNumber.areFriendly(6, 28));
15+
// Every number is friendly with itself
16+
assertTrue(FriendlyNumber.areFriendly(6, 6));
17+
assertTrue(FriendlyNumber.areFriendly(1, 1));
18+
}
19+
20+
@Test
21+
public void testNonFriendlyNumbers() {
22+
assertFalse(FriendlyNumber.areFriendly(6, 10));
23+
assertFalse(FriendlyNumber.areFriendly(10, 15));
24+
assertFalse(FriendlyNumber.areFriendly(4, 9));
25+
}
26+
27+
@Test
28+
public void testInvalidInputs() {
29+
assertFalse(FriendlyNumber.areFriendly(0, 6));
30+
assertFalse(FriendlyNumber.areFriendly(-1, 6));
31+
assertFalse(FriendlyNumber.areFriendly(6, -1));
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* @author Vraj Prajapati (@Rosander0)
10+
*/
11+
public class PadovanSequenceTest {
12+
13+
@Test
14+
public void testBaseCase() {
15+
assertEquals(1, PadovanSequence.padovan(0));
16+
assertEquals(1, PadovanSequence.padovan(1));
17+
assertEquals(1, PadovanSequence.padovan(2));
18+
}
19+
20+
@Test
21+
public void testKnownValues() {
22+
assertEquals(2, PadovanSequence.padovan(3));
23+
assertEquals(2, PadovanSequence.padovan(4));
24+
assertEquals(3, PadovanSequence.padovan(5));
25+
assertEquals(4, PadovanSequence.padovan(6));
26+
assertEquals(5, PadovanSequence.padovan(7));
27+
assertEquals(7, PadovanSequence.padovan(8));
28+
assertEquals(9, PadovanSequence.padovan(9));
29+
assertEquals(12, PadovanSequence.padovan(10));
30+
}
31+
32+
@Test
33+
public void testInvalidInput() {
34+
assertThrows(IllegalArgumentException.class, () -> PadovanSequence.padovan(-1));
35+
}
36+
}

src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,15 @@ void testInterpolationSearchLargeNonUniformArray() {
8787
int key = 21; // Present in the array
8888
assertEquals(6, interpolationSearch.find(array, key), "The index of the found element should be 6.");
8989
}
90+
91+
/**
92+
* Test for interpolation search with specific sorted arrays that previously caused division by zero.
93+
*/
94+
@Test
95+
void testInterpolationSearchDivisionByZeroEdgeCases() {
96+
InterpolationSearch interpolationSearch = new InterpolationSearch();
97+
assertEquals(3, interpolationSearch.find(new int[] {0, 0, 0, 2}, 2));
98+
assertEquals(0, interpolationSearch.find(new int[] {2, 2, 2, 2}, 2));
99+
assertEquals(3, interpolationSearch.find(new int[] {0, 1, 2, 4}, 4));
100+
}
90101
}

0 commit comments

Comments
 (0)