473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Need a way to determine Prime Numbers

I am looking for some help on a program that when you write a number it will determine it if is prime or not. Any help is appreciated.
Oct 15 '07 #1
15 8387
Killer42
8,435 Expert 8TB
I am looking for some help on a program that when you write a number it will determine it if is prime or not. Any help is appreciated.
I'd recommend you start by looking up primes on Wikipedia. I haven't checked, but it will probably provide info on how they are determined.

Also, if you want a simple brute-force approach, for number n just test every number from 2 to n-1 to see whether it divides evenly into n. If so, n isn't prime.
Oct 15 '07 #2
kadghar
1,295 Expert 1GB
I'd recommend you start by looking up primes on Wikipedia. I haven't checked, but it will probably provide info on how they are determined.

Also, if you want a simple brute-force approach, for number n just test every number from 2 to n-1 to see whether it divides evenly into n. If so, n isn't prime.
brute-force works here testing every number from 2 to the square root of n.
even then, it's brute force.
Oct 15 '07 #3
Firecore
114 100+
brute-force works here testing every number from 2 to the square root of n.
even then, it's brute force.
Everyone can use a little bit of brute force ;)
Oct 15 '07 #4
Killer42
8,435 Expert 8TB
brute-force works here testing every number from 2 to the square root of n.
Good point! It'd just be redundant going through the rest of them, huh.
Oct 16 '07 #5
jamesd0142
469 256MB
Expand|Select|Wrap|Line Numbers
  1.     '--------------
  2.     Public Function IsPrime(ByVal TestPrime As Long) As Boolean
  3.         Dim TestNum As Long
  4.         Dim TestLimit As Long
  5.  
  6.         '   Eliminate even numbers
  7.         If TestPrime Mod 2 = 0 Then Exit Function
  8.  
  9.         '   Loop through ODD numbers starting with 3
  10.         TestNum = 3
  11.         TestLimit = TestPrime
  12.         Do While TestLimit > TestNum
  13.  
  14.             If TestPrime Mod TestNum = 0 Then
  15.                 '   Uncomment this if you really want to know
  16.                 'MsgBox("Divisible by " & TestNum)
  17.                 Exit Function
  18.             End If
  19.  
  20.             '   There's logic to this.  Think about it.
  21.             TestLimit = TestPrime \ TestNum
  22.  
  23.             '   Remember, we only bother to check odd numbers
  24.             TestNum = TestNum + 2
  25.         Loop
  26.  
  27.         '   If we made it through the loop, the number is a prime.
  28.         IsPrime = True
  29.     End Function
  30.  
and usage:

Expand|Select|Wrap|Line Numbers
  1.  If IsPrime(<<<YOUR-VALUE>>>) = "True" Then
  2.             MsgBox("PRIME")
  3.         Else
  4.             MsgBox("NOT PRIME")
  5.         End If
  6.  
Oct 16 '07 #6
Killer42
8,435 Expert 8TB
As kadghar pointed out, TestLimit probably should be set to Sqr(TestPrime), to reduce or eliminate wasted effort.
Oct 16 '07 #7
Private Sub Command1_Click()
Dim x, i, pr As Integer
pr = 1
x = Text1.Text
If x >= 2 Then
For i = 2 To x - 1
If x Mod i = 0 Then
pr = 0
End If
Next
If pr = 0 Then
Label1.Caption = x & " is a Composite number."
Else
Label1.Caption = x & " is a Prime number."
End If
End If
End Sub
Did it an easy way, ty but it works :)
Oct 17 '07 #8
gpl
152 100+
I dont know how fast your processor is, but entering a number then testing for all the divisions will take a while.

Why not store a list of all the primes (up to the max number you can store in a variable) in an array or collection of some description.

Then when a number is entered, just check against the list of primes; quick and easy!

Graham
Oct 17 '07 #9
JamieHowarth0
533 Expert 512MB
I dont know how fast your processor is, but entering a number then testing for all the divisions will take a while.

Why not store a list of all the primes (up to the max number you can store in a variable) in an array or collection of some description.

Then when a number is entered, just check against the list of primes; quick and easy!

Graham
Nice idea except that there are an infinte number of prime numbers (depending on how reasonable you want to be) - checking a single value against every prime number in existence? Talk about processor strain!
I like the brute-force approach - loop up to Sqr(n) to determine whether any number divides evenly into n. If not, finish the loop, it's a prime number. If not, break the loop, it's not prime. Simple.

medicineworker
Oct 17 '07 #10
gpl
152 100+
Nice idea except that there are an infinte number of prime numbers (depending on how reasonable you want to be) - checking a single value against every prime number in existence? Talk about processor strain!
I like the brute-force approach - loop up to Sqr(n) to determine whether any number divides evenly into n. If not, finish the loop, it's a prime number. If not, break the loop, it's not prime. Simple.

medicineworker
True, there are an infinite number ... but I was assuming that the test would be against a range limited by that which a computer could hold (bigint - 2 billion or is it 4 billion unsigned ?)

I dont know how many primaries there are in that range, but a simple binary search would make the number of comparisons a max of 32 (or is it 33).

Using larger types (floats) would encompass a much larger range, but a search would still be n (or n+1) comparisons where the range is 0 - 2^n

Of course, the figures above assumed searching for each number, the search for primaries would be far fewer


The brute-force approach of course would not need to divide by each number, but by each prime found so far, up to sqr(n) as any non-prime divisor would already have been tested by its factors
Graham
Oct 17 '07 #11
I know that evry prime number is of the form:

6r - 1

OR

6r + 1

Except the prime numbers 3 and 5

Try it!!!!
Oct 17 '07 #12
Killer42
8,435 Expert 8TB
6r - 1
OR
6r + 1
What does r represent?
Oct 17 '07 #13
kadghar
1,295 Expert 1GB
I know that evry prime number is of the form:

6r - 1

OR

6r + 1

Except the prime numbers 3 and 5

Try it!!!!
Well yes, with that you're saying that they're always near a number that is divisible by 2 and 3, which makes them not divisible by any of both.

5 is included, is the case 6*1 -1

This could help to make it faster, since you don't have to check every number from 2 to sqrt (n) ... say n is the number you want to check.

This way you'll only have to check 2, 3 and the ones with that rule.

That'll make you check the prime numbers and.. some others, but still faster.

Expand|Select|Wrap|Line Numbers
  1. If n / 2 = Int(n / 2) Or n / 3 = Int(n / 3) Then
  2.     MsgBox ("This ain't a prime number buddy")
  3. End If
  4. For r = 1 To Int((n ^ 0.5) / 6) + 1
  5.     If n / (6 * r + 1) = Int(n / (6 * r + 1)) Or n / (6 * r - 1) = Int(n / (6 * r - 1)) Then
  6.         MsgBox ("This ain't a prime number buddy")
  7.     End If
  8. Next
  9.  
Note I put a (+ 1) in the upper limit of the FOR, since other way when the sqrt of n is 6*r-1 you wouldn't consider it. And we don't want that to happen. Even then, most of the cases it will be only redundant.
:)
Oct 17 '07 #14
A little clarification on what r represents?
Oct 18 '07 #15
If you are using QB, you can build a quick and dirty hard divide routine.
let p=a prime candidate (odd number of course, but there are other limits you can impose if you know how prime numbers behave.)

in a loop
if p mod p' where prime' is a previously known prime number
equals 0 then p is not prime
while p' <= sqrt(p)


But this requires a lot of casheing of the known prime numbers.


I just finished developing a routine that locates prime numbers using nothing but addition - no wasted calculations. I developed a self-expanding sieve of Eratosthenes with no upper bound limit. It is not fast but is solid and steady and does not lose runtime efficiency or accuracy with higher order numbers. No repetitive division, no square checking, just shooting fish in a barrel
Nov 27 '18 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
4
by: Caffiend | last post by:
Thanks to everyone who helped me out with my early version of the prime number calculator... Here is the complete and updated version including unsigned 64 bit integers...mmmmmm....... Lots of fun...
5
by: trillianx | last post by:
I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: # Find the prime numbers # This is a user input program that lets you...
11
by: aidasa2001 | last post by:
I need a program which can say the number (input)is prime and then write other prime numbers with that digits. eg.for 13 print the number is prime and print 113( another prime num with that digits)...
4
by: Villanmac | last post by:
Hi i have made the following program to make a fucntion that determines if a number is prime. Than i am supposed to use this function to print all prime numbers between 1 and 10000. What am I...
8
by: cokofreedom | last post by:
I was reading up on this site http://www.noulakaz.net/weblog/ 2007/03/18/a-regular-expression-to-check-for-prime-numbers/] of an interesting way to work out prime numbers using Regular Expression....
12
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.