Is it a completely even number?

A completely even number is a positive even integer whose divisors (not including 1) are all even.

Some completely even numbers are 2 (divisors: 2, 1), 4 (divisors: 4, 2, 1), 16 (divisors: 16, 8, 4, 2, 1), and 128 (divisors: 128, 64, 32, 16, 8, 4, 2, 1).

Some non-completely even numbers are 10 (divisors: 10, 5, 2, 1), 12 (divisors: 12, 6, 4, 3, 2, 1), 14 (divisors: 14, 7, 2, 1), and 18 (divisors: 18, 9, 6, 3, 2, 1). 1 is not completely even, and odd numbers are obviously not completely even. 0 is not completely even but you do not need to handle this.

Your challenge is to take a positive integer as input and output a truthy value if it is completely even and a falsy value if it is not. These outputs do not need to be consistent.

Input may be taken via any allowed method, and output may be given via any allowed method.

The shortest code in bytes wins!

• Related – H.PWiz Sep 12 '17 at 16:14
• This is powers of 2 (credit to El'endia Starman in chat for pointing it out) – Skidsdev Sep 12 '17 at 16:22
• – Leaky Nun Sep 12 '17 at 16:23
• @Xcali that one is closed, and this one doesn't have the restricted-source – Stephen Sep 13 '17 at 14:04
• @Dennis, the question contradicts itself. By the definition given in the first paragraph, 1 is completely even, because its divisors except 1 form an empty set and so are trivially all even. – Peter Taylor Sep 14 '17 at 15:30

05AB1E, 2 bytes

Óg

Try it online!

In 05AB1E only 1 is truthy. Input-1-and-input-0-verified.

MATL, 2 bytes

qB

Try it online!

How it works

This takes advantage of MATL's convenient interpretation of truthy and falsy. q decrements the input and B gets the binary representation of the result. This yields a non-empty array of 1's (truthy) for even powers of two, an array that is either empty of contains a 0 (falsy) otherwise.

• Throws an error for 0. – Shaggy Sep 12 '17 at 19:15
• Which makes the output empty and thus falsy. – Dennis Sep 12 '17 at 19:16

Python 2, 18 bytes

lambda x:~-x&x<1<x

Try it online!

-3 thanks to Rod.

Jelly, 3 bytes

^’>

Try it online!

How it works

^’>  Main link. Argument: n

’   Decrement; yield n-1.
^    Compute the bitwise XOR of n and n-1.
This will conserve the highest set bit of n only if n is a power of two.
If n is even, n-1 will be positive and the result will be different from n.
>  Test if the result is larger than n.

JavaScript (ES6), 14 bytes

f=
n=>n>1>(n&n-1)
<input type=number min=0 oninput=o.textContent=f(this.value)><pre id=o>

Python doesn't have the monopoly on chained comparisons!

• This might be the first time I've ever seen JS's comparison chaining come in handy in a function this short... – ETHproductions Sep 13 '17 at 1:17

Python 3, 16 bytes

lambda n:n^n-1>n

Returns True or False.

Try it online!

How it works

• If n = 0, then n ⊕ (n - 1) = 0 ⊕ -1 = -1 < 0 = n, so the function returns False.

• If n = 1, then n ⊕ (n - 1) = 1 ⊕ 0 = 1 = n, so the function returns False.

• If 2k < n < 2k+1, then 2k ≤ n - 1 < 2k+1, so n and n - 1 have the 2k bit in common,
n ⊕ (n - 1) < 2k < n, and the function returns False.

• Finally, if n = 2k with k > 0, then n = 2k and n - 1 = 2k - 1 have no bits in common, so
n ⊕ (n - 1) = n + (n - 1) > n + 0 = n and the function returns True.

Japt, 6 bytes

õ!² øU

Test it

Explanation

Generate an array of integers (õ) from 1 to input U. Raise 2 to the power of each (). Check if the array includes (ø) U.

Python 2, 33 bytes

lambda n:bin(n).count('1')==1-n%2

Try it online!

Recusive approach, 38 36 bytes

-2 bytes thanks to Leaky Nun

f=lambda n:n>1if n<3else f(~n%2*n/2)

Try it online!

• 37 bytes – Leaky Nun Sep 12 '17 at 16:47
• 36 bytes – Leaky Nun Sep 12 '17 at 16:48
• You have lambda n but you use variable i. – NoOneIsHere Sep 16 '17 at 4:41
• @NoOneIsHere my bad, fixed – Rod Sep 16 '17 at 15:00

Jelly, 5 bytes

Æf=2Ȧ

Try it online!

Does NOT fail for 1!

Java (OpenJDK 8), 18 16 bytes

i->i>1&(i&~-i)<1

Try it online!

• This also works in JS, if you replace the "skinny arrow" with a "fat arrow": tio.run/##y0osSyxOLsosKNHNy09J/… – Shaggy Sep 12 '17 at 19:21
• Way better than the method based version, i->Integer.bitCount(i+1)==1 by 11 bytes! – corsiKa Sep 12 '17 at 21:53
• @corsiKa The most I could golf the method based one down to was 22 bytes: i->i>1&i.bitCount(i)<2. You can call a class' static methods using an instance – Roberto Graham Sep 13 '17 at 8:52

Husk, 3 bytes

Returns log₂(x) if True 0 otherwise

£İ2

Explanation

£       Is it an element of the increasing sequence
İ2     powers of two (starting at 2)

Try it online!

• Are all positive integers truthy in Husk? – Okx Sep 12 '17 at 16:24
• Yes, and negative integers too – H.PWiz Sep 12 '17 at 16:24

MATL, 4 bytes

Saved a byte thanks to Luis Mendo!

Yf2=

Try it here.

Returns 1 (or an array of several 1s) for truthy and 0 or (the empty string) for falsy.

How?

Yf2=   % Full program.

Yf     % Prime factors.
2=   % All equal 2?
% Output implicitly.
• @LuisMendo Thanks, thanks a lot! – Mr. Xcoder Sep 12 '17 at 17:12
• @LuisMendo Thanks, addressed. – Mr. Xcoder Sep 12 '17 at 17:16

Implicit, 72 3 bytes

½?ö

Try it online! Explanation:

implicit float input
½    calculate log2(input)
?   if truthy
ö   push 1 if top-of-stack is whole, 0 if non-whole
implicit int output

½ pushes log2(input). If the input is 0 or 1 it will push 0.000000. 0 is a whole number so performing ö on 0 will yield 1, giving the incorrect result for the input 1. ? only performs the next command if the top of stack is truthy. So if the input was 1 or 0, it will skip the ö and print 0 as it's supposed to. Otherwise it will push 1 if log2(input) is whole and 0 if it's not.

Neim, 3 bytes

𝐅ᛃ𝐩

Doesn't work on TIO.

• Fails for input 1? – Leaky Nun Sep 12 '17 at 16:31
• @LeakyNun Trying the program that produces the inverse output, 𝐅ᛄ𝐩 (which works on TIO), succeeds for input 1. – Okx Sep 12 '17 at 16:31

Jelly, 5 bytes

ÆEL’¬

Try it online!

• Kind of different approach :p – Erik the Outgolfer Sep 12 '17 at 16:38

Retina, 25 bytes

.+
$* +`^(11+)\1$
$1 ^11$

Try it online!

Actually, 5 bytes

;R♂╙c

Try it online!

Explanation:

;R♂╙c
;      duplicate n
R     range(1, n+1)
♂╙   powers of 2
c  contains n

Try it online!

TI-Basic, 11 bytes

logBASE(Ans,2
Ans and not(fPart(Ans

Alternate solution (14 bytes):

max(Ans=2^seq(I²,I,2,Ans

C (gcc), 373327 18 bytes

Thanks to @MD XF for the idea

f(n){n=(n^n-1)>n;}

Try it online!

• 20 bytes – MD XF Sep 12 '17 at 21:47
• @MDXF Nice! I knew there was a way to determine with simple arithmetic just couldn't figure out how. – cleblanc Sep 13 '17 at 12:24

QBIC, 16 bytes

[:|~2^a=b\_X1}?0

This checks all powers of 2 from 1 to the input, to see if it matches the input. Prints 1 when it does, 0 when it doesn't.

Cubically, 12 bytes

$R:7-8⊕7>7%6 Try it online! Cubically's code is quite short and easily writable, if the solution does not involves too much (more than 1) temporary variables. Explanation: I used other people's solutions, calculate (n^(n-1)) > n.$               Input number n, store to memory index 7.
R              Make the cube unsolved by perform a move "R" on the cube, thus the value of face 8 will be truthy (1).
:7            Set notepad value to value of memory index 7 (number n)
-8          Subtract by value of memory index 8, which is 1 because cube is unsolved.
⊕7       Calculate bit-xor of current notepad value (n-1) with value of memory index 7 (n), store to notepad.
>7      Calculate (notepad > n), store to notepad.
%6    Print value of notepad as number (0 for falsy, 1 for truthy).
• As long as an interpreter exists that your 11 byte version works in then it is valid and can be submitted as your solution. – Shaggy Sep 13 '17 at 14:20
• What's wrong with TIO's Cubically? – MD XF Sep 13 '17 at 15:04
• @MDXF According to the specification when the cube is solved (initially) face 8 should be truthy (1), but it is 0. Try it online! – user202729 Sep 13 '17 at 15:09
• Oh, that's a bug in the spec, not the language. 8 is used for looping until the cube is solved, e.g. (R2)8 will perform R2 while the cube is not solved. So 8 contains 0 when the cube is solved and 1 when it's not. Just fixed that in the spec. – MD XF Sep 13 '17 at 15:11
• The if-else is still bugged, though. (isn't it? I didn't actually check right now) – user202729 Sep 13 '17 at 15:19

Perl 5, 28 bytes

$_=sprintf'%b',<>;say/^10+$/

Try it online!