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.
15 8387
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.
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.
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 ;)
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.
-
'--------------
-
Public Function IsPrime(ByVal TestPrime As Long) As Boolean
-
Dim TestNum As Long
-
Dim TestLimit As Long
-
-
' Eliminate even numbers
-
If TestPrime Mod 2 = 0 Then Exit Function
-
-
' Loop through ODD numbers starting with 3
-
TestNum = 3
-
TestLimit = TestPrime
-
Do While TestLimit > TestNum
-
-
If TestPrime Mod TestNum = 0 Then
-
' Uncomment this if you really want to know
-
'MsgBox("Divisible by " & TestNum)
-
Exit Function
-
End If
-
-
' There's logic to this. Think about it.
-
TestLimit = TestPrime \ TestNum
-
-
' Remember, we only bother to check odd numbers
-
TestNum = TestNum + 2
-
Loop
-
-
' If we made it through the loop, the number is a prime.
-
IsPrime = True
-
End Function
-
and usage: -
If IsPrime(<<<YOUR-VALUE>>>) = "True" Then
-
MsgBox("PRIME")
-
Else
-
MsgBox("NOT PRIME")
-
End If
-
As kadghar pointed out, TestLimit probably should be set to Sqr(TestPrime), to reduce or eliminate wasted effort.
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 :)
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
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
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
I know that evry prime number is of the form:
6r - 1
OR
6r + 1
Except the prime numbers 3 and 5
Try it!!!!
6r - 1
OR
6r + 1
What does r represent?
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. -
If n / 2 = Int(n / 2) Or n / 3 = Int(n / 3) Then
-
MsgBox ("This ain't a prime number buddy")
-
End If
-
For r = 1 To Int((n ^ 0.5) / 6) + 1
-
If n / (6 * r + 1) = Int(n / (6 * r + 1)) Or n / (6 * r - 1) = Int(n / (6 * r - 1)) Then
-
MsgBox ("This ain't a prime number buddy")
-
End If
-
Next
-
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.
:)
A little clarification on what r represents?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: newstips6706 |
last post by:
1, 2, 3, 5, 7... PRIME Numbers
________________________________
Definitions
What is a PRIME Number ?
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |