File tree 2 files changed +34
-0
lines changed 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments