File tree Expand file tree Collapse file tree 2 files changed +18
-18
lines changed
Expand file tree Collapse file tree 2 files changed +18
-18
lines changed Original file line number Diff line number Diff line change 1212// = exponent(x*x, floor(n/2)) ; if n is odd
1313// = x*exponent(x*x, floor(n/2)) ; if n is even
1414const exponent = ( x , n ) => {
15- let ans = 1
16- while ( n > 0 ) {
17- if ( n % 2 != 0 ) ans *= x
18- n = Math . floor ( n / 2 )
19- if ( n > 0 ) x *= x
20- }
21- return ans
15+ let ans = 1
16+ while ( n > 0 ) {
17+ if ( n % 2 != = 0 ) ans *= x
18+ n = Math . floor ( n / 2 )
19+ if ( n > 0 ) x *= x
20+ }
21+ return ans
2222}
2323
24- export { exponent }
24+ export { exponent }
Original file line number Diff line number Diff line change 11import { exponent } from '../BinaryExponentiationIterative'
22
33describe ( 'exponent' , ( ) => {
4- it ( 'should return 1 when power is 0' , ( ) => {
5- expect ( exponent ( 5 , 0 ) ) . toBe ( 1 )
6- } )
4+ it ( 'should return 1 when power is 0' , ( ) => {
5+ expect ( exponent ( 5 , 0 ) ) . toBe ( 1 )
6+ } )
77
8- it ( 'should return 0 when base is 0' , ( ) => {
9- expect ( exponent ( 0 , 7 ) ) . toBe ( 0 )
10- } )
8+ it ( 'should return 0 when base is 0' , ( ) => {
9+ expect ( exponent ( 0 , 7 ) ) . toBe ( 0 )
10+ } )
1111
12- it ( 'should return the value of a base raised to a power' , ( ) => {
13- expect ( exponent ( 3 , 5 ) ) . toBe ( 243 )
14- } )
15- } )
12+ it ( 'should return the value of a base raised to a power' , ( ) => {
13+ expect ( exponent ( 3 , 5 ) ) . toBe ( 243 )
14+ } )
15+ } )
You can’t perform that action at this time.
0 commit comments