对数整数解释
Logarithm integers explanation
我希望社区中的任何人都可以详细解释我在网络上找到的问题的一半答案。问题如下
Given a N integer, positive , return true if it could be draw as a sum of two or more consecutive integers (10 = 1 + 2 + 3 + 4) , if not, return false .
这是我找到的答案
const consecutiveIntegers = (num) => !Number.isInteger( Math.log2(num));
之前一直没弄明白Ln和Le是什么意思,研究后才明白如果一个数可以拆分
进入 Log2(8 = 3 因此 8 = 2 * 2 * 2)不能按要求表示(没有连续的数字)。但我不明白为什么 "The number is not an integer" (布尔值)。不应该检查对面吗? Number 真的是整数吗?请记住,我正在尝试理解答案,而不是试图改进它或让它更花哨。
预先感谢您的时间和耐心。
完全归功于 GIDEO 的答案。
这样的数称为polite number。众所周知,不礼貌的数字恰好是2的幂。
现在,当且仅当 log2(n)
是整数时,数字 n
是 2 的幂,因为 log2(2^k) = k
.
我希望社区中的任何人都可以详细解释我在网络上找到的问题的一半答案。问题如下
Given a N integer, positive , return true if it could be draw as a sum of two or more consecutive integers (10 = 1 + 2 + 3 + 4) , if not, return false .
这是我找到的答案
const consecutiveIntegers = (num) => !Number.isInteger( Math.log2(num));
之前一直没弄明白Ln和Le是什么意思,研究后才明白如果一个数可以拆分
进入 Log2(8 = 3 因此 8 = 2 * 2 * 2)不能按要求表示(没有连续的数字)。但我不明白为什么 "The number is not an integer" (布尔值)。不应该检查对面吗? Number 真的是整数吗?请记住,我正在尝试理解答案,而不是试图改进它或让它更花哨。
预先感谢您的时间和耐心。
完全归功于 GIDEO 的答案。
这样的数称为polite number。众所周知,不礼貌的数字恰好是2的幂。
现在,当且仅当 log2(n)
是整数时,数字 n
是 2 的幂,因为 log2(2^k) = k
.