17
\$\begingroup\$

Your challenge, should you chose to accept it, is to code-golf a function that returns true or false (or some similar meaningful representation of yes and no) if a number meets the following criteria:

  1. The integer itself is a prime number OR
  2. Either of its neighbor integers are prime

For example:
An input of 7 would return True.
An input of 8 would also return True.
An input of 15 would return False. (Neither 14, 15, or 16 are prime)

The input must be able to return correctly for numbers between 2^0 and 2^20 inclusive, so there's no need to worry about sign issues or integer overflows.

\$\endgroup\$
  • \$\begingroup\$ 32-bit number overflows, not buffer overflows, I guess. \$\endgroup\$ – user unknown Jan 14 '12 at 1:29
  • \$\begingroup\$ Whoops, meant "integer overflow". Brain went on autopilot. \$\endgroup\$ – Mr. Llama Jan 16 '12 at 14:57

31 Answers 31

1
2
0
\$\begingroup\$

APL(NARS) 13 chars, 26 bytes

{∨/0π⍵+¯1..1}

Here {0π⍵} is the function says if its argument it is a prime (return 1[true], else it return 0[false]), that function can have argument both one scalar integer or one list of integers as the above case... test:

  f←{∨/0π⍵+¯1..1}
  f¨1 2 7 8 14 15 16 (2*20)
1 1 1 1 1 0 1 0 
| improve this answer | |
\$\endgroup\$
1
2

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.