Idea from http://montreal.pm.org/tech/neil_kandalgaonkar.shtml. Notice that the number cannot be too big.
-
-
Save tsaniel/1098962 to your computer and use it in GitHub Desktop.
isPrimeNumber (using RegExp)
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( | |
a // the number | |
){ | |
return !/^,?$|^(,,+)\1+$/.test( | |
Array( // repeat the string ',' | |
-~a // coerce to integer | |
) | |
) | |
} |
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(a){return!/^,?$|^(,,+)\1+$/.test(Array(-~a))} |
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) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
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": "isPrimeNumber", | |
"description": "Check if a number is prime with RegExp", | |
"keywords": [ | |
"isPrimeNumber", | |
"RegExp", | |
"Prime" | |
] | |
} |
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>isPrimeNumber</title> | |
<div>Expected value: <b>true</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var myFunction = function(a){return!/^,?$|^(,,+)\1+$/.test(Array(-~a))}; | |
document.getElementById( "ret" ).innerHTML = myFunction(541) | |
</script> |
According to the picture, i think the difference is obvious only if testing big numbers.
Same test with big numbers. Both greedy and ungreedy versions are so slow compared to other solutions, it collapses to an invisible bar in the chart. But you are right, for very large numbers the greedy version is a little bit slower. All solutions (even @p01's) have an exponential complexity of O(d^n) while d is a little bit higher for the greedy regular expression. Update: Mistake in my benchmark. All are almost linear.
Just because these solutions use the brute-force method.
All solutions have an exponential complexity of O(d^n)
I'd like to see a prove for that.
OK, guys. stop wasting your time now and get sane. An End to Negativity!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not statistical significant. In average, it's the same. In my revision of the test it's exactly the same. I'm testing all numbers (in this case up to 100) instead of two selected ones. I think this makes the test more realistic.