-5
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ Related \$\endgroup\$ – H.PWiz Sep 12 '17 at 16:14
  • 16
    \$\begingroup\$ This is powers of 2 (credit to El'endia Starman in chat for pointing it out) \$\endgroup\$ – Skidsdev Sep 12 '17 at 16:22
  • \$\begingroup\$ Related, related. \$\endgroup\$ – Leaky Nun Sep 12 '17 at 16:23
  • 4
    \$\begingroup\$ @Xcali that one is closed, and this one doesn't have the restricted-source \$\endgroup\$ – Stephen Sep 13 '17 at 14:04
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Peter Taylor Sep 14 '17 at 15:30

33 Answers 33

6
\$\begingroup\$

05AB1E, 2 bytes

Óg

Try it online!

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

\$\endgroup\$
8
\$\begingroup\$

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.

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

Python 2, 18 bytes

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

Try it online!

-3 thanks to Rod.

\$\endgroup\$
4
\$\begingroup\$

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.
\$\endgroup\$
4
\$\begingroup\$

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!

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

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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!

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

Jelly, 5 bytes

Æf=2Ȧ

Try it online!

Does NOT fail for 1!

\$\endgroup\$
2
\$\begingroup\$

Java (OpenJDK 8), 18 16 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This also works in JS, if you replace the "skinny arrow" with a "fat arrow": tio.run/##y0osSyxOLsosKNHNy09J/… \$\endgroup\$ – Shaggy Sep 12 '17 at 19:21
  • 1
    \$\begingroup\$ Way better than the method based version, i->Integer.bitCount(i+1)==1 by 11 bytes! \$\endgroup\$ – corsiKa Sep 12 '17 at 21:53
  • \$\begingroup\$ @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 \$\endgroup\$ – Roberto Graham Sep 13 '17 at 8:52
2
\$\begingroup\$

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!

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

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.
\$\endgroup\$
  • \$\begingroup\$ @LuisMendo Thanks, thanks a lot! \$\endgroup\$ – Mr. Xcoder Sep 12 '17 at 17:12
  • \$\begingroup\$ @LuisMendo Thanks, addressed. \$\endgroup\$ – Mr. Xcoder Sep 12 '17 at 17:16
2
\$\begingroup\$

Implicit, 7 2 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.

\$\endgroup\$
1
\$\begingroup\$

Neim, 3 bytes

𝐅ᛃ𝐩

Doesn't work on TIO.

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

Jelly, 5 bytes

ÆEL’¬

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Kind of different approach :p \$\endgroup\$ – Erik the Outgolfer Sep 12 '17 at 16:38
1
\$\begingroup\$

Retina, 25 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Actually, 5 bytes

;R♂╙c

Try it online!

Explanation:

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

Haskell, 24 bytes

f n=elem n$map(2^)[1..n]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Actually, 4 bytes

yN2=

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Pyke, 6 bytes

P}2]1q

Try it here!

P      - Prime factors.
 }     - Deduplicate.
  2]1  - Create a one-element list, [2].
     q - Is equal?
\$\endgroup\$
1
\$\begingroup\$

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 [2].
       ~ Output (implicitly).
\$\endgroup\$
  • \$\begingroup\$ !-PQ2 is 5 bytes too, similar idea \$\endgroup\$ – Dave Sep 13 '17 at 12:12
1
\$\begingroup\$

Mathematica, 21 bytes

IntegerQ@Log2@#&&#>1&
\$\endgroup\$
  • \$\begingroup\$ @deleter I think it works for 1 now. \$\endgroup\$ – NoOneIsHere Sep 12 '17 at 20:59
1
\$\begingroup\$

Kotlin, 19 17 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

\$\endgroup\$
1
\$\begingroup\$

Julia 0.6, 10 bytes

!n=n-1$n>n

Try it online!

\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 11 bytes

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

Alternate solution (14 bytes):

max(Ans=2^seq(I²,I,2,Ans
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 37 33 27 18 bytes

Thanks to @MD XF for the idea

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

Try it online!

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

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.

\$\endgroup\$
1
\$\begingroup\$

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).
\$\endgroup\$
  • \$\begingroup\$ As long as an interpreter exists that your 11 byte version works in then it is valid and can be submitted as your solution. \$\endgroup\$ – Shaggy Sep 13 '17 at 14:20
  • \$\begingroup\$ What's wrong with TIO's Cubically? \$\endgroup\$ – MD XF Sep 13 '17 at 15:04
  • \$\begingroup\$ @MDXF According to the specification when the cube is solved (initially) face 8 should be truthy (1), but it is 0. Try it online! \$\endgroup\$ – user202729 Sep 13 '17 at 15:09
  • \$\begingroup\$ 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. \$\endgroup\$ – MD XF Sep 13 '17 at 15:11
  • \$\begingroup\$ The if-else is still bugged, though. (isn't it? I didn't actually check right now) \$\endgroup\$ – user202729 Sep 13 '17 at 15:19
0
\$\begingroup\$

Perl 5, 28 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.