-4
\$\begingroup\$

Your challenge is to write a Python program to print all the primes (separated by whitespace) less than a given integer N with an asterisk (*) next to each twin prime in ONE statement. A twin prime is a prime number that is either two more or two less than another prime.

All output should be to stdout and input should be read from stdin. (Hardcoding outputs is allowed, but it is not likely to result in very short code.)

Shortest code wins!

For the less restricted version of the challenge for all languages, click here.

Input

  • N is always one of 50, 100, 1000, 1000000, or 10000000

Output

  • Possible output for input 12 could be
    2 3* 5* 7* 11*
    (However, the input will never be 12, as specified above. Your program need not work for this input.)

Restrictions

  • No new lines or semicolons
  • Do not use eval or exec
\$\endgroup\$
11
  • \$\begingroup\$ "one statement" - does that allow something like for i in []: print(i) or not? It's technically a compound statement, which contains another one, and it doesn't have any newlines or semicolons... \$\endgroup\$
    – pxeger
    Commented Feb 28, 2021 at 16:32
  • \$\begingroup\$ @pxeger Yes, that is allowed. \$\endgroup\$
    – user101295
    Commented Feb 28, 2021 at 16:35
  • 1
    \$\begingroup\$ Requiring input to be taken from stdin and written to stdout generally isn't recommended here because it makes the challenge less about solving the problem and more about boring I/O plumbing/implementation. I'd recommend using the standard I/O rules \$\endgroup\$
    – pxeger
    Commented Feb 28, 2021 at 16:42
  • 1
    \$\begingroup\$ @Wasif I've created a less restricted version of the challenge here. \$\endgroup\$
    – user101295
    Commented Feb 28, 2021 at 18:31
  • 1
    \$\begingroup\$ I've voted to close this as a duplicate of the less restricted version, as the [restricted-source] part of the challenge does not significantly change the task, so the challenges are, in my view, essentially identical \$\endgroup\$ Commented Feb 28, 2021 at 18:32

1 Answer 1

5
\$\begingroup\$

Python 3.8 (pre-release), 119 bytes

print(*[str(i)+'*'*(p(i-2)|p(i+2))for i in range(2,int(input()))if(p:=lambda x:all(x%i for i in range(2,x))*(x>1))(i)])

Reads from stdin.

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Updated to do that, but in general on this site a function is a valid form of IO. \$\endgroup\$
    – rak1507
    Commented Feb 28, 2021 at 16:43

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