-
-
Save immaterialh/8519889 to your computer and use it in GitHub Desktop.
making a small prime-number checker
This file contains 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
// tiny prime tester | |
// t(11) -> true | |
// t(12) -> false | |
function t( | |
n,// number to test | |
i // last test devisor | |
){ | |
return | |
!i // if we don't have a devisor ... run with one after some checks pass | |
||n%--i? // or if we don't devide by the next test devisor (ie there is a remainder) (trinary ?: test is really compact) | |
i<2|| // prime if we're down to 1 (|| shortcut smaller than if) | |
t(n,i||n) // prime if next devisor passes (self-calling is shorter than loops) | |
:i<2} // we devide by a number - prime if it's less than 2, non-prime otherwise |
This file contains 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 t(n,i){return!i||n%--i?i<2||t(n,i||n):i<2} |
This file contains 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) 2012 Inkbug | |
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 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": "PrimeTester", | |
"description": "A tiny prime checker.", | |
"keywords": [ | |
"prime", | |
"tester" | |
] | |
} |
This file contains 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>PrimeTester</title> | |
<div>Expected value: <b>2,3,5,7,11,13,17,19</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
// write a small example that shows off the API for your example | |
// and tests it in one fell swoop. | |
function t(n,i){return!i||n%--i?i<2||t(n,i||n):i<2} | |
for(var l=[],n=2;n<20;n++)if(t(n))l.push(n); | |
document.getElementById( "ret" ).innerHTML = l+"" | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment