Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit47e3ef4

Browse files
committed
feat: add distinctPowers function with test cases (Problem029)
1 parent1d252d7 commit47e3ef4

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+
exportconstdistinctPowers=(limit=100)=>{
8+
if(limit<2)thrownewError('Power out of scope')
9+
// A Set data structure to keep track of distinct powers
10+
letdistinctPowerSet=newSet()
11+
for(leta=2;a<=limit;a++){
12+
for(letb=2;b<=limit;b++){
13+
distinctPowerSet.add(Math.pow(a,b))
14+
}
15+
}
16+
returndistinctPowerSet.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)

[8]ページ先頭

©2009-2025 Movatter.jp