Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Created September 15, 2016 02:54
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 kidGodzilla/509e807efdedba14a9fd8f5511d03a2a to your computer and use it in GitHub Desktop.
Save kidGodzilla/509e807efdedba14a9fd8f5511d03a2a to your computer and use it in GitHub Desktop.
Simple Primes
// ES5 :)
function isPrime (n) {
for (i = 2; i < n; i++)
if (n % i === 0) return false
return true
}
// ES6 (readable)
isPrime = n => { for (i = 2; i < n; i++) if (n % i === 0) return false; return true }
// ES6 (Codegolfed to 55chars)
isPrime=n=>{for(i=2;i<n;i++)if(n%i==0)return false;return true}
// AssertGolf
assert=(c,t)=>{c?console.log(t):console.warn(t)}
// Tests
assert(isPrime(0)==1, "Zero is prime")
assert(isPrime(2)==1, "Two is prime")
assert(isPrime(3)==1, "Three is prime")
assert(isPrime(4)==0, "Four is not prime")
assert(isPrime(5)==1, "Five is prime")
assert(isPrime(6)==0, "Six is not prime")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment