Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Regex to verify a number is a prime number.

Posted on 2007-12-06
Medium Priority
887 Views
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! :)
0
Question by:Onthrax
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 5

LVL 8

Expert Comment

ID: 20419005
Can't be done. I can show you a function for it though. What language are you developing in?
0

LVL 13

Author Comment

ID: 20419014
That would be great also.. I would need it for asp.net and javascript. :)
0

LVL 8

Expert Comment

ID: 20419064
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>
``````
0

LVL 8

Expert Comment

ID: 20419073
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>
``````
0

LVL 13

Author Comment

ID: 20419086
vb, but I can probably convert a C# one, so that would be fine too..
0

LVL 8

Expert Comment

ID: 20419100
0

LVL 8

Accepted Solution

kebabs earned 2000 total points
ID: 20419131
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>
``````
0

LVL 13

Author Comment

ID: 20419463
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

LVL 13

Author Comment

ID: 20419468
0

LVL 13

Author Comment

ID: 20419474
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
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,â€¦
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will wâ€¦
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: Tâ€¦
###### Suggested Courses
Course of the Month7 days, 3 hours left to enroll