Skip to content

Instantly share code, notes, and snippets.

@immaterialh
Forked from Inkbug/LICENSE.txt
Last active January 3, 2016 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save immaterialh/8519889 to your computer and use it in GitHub Desktop.
Save immaterialh/8519889 to your computer and use it in GitHub Desktop.
making a small prime-number checker
// 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
function t(n,i){return!i||n%--i?i<2||t(n,i||n):i<2}
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.
{
"name": "PrimeTester",
"description": "A tiny prime checker.",
"keywords": [
"prime",
"tester"
]
}
<!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