Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sexy Prime Generator
// A short prime tester:
// t(11) -> true
// t(12) -> false
function t(
n, //Input
i //Placeholder
){
for(i=2;i<n;i++)
if(!(n%i))
return!1;
return!0
}
// Sexy prime generator
// https://en.wikipedia.org/wiki/Sexy_prime
function s(
n, //Input
p //Placeholder
){
// Loop through all numbers from 2 till n-6, checking primacy of i and i+6
for(
p=[], //Initialize array
i=2;
i<n-6;
i++
)
// If both i and i+6 are prime, add to p
if(t(i)&&t(i+6))
p.push([i,i+6]);
return p
}
function t(n,i){for(i=2;i<n;i++)if(!(n%i))return!1;return!0}function s(n,p){for(p=[],i=2;i<n-6;i++)if(t(i)&&t(i+6))p.push([i,i+6]);return p}
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": "SexyPrimes",
"description": "A Sexy Prime generator, plus a prime checker.",
"keywords": [
"prime",
"generator"
]
}
<!DOCTYPE html>
<title>Sexy Primes</title>
<div>Expected value: <b>[[5,11],[7,13]]</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){for(i=2;i<n;i++)if(!(n%i))return!1;return!0}function s(n,p){for(p=[],i=2;i<n-6;i++)if(t(i)&&t(i+6))p.push([i,i+6]);return p}
document.getElementById( "ret" ).innerHTML = s(15)
</script>
@tsaniel

This comment has been minimized.

Copy link

commented Aug 3, 2012

Just simply combine two functions:
function f(a,b,c,d,e){for(d=c=[];b?b%--a:d++<a-6;)b||f(d,d)|f(e=d+6,e)||c.push([d,e]);return b?a^1:c}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.