Skip to content

Commit fbe4fb4

Browse files
committed
Add countDistinctPowers function and associated tests
1 parent 714bdab commit fbe4fb4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Diff for: Project-Euler/Problem029.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Problem - Distinct Powers
3+
*
4+
* Find the number of distinct terms generated by a^b for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100.
5+
*/
6+
7+
/**
8+
* Main function to count distinct powers a^b.
9+
*
10+
* @param {number} minA
11+
* @param {number} maxA
12+
* @param {number} minB
13+
* @param {number} maxB
14+
* @returns {number}
15+
*/
16+
function countDistinctPowers(minA, maxA, minB, maxB) {
17+
/**
18+
* Set to store distinct terms generated by a^b.
19+
*/
20+
const distinctTerms = new Set()
21+
22+
for (let a = minA; a <= maxA; a++) {
23+
for (let b = minB; b <= maxB; b++) {
24+
distinctTerms.add(Math.pow(a, b))
25+
}
26+
}
27+
28+
return distinctTerms.size
29+
}
30+
31+
const minA = 2
32+
const maxA = 100
33+
const minB = 2
34+
const maxB = 100
35+
36+
const result = countDistinctPowers(minA, maxA, minB, maxB)
37+
console.log(`Number of distinct terms: ${result}`)
38+
39+
export { countDistinctPowers }

Diff for: Project-Euler/test/Problem029.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { countDistinctPowers } from '../Problem029'
2+
3+
/**
4+
* Tests for the countDistinctPowers function.
5+
*/
6+
describe('countDistinctPowers', () => {
7+
it.each([
8+
{ minA: 2, maxA: 5, minB: 2, maxB: 5, expected: 15 },
9+
{ minA: 2, maxA: 100, minB: 2, maxB: 100, expected: 9183 },
10+
{ minA: 2, maxA: 2, minB: 2, maxB: 2, expected: 1 },
11+
{ minA: 3, maxA: 3, minB: 2, maxB: 5, expected: 4 },
12+
{ minA: 10, maxA: 10, minB: 2, maxB: 5, expected: 4 }
13+
])(
14+
'should return $expected for ranges $minA to $maxA and $minB to $maxB',
15+
({ minA, maxA, minB, maxB, expected }) => {
16+
const result = countDistinctPowers(minA, maxA, minB, maxB)
17+
expect(result).toBe(expected)
18+
}
19+
)
20+
})

0 commit comments

Comments
 (0)