Skip to content

Commit 87f9ebc

Browse files
authored
Add Frizzy Number (fixes #3379) (#3906)
1 parent f361338 commit 87f9ebc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
5+
/** Program description - To find the FrizzyNumber*/
6+
7+
8+
package com.thealgorithms.maths;
9+
10+
public class FrizzyNumber {
11+
12+
/**
13+
* Returns the n-th number that is a sum of powers
14+
* of the given base.
15+
* Example: base = 3 and n = 4
16+
* Ascending order of sums of powers of 3 =
17+
* 3^0 = 1, 3^1 = 3, 3^1 + 3^0 = 4, 3^2 + 3^0 = 9
18+
* Ans = 9
19+
*
20+
* @param base The base whose n-th sum of powers is required
21+
* @param n Index from ascending order of sum of powers of base
22+
* @return n-th sum of powers of base
23+
*/
24+
public static double getNthFrizzy(int base, int n) {
25+
double final1 = 0.0;
26+
int i = 0;
27+
do
28+
{
29+
final1 += Math.pow(base, i++) * (n % 2);
30+
} while ((n /= 2) > 0);
31+
return final1;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
public class FrizzyNumberTest {
7+
@Test
8+
public void testFrizziesForBase2() {
9+
assertEquals(
10+
1,
11+
FrizzyNumber.getNthFrizzy(2, 1));
12+
assertEquals(
13+
3,
14+
FrizzyNumber.getNthFrizzy(2, 3));
15+
assertEquals(
16+
1000,
17+
FrizzyNumber.getNthFrizzy(2, 1000));
18+
}
19+
20+
@Test
21+
public void testFrizziesForBase3() {
22+
assertEquals(
23+
1,
24+
FrizzyNumber.getNthFrizzy(3, 1));
25+
assertEquals(
26+
3,
27+
FrizzyNumber.getNthFrizzy(3, 2));
28+
assertEquals(
29+
29430,
30+
FrizzyNumber.getNthFrizzy(3, 1000));
31+
}
32+
33+
@Test
34+
public void testFrizziesForBase69() {
35+
assertEquals(
36+
1,
37+
FrizzyNumber.getNthFrizzy(69, 1));
38+
assertEquals(
39+
69,
40+
FrizzyNumber.getNthFrizzy(69, 2));
41+
assertEquals(
42+
328510,
43+
FrizzyNumber.getNthFrizzy(69, 9));
44+
assertEquals(
45+
333340,
46+
FrizzyNumber.getNthFrizzy(69, 15));
47+
}
48+
}

0 commit comments

Comments
 (0)