mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Merge 9af617a1b8
into 2c67b48c21
This commit is contained in:
commit
f3d774dfe3
19
src/algorithms/math/is-power-of-two/different_power_of2.js
Normal file
19
src/algorithms/math/is-power-of-two/different_power_of2.js
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
// different power of 2 requiered so that the sum of different power of 2 equals to no..
|
||||
export default function diffPowerOfTwo(number) {
|
||||
if (number < 1) {
|
||||
return 0;
|
||||
}
|
||||
var a=0;
|
||||
let dividedNumber = number;
|
||||
while (dividedNumber !== 1) {
|
||||
if (dividedNumber % 2 !== 0) {
|
||||
a+=1;
|
||||
}
|
||||
a+=1;
|
||||
|
||||
dividedNumber /= 2;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
Loading…
Reference in New Issue
Block a user