Skip to content

Instantly share code, notes, and snippets.

@hdragomir
Created July 26, 2010 17:00
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 hdragomir/490838 to your computer and use it in GitHub Desktop.
Save hdragomir/490838 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>prime number checker</title>
<style>
body, input{
font-family: Georgia, serif;
background-color: white;
}
form{
text-align: center;
width: 33%;
margin: 50px auto;
}
output{
display: block;
text-align: center;
}
.false{
color: red;
}
.true{
color: green;
}
input{
line-height: 4em;
height: 4em;
border: 0;
text-align: center;
font-size: 2em;
}
</style>
<script>
var input, output, re = /^1?$|^(11+?)\1+$/;
document.addEventListener('DOMContentLoaded', function(){
input = document.querySelector('input[type=text]');
output = document.querySelector('output');
input.addEventListener('keyup', function(){
input.className = output.className = output.value = isPrime(input.value);
}, false);
input.focus();
document.querySelector('form').addEventListener('submit', function(ev){
ev.preventDefault();
return false;
}, false);
}, false);
function isPrime(number){
return !re.test((function(i){
var pad = '';
while(i--) pad += '1';
return pad;
})(number));
}
</script>
</head>
<body>
<form action="" method="get">
<input type="text" />
<output />
</form>
</body>
<!-- gist by hdragomir (http://hdragomir.com/), regex by avinash (http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/) -->
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment