30
6
Your goal is to determine whether a given number n
is prime in the fewest bytes. But, your code must be a single Python 2 expression on numbers consisting of only
- operators
- the input variable
n
- integer constants
- parentheses
No loops, no assignments, no built-in functions, only what's listed above. Yes, it's possible.
Operators
Here's a list of all operators in Python 2, which include arithmetic, bitwise, and logical operators:
+ adddition
- minus or unary negation
* multiplication
** exponentiation, only with non-negative exponent
/ floor division
% modulo
<< bit shift left
>> bit shift right
& bitwise and
| bitwise or
^ bitwise xor
~ bitwise not
< less than
> greater than
<= less than or equals
>= greater than or equals
== equals
!= does not equal
All intermediate values are integers (or False/True, which implicitly equals 0 and 1). Exponentiation may not be used with negative exponents, as this may produce floats. Note that /
does floor-division, unlike Python 3, so //
is not needed.
Even if you're not familiar with Python, the operators should be pretty intuitive. See this table for operator precedence and this section and below for a detailed specification of the grammar. You can run Python 2 on TIO.
I/O
Input: A positive integer n
that's at least 2.
Output: 1 if n
is prime, and 0 otherwise. True
and False
may also be used. Fewest bytes wins.
Since your code is an expression, it will be a snippet, expecting the input value stored as n
, and evaluating to the output desired.
Your code must work for n
arbitrarily large, system limits aside. Since Python's whole-number type is unbounded, there are no limits on the operators. Your code may take however long to run.
Maybe this should have the python tag? – fəˈnɛtɪk – 2018-08-24T19:41:27.610