Solved

Regex to verify a number is a prime number.

Posted on 2007-12-06
10
878 Views
Last Modified: 2008-02-01
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
Comment
Question by:Onthrax
  • 5
  • 5
10 Comments
 
LVL 8

Expert Comment

by:kebabs
Comment Utility
Can't be done. I can show you a function for it though. What language are you developing in?
0
 
LVL 13

Author Comment

by:Onthrax
Comment Utility
That would be great also.. I would need it for asp.net and javascript. :)
0
 
LVL 8

Expert Comment

by:kebabs
Comment Utility
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
 
LVL 8

Expert Comment

by:kebabs
Comment Utility
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
 
LVL 13

Author Comment

by:Onthrax
Comment Utility
vb, but I can probably convert a C# one, so that would be fine too..
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Expert Comment

by:kebabs
Comment Utility
0
 
LVL 8

Accepted Solution

by:
kebabs earned 500 total points
Comment Utility
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
 
LVL 13

Author Comment

by:Onthrax
Comment Utility
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

by:Onthrax
Comment Utility
0
 
LVL 13

Author Comment

by:Onthrax
Comment Utility
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now