# 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 Sep 12 '17 at 16:14
• This is powers of 2 (credit to El'endia Starman in chat for pointing it out) Sep 12 '17 at 16:22
• @Xcali that one is closed, and this one doesn't have the restricted-source 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. Sep 14 '17 at 15:30
• Yeah, it really is the powers of 2. So I think it should be a generic "powers of 2" challenge, which we don't yet have. Sep 14 '17 at 15:55

# 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. Sep 12 '17 at 19:15
• Which makes the output empty and thus falsy. Sep 12 '17 at 19:16

# 05AB1E, 2 bytes

Óg


Try it online!

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

# 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.


# 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.

# Python 2, 18 bytes

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


Try it online!

-3 thanks to Rod.

## 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... Sep 13 '17 at 1:17

# 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 Sep 12 '17 at 16:24

# 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 Sep 12 '17 at 16:47
• 36 bytes Sep 12 '17 at 16:48
• You have lambda n but you use variable i. Sep 16 '17 at 4:41
– Rod
Sep 16 '17 at 15:00

# 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.

# 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/… Sep 12 '17 at 19:21
• Way better than the method based version, i->Integer.bitCount(i+1)==1 by 11 bytes! 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 Sep 13 '17 at 8:52

# 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! Sep 12 '17 at 17:12
• @LuisMendo Thanks, addressed. Sep 12 '17 at 17:16

# Neim, 3 bytes

𝐅ᛃ𝐩


Doesn't work on TIO.

• Fails for input 1? 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 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


f n=elem n$map(2^)[1..n]  Try it online! # Actually, 4 bytes yN2=  Try it online! # CJam, 9 bytes {mf2f=:*}  Anonymous block that takes the input number from the stack and replaces it by either 1 or 0. Try it online! ### Explanation { e# Begin block mf e# Array of (repeated) prime factors 2f= e# Compare each element of that array with 2 :* e# Fold product over the array } e# End block  # Pyke, 6 bytes P}2]1q  Try it here! P - Prime factors. } - Deduplicate. 2]1 - Create a one-element list, . q - Is equal?  # Pyth, 5 bytes q]2{P  Verify all the test cases. Alternative: q2s{P  # Explanation q]2{P ~ Full program with implicit input (Q) at the end. P ~ Prime factors. { ~ Deduplicated. q ~ Equals? ]2 ~ The literal . ~ Output (implicitly).  • !-PQ2 is 5 bytes too, similar idea – Dave Sep 13 '17 at 12:12 • q2eP for 4 bytes. P gives an ordered list, so eP is the largest prime factor. Mar 28 at 16:14 # Mathematica, 21 bytes IntegerQ@Log2@#&&#>1&  • @deleter I think it works for 1 now. Sep 12 '17 at 20:59 # Kotlin, 1917 16 bytes {it-1 xor it>it}  ## Test private val t: (Int) -> Boolean = {it-1 xor it>it} fun main(args: Array<String>) { println(t(0)) println(t(1)) println(t(999)) println(t(1024)) println(t(2)) }  TryItOnline # Julia 0.6, 10 bytes !n=n-1$n>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 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. 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.
%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. Sep 13 '17 at 14:20
• What's wrong with TIO's Cubically? 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! 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. Sep 13 '17 at 15:11
• The if-else is still bugged, though. (isn't it? I didn't actually check right now) Sep 13 '17 at 15:19

# JavaScript (Node.js), 12 bytes

n=>(n^n-1)>n
`