Skip to content

Commit 47e3ef4

Browse files
committed
feat: add distinctPowers function with test cases (Problem029)
1 parent 1d252d7 commit 47e3ef4

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Project-Euler/Problem029.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://projecteuler.net/problem=29
2+
3+
/*
4+
How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
5+
*/
6+
7+
export const distinctPowers = (limit = 100) => {
8+
if (limit < 2) throw new Error('Power out of scope')
9+
// A Set data structure to keep track of distinct powers
10+
let distinctPowerSet = new Set()
11+
for (let a = 2; a <= limit; a++) {
12+
for (let b = 2; b <= limit; b++) {
13+
distinctPowerSet.add(Math.pow(a, b))
14+
}
15+
}
16+
return distinctPowerSet.size
17+
}

Project-Euler/test/Problem029.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { distinctPowers } from '../Problem029'
2+
3+
describe('Distinct numbers of a ^ b where a and b in range [2,100]', () => {
4+
it('should throw error when number is less than 2', () => {
5+
expect(() => distinctPowers(0)).toThrowError('Power out of scope')
6+
})
7+
it('should throw error when number is negative', () => {
8+
expect(() => distinctPowers(-3)).toThrowError('Power out of scope')
9+
})
10+
test('if the number is greater than or equal to 2', () => {
11+
expect(distinctPowers(5)).toBe(15)
12+
})
13+
// Project Euler Condition Check
14+
test('if the number is greater than or equal to 2', () => {
15+
expect(distinctPowers(100)).toBe(9183)
16+
})
17+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy