This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Number | Nearest Lower Square | |
|---|---|---|
| 1 | 1 | |
| 2 | 2 | |
| 3 | 2 | |
| 4 | 4 | |
| 5 | 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Number | Nearest Lower Square | |
|---|---|---|
| 1 | 1 | |
| 2 | 2 | |
| 3 | 2 | |
| 4 | 4 | |
| 5 | 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let nearestLowerPow2 = function (x) { | |
| if (x == 0) return 0; | |
| const arr = [1, 2, 4, 8, 16]; | |
| arr.forEach(function(n) { | |
| x |= (x >> n); | |
| }); | |
| return x - (x >> 1); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Third solution binary based | |
| const solution3 = (n) => { | |
| // Convert to BIN | |
| const binaryStr = Number(n).toString(2); | |
| // Swapping digits | |
| const newBinaryStr = (binaryStr + binaryStr[0]).replace(/./, ''); | |
| // Convert back to DEC | |
| return parseInt(newBinaryStr, 2); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // My ugly first solution draft | |
| const solution = (n) => { | |
| // Ensure n is number | |
| if (typeof n !== 'number') return 0; | |
| // Ensure n is > 0 | |
| if (n < 1) return 0; | |
| // This array represents the circle | |
| const arr = []; | |
| // Fill the array with 1 ones to begin |
OlderNewer