-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

0
\$\begingroup\$

JavaScript (Node.js), 12 bytes

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

Port of my Python answer.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 22+1 bytes

<?=($n=$argn)^$n-1>$n;

Run as pipe with -rF. Prints 1 for truthy, empty output for falsy.

hmm ... looks like a port from Dennis´ answer.

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

C, 31 bytes

f(x){return !(x&(x-1))&&!(x-1)}

These are the powers of 2, except 1, and thus binary arithmetics solves the problem.

\$\endgroup\$
  • 1
    \$\begingroup\$ All submissions must be either full programs or functions. In particular, assuming that the input is stored in a certain variable is not allowed. Also, this approach fails when x = 1, which is a power of two but not.completely even. \$\endgroup\$ – Dennis Sep 14 '17 at 0:12
  • \$\begingroup\$ @Dennis 1) Ok, I did, bit now I lost a lot of bytes. 2) The task still only describes the powers of 2, except 1. 3) What it with the languages on which no functions and procedures exist? 4) If you accept functions and programs, why you don't accept expressions? An expression in C is an independently calculatable execution fragment, just as the programs and the functions. | I am so sad, my solution is already a dirty one, it lacks already the katra. \$\endgroup\$ – peterh says reinstate Monica Sep 14 '17 at 17:46

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.