Search code examples
javascriptmathintegerlogarithm

Logarithm integers explanation


I'd like if anyone in the community can explain in detail half the answer to a problem that I found in the web. The problem goes as follows

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 .

And this is the answer I found

const consecutiveIntegers = (num) => !Number.isInteger( Math.log2(num));

I never came to what Ln or Le meant before, so after research I understand that if a number can be split into a Log2 (8 = 3 therefore 8 = 2 * 2 * 2) can't be expressed as asked (no consecutive numbers). But I can't see why the "The number is not an integer" (boolean) thing. Shouldn't check the opposite? Number been truly an Integer? Please, do bear in mind that I am trying to understand the answer and no trying to improve it or making it more fancy.
Thanks in advance for your time and patience.
Full credit for the answer to GIDEO.


Solution

  • Such a number is called a polite number. It is known that the impolite numbers are exactly the powers of 2.

    Now, a number n is a power of 2 if and only if log2(n) is an integer, because of log2(2^k) = k.