77
\$\begingroup\$

Task:

Given an integer input, figure out whether or not it is a Cyclops Number.

What is a Cyclops number, you may ask? Well, it's a number whose binary representation only has one 0 in the center!

Test Cases:

Input | Output | Binary  | Explanation
--------------------------------------
0     | truthy | 0       | only one zero at "center"
1     | falsy  | 1       | contains no zeroes
5     | truthy | 101     | only one zero at center
9     | falsy  | 1001    | contains two zeroes (even though both are at the center)
10    | falsy  | 1010    | contains two zeroes
27    | truthy | 11011   | only one zero at center
85    | falsy  | 1010101 | contains three zeroes
101   | falsy  | 1100101 | contains three zeroes
111   | falsy  | 1101111 | only one zero, not at center
119   | truthy | 1110111 | only one zero at center

Input:

  • An integer or equivalent types. (int, long, decimal, etc.)

  • Assume that if evaluating the input results in an integer overflow or other undesirable problems, then that input doesn't have to be evaluated.

Output:

  • Truthy or falsy.

  • Truthy/falsy output must meet the used language's specifications for truthy/falsy. (e.g. C has 0 as false, non-zero as true)

Challenge Rules:

  • Input that is less than 0 is assumed to be falsy and thus does not have to be evaluated.

  • If the length of the binary representation of the number is even, then the number cannot be a Cyclops number.

General Rules:


This is my first Programming Puzzles & Code Golf challenge, so any feedback on how I should improve would be much appreciated!

\$\endgroup\$
6
  • 28
    \$\begingroup\$ Note: This is A129868 \$\endgroup\$
    – tsh
    Commented Mar 8, 2019 at 2:40
  • 40
    \$\begingroup\$ +1 for the 2800 year late pop culture reference in the title \$\endgroup\$
    – Sanchises
    Commented Mar 8, 2019 at 9:21
  • \$\begingroup\$ what is the maximum number that is be tested? \$\endgroup\$
    – Serverfrog
    Commented Mar 8, 2019 at 13:06
  • \$\begingroup\$ @Serverfrog since I did not specify a limit, assume that any positive integer can be tested. \$\endgroup\$ Commented Mar 8, 2019 at 14:09
  • \$\begingroup\$ Is binary input allowed? \$\endgroup\$
    – Qwertiy
    Commented May 25, 2019 at 8:22

62 Answers 62

1 2
3
0
\$\begingroup\$

Uiua, 17 characters (32 bytes UTF-8)

↥↧≍⇌.∶=1/+¬.⋯∶=0.

Explanation of ungolfed version:

↥⊃(↧⊃(≍⇌.)(=1/+¬)⋯)(=0)­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠⁠⁠⁠⁠‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏⁠‎⁡⁠⁢⁢⁢‏⁠‎⁡⁠⁢⁢⁣‏‏​⁡⁠⁡‌⁢​‎⁠‎⁡⁠⁢⁡⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏⁠‏​⁡⁠⁡‌⁤​‎⁠⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌­
↥⊃                   (=0)  # ‎⁡n is zero, or
                  ⋯       # ‎⁢the binary expansion of n
   ↧⊃      (=1/+¬)         # ‎⁣sums to 1 when all its bits are flipped
   ↧⊃(≍⇌.)                # ‎⁤and matches itself reversed
\$\endgroup\$
0
\$\begingroup\$

Japt, 8 bytes

Adding to the collection of 8-byte Japt solutions; can't seem to come up with anything shorter.

¢¶¢Ôp¢èT

Try it (Includes all test cases)

¢¶¢Ôp¢èT     :Implicit input of integer U
¢            :Convert to binary
 ¶           :Check for equality with
  ¢Ô         :  Convert U to binary and reverse
    p        :  Repeated
     ¢è      :    Convert U to binary and count the occurrences of
       T     :      0
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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