41 bytes version initially golfed from @Georg_Tavonius entry ( http://twitter.com/#!/Georg_Tavonius/status/97239621959815170 )
-
-
Save p01/1115434 to your computer and use it in GitHub Desktop.
isPrimeNumber in 41 bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
n, // the number | |
i // placeholder counter | |
){ | |
for(i=n;n%--i;); // keeps going until the modulo is falsy like n%1 for instance | |
return i<2 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(n,i){for(i=n;n%--i;);return i<2} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Mathieu 'p01' Henri <http://www.p01.org/releases/> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "isPrimeNumber", | |
"description": "Check if a number is prime in 41bytes.", | |
"keywords": [ | |
"isPrimeNumber", | |
"prime", | |
"number" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>isPrimeNumber</title> | |
<div>Expected values: <b>false,true</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var myFunction = function(n,i){for(i=n;n%--i;);return i<2}; | |
document.getElementById( "ret" ).innerHTML = myFunction(140)+','+myFunction(541); | |
</script> |
I mean the theorem cannot tell whether a number is really a prime number or not, 341 is an example(It passes the test even it is not a prime number).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mattneary: I think even JavaScript could handle large numbers, your code won't work.
Because some non-prime numbers (e.g. 341) can pass the test.