Skip to main content
extra 5 bytes to make it work as a program
Source Link
mkst
  • 5.3k
  • 13
  • 19

K (oK), 1414 19 bytes

Solution:

2=+/d=_d:x%!x:. 0:` 

Try it online!Try it online!

Example:

root@c957fa0dccbd:/ok# echo 1 | node repl.js examples/prime.k 0 root@c957fa0dccbd:/ok# echo 2 | node repl.js examples/prime.k 1 root@c957fa0dccbd:/ok# echo 5 | node repl.js examples/prime.k 1 root@c957fa0dccbd:/ok# echo 97 | node repl.js examples/prime.k 1 

Explanation:

Calculate number of factors for input, if equal to 2, then it's prime.

2=+/d=_d:x%!x:. 0:` / the solution 0:` / read from stdin . / value (ie convert "123" > 123) x:  / store input as x !  / range 0..x x%  / x divided by ... d:  / store as d _  / floor d=  / d equal to ... +/  / sum 2=  / 2 equals? 

K (oK), 14 bytes

Solution:

2=+/d=_d:x%!x: 

Try it online!

Explanation:

Calculate number of factors for input, if equal to 2, then it's prime.

2=+/d=_d:x%!x: / the solution x: / store input as x ! / range 0..x x% / x divided by ... d: / store as d _ / floor d= / d equal to ... +/ / sum 2= / 2 equals? 

K (oK), 14 19 bytes

Solution:

2=+/d=_d:x%!x:. 0:` 

Try it online!

Example:

root@c957fa0dccbd:/ok# echo 1 | node repl.js examples/prime.k 0 root@c957fa0dccbd:/ok# echo 2 | node repl.js examples/prime.k 1 root@c957fa0dccbd:/ok# echo 5 | node repl.js examples/prime.k 1 root@c957fa0dccbd:/ok# echo 97 | node repl.js examples/prime.k 1 

Explanation:

Calculate number of factors for input, if equal to 2, then it's prime.

2=+/d=_d:x%!x:. 0:` / the solution 0:` / read from stdin . / value (ie convert "123" > 123) x:  / store input as x !  / range 0..x x%  / x divided by ... d:  / store as d _  / floor d=  / d equal to ... +/  / sum 2=  / 2 equals? 
Source Link
mkst
  • 5.3k
  • 13
  • 19

K (oK), 14 bytes

Solution:

2=+/d=_d:x%!x: 

Try it online!

Explanation:

Calculate number of factors for input, if equal to 2, then it's prime.

2=+/d=_d:x%!x: / the solution x: / store input as x ! / range 0..x x% / x divided by ... d: / store as d _ / floor d= / d equal to ... +/ / sum 2= / 2 equals?