Regex to verify a number is a prime number.

I have been searching through google to find a regex that allows me to only allow prime numbers but can't seem to find it.

Could someone provide me the regex I need to verify if a number is a prime number? thank you! :)
LVL 13
OnthraxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kebabsConnect With a Mentor Commented:
Actually, that JS can be simplified to this (derived from .NET examples in above URL):
<script type="text/javascript">
function CheckIfPrime(n) {
  result = true;
  for (i = 2; i <= Math.sqrt(n); i++)
    if (!(result = n % i != 0)) break;
  return result;
}
</script>

Open in new window

0
 
kebabsCommented:
Can't be done. I can show you a function for it though. What language are you developing in?
0
 
OnthraxAuthor Commented:
That would be great also.. I would need it for asp.net and javascript. :)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
kebabsCommented:
C# ?

For now, here is the JavaScript version:
<script type="text/javascript">
function IsPrime(n) {
  no = 0;
  for(i = 2; i <= n; i++) {
    for(d = 2; d < i; d++) {
      v = i / d;
      if(v != 1 && Math.floor(v)==v) { no = 1; d = i; }
    }
    if(no != 1) result = i;
    no = 0;
  }
  return (result == n)
}
</script>

Open in new window

0
 
kebabsCommented:
Hmm... that one doesn't allow IsPrime(1)...

This one fixes that:
<script type="text/javascript">
function IsPrime(n) {
  no = 0;
  if (i < 2) return true;
  for(i = 2; i <= n; i++) {
    for(d = 2; d < i; d++) {
      v = i / d;
      if(v != 1 && Math.floor(v)==v) { no = 1; d = i; }
    }
    if(no != 1) result = i;
    no = 0;
  }
  return (result == n)
}
</script>

Open in new window

0
 
OnthraxAuthor Commented:
vb, but I can probably convert a C# one, so that would be fine too..
0
 
kebabsCommented:
0
 
OnthraxAuthor Commented:
Thanks kebabs. Your javascript function worked like a charm. However the vb.net example from the URL you provided is flaud. It confirms numbers are prime which are not. For instance 99 is prime when using the function, which is obviously not since it can be divided by 9.

For future reference to this thread. I converted the C# function to VB and works fine now.

Thnx for the help m8!
0
 
OnthraxAuthor Commented:
0
 
OnthraxAuthor Commented:
Hmm for some reason it has't attached the code snippet twice.. third attempt..


Dim isPrime As Boolean = True
Dim i As Integer = 2
 
While i <= Math.Sqrt(Currentvalue) AndAlso isPrime
    isPrime = Currentvalue Mod i <> 0
    i += 1
End While

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.