473,399 Members | 3,401 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,399 software developers and data experts.

prime number routine

don
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime
number or not...... all I'm asking for is Not the exact code ( well maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....
Jul 22 '05 #1
11 7114
"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is prime....


Here are two relevant links:

http://www.cse.iitk.ac.in/news/primality.html
http://www.parashift.com/c++-faq-lite/

HTH.

Jonathan
Jul 22 '05 #2
Ken
Well, this is really inefficient, but

int i = inputted number
int j = i - 1;
int prime = 1; //0 for false, 1 for true
for (j > 1; j--)
{
if ( (i/ j == 0)
{
//is not prime
prime = 0;
break;
}
}
if (prime == 1) prime;

else { not prime}
"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is
prime....

Jul 22 '05 #3
Well this isn't the best way to do it but here is an idea:

Take the number that was input and divide it by all numbers up to and
including that number.

Each time the number comes out even add 1 to count and when it is done if
count is greater than 2 then the number is not prime!!!

Or if you do not want to do it that way you can mod it up to and including
that number and each time that the mod is 0 then add 1 to count and if count
is > 2 then you know it is not prime!!!

Shawn
"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is
prime....

Jul 22 '05 #4

"Ken" <s4******@student.uq.edu.au> wrote in message
news:c0**********@bunyip.cc.uq.edu.au...
Well, this is really inefficient, but

int i = inputted number
int j = i - 1;
int prime = 1; //0 for false, 1 for true
for (j > 1; j--)
{
if ( (i/ j == 0)
{
//is not prime
prime = 0;
break;
}
}
if (prime == 1) prime;

else { not prime}


Not only inefficient, its also incorrect, doesn't compile, doesn't work even
when syntax errors are fixed.

Please don't do others homework. Request for algorithm is OK, request for
code is not, at least in my book.

john
Jul 22 '05 #5

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c0*************@ID-216073.news.uni-berlin.de...
"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...
Ok, this is a homework assignment, but can you help me out

anyway...... I
need a routine for figuring out if a number inputted by the user is

a prime
number or not...... all I'm asking for is Not the exact code ( well

maybe a
little) but the logic or algorithm to tell whether or not a number

is
prime....


Here are two relevant links:

http://www.cse.iitk.ac.in/news/primality.html


This is indeed a very fine piece of work! However, I doubt that it will be
of much help to a newbie because if you're able to understand the approach
to primality starting from Fermat's little theorem you can easily come up
with more trivial solutions.
BTW the Miller-Rabin-Probabilistic primality test is also nice :-)

Cheers
Chris
Jul 22 '05 #6

"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is
prime....


There are various ways to test for primality. One would be the following:

Consider two thresholds (Min & Max) and an input variable x which you would
like to test for primality. A prime is typicall characterized by the fact
that the modulo division by another number should not equal 0, except that
number is the input value itself or 1. And this is exactly what the (quite
simple) algorithm builds on to.

First of all set Min and Max to 2. The basic idea is to test the modulo
division of our input x against a bunch of other numbers and we're trying to
keep this test efficient by eliminating redundant tests. Loop as long as Min 1 and x does not equal the Max threshold.


To begin with check whether the modulo division of x with Min (remember
that's 2 during the first looping) does not equal zero. If it does you can
immediately deduce that x is not a prime. If it does we adjust our Max
threshold by setting it to x / Min. This way we eliminated a whole lot of
redundant tests immediately. Furthermore check if Max <= 1. If this is true
then x is also a prime. Subsequently we will put our Max threshold into
action by testing whether x % Max != 0. If this is true then x is no prime
either, just as above. However, if it is not true we have to adjust our Min
threshold before starting the loop again. Our new Min threshold is set to x
/ Max + 1.

Well and that's all. This is certainly not the most efficient algorithm for
large prime numbers (there you have to resort to mathematical tests based on
Fermat's little theorem and probabilistic approaches), but it is certainly
more efficient than the brute force method.

HTH
Chris
Jul 22 '05 #7
don wrote:

Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime
number or not...... all I'm asking for is Not the exact code ( well maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....


For all the regulars: The follwing is a simple exmplanation
which does not take any optimization or efficiency into account.
It is ment to bring the OP up to speed.

Well. When is a number considered to be prime?
A number is prime, if there is no other number except
1 and the number itself which divides that number evenly.

Example:

5 is prime, since you cannot find another number except
1 and 5, which divides 5

15 is not prime, since 5 divdes 15 evenly

it is very clear, that if such a potential divisor exists, it cannot be
greater then the number itself because in that case the quotient will
always be less then 1. Example: 15 / 32. Even if we don't know the
exact result, we know for sure that 15 / 32 will be less then 1.

OK. So from this we know, that it is sufficient to test all numbers
starting from 2 up to out number in question minus 1. If such a test
number divides the number in question, then the number in question
is not a prime number, otherwise it is.

Example:

5

quotient remainder
5 / 2 2 1
5 / 3 1 2
5 / 4 1 3

In no case were we left with a remainder of 0, which would indicate
an even division. Thus 5 is prime.
15

quotient remainder
15 / 2 7 1
15 / 3 5 0

Uups: 25 is evenly divisable by 5, thus 25 is not a prime
number.

So what did we do?

We looped (hint!) through some test numbers to see if one
of them devides our number evenly (remainder == 0)
If one does, then the number is not prime.

Now this translates nicely into a programming structure

int Number = 25;
bool IsPrime = true; // assume number is prime
// if one of the tests shows that the assumption
// is wrong, this flag will be changed

for( int TestNumber = 2; TestNumber < Number; TestNumber++ )
{
if( Number % TestNumber == 0 )
IsPrim = false;
}

if( IsPrime )
std::cout << Number << " is prime\n";
else
std::cout << Number << " is not prime\n";
Now that we have a working solution it is time to think about ways to
improve it. For this you have 2 answer 2 questions:

* is it really neccessary to test all numbers up to Number - 1?
* once a divisor is found, is it really neccessary to test other numbers?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
"don" <do*@panix.com> wrote in message news:<c0**********@reader2.panix.com>...
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime
number or not...... all I'm asking for is Not the exact code ( well maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....


to see if k is prime, there are 2 ways:

1) test if any _integer_ from 2 to sqrt(k), and if that integer
divides k then k is not prime

2) do the following on startup to generate a prime list (i personally
hate this way even though it's good). Start from n=2 and continue for
as long as you (or the one who set the problem) want, and erase the
multiples of n from the prime list).

BTW: I think the first way is so easy you could've thought of it
yourself :/
Jul 22 '05 #9
"don" <do*@panix.com> wrote in message news:<c0**********@reader2.panix.com>...
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime
number or not...... all I'm asking for is Not the exact code ( well maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....


Precompute a big table of primes up to some maximum size.

Get your number. If it is smaller than the max, then
if it is in the table it is prime and if not not. Miller time.

If it is bigger than your max, then you have to start doing
some dividing. But first, you don't need to divide by anything
bigger than the square root of the number. (You have to explain
why.) And you don't need to divide by any number that is itself
not prime. (Again, you have to explain why.)

So, if your number is smaller than the square of the max of your
table, again, the scheme is pretty easy. You only need to divide
by the numbers in your table.

If your number is bigger than that, then you are on your own.
Socks
Jul 22 '05 #10
Let the number be n. If n is not prime, it is composite
and can be written as n=p*q. Let r = min(p,q). Then r is
at most sqrt(n). So you have to test for divisibility against
r/2 numbers to find a possible odd divisor.

Here is a simple brute force algorithm:

Input: non-negative integer n
Output: the answer to the question: "Is n a prime number?"
BEGIN
if n < 2 or n is even
then output NO
else if n==2
then output YES
else let i=3
while i < sqrt(n) do
if i divides n
then output NO
i = i+2
output YES
END

This algorithm is exponential in the length of the representation of the
number n.
As long as the number n is restricted to be less than, say, 10^10, it
shouldn't take too long.

On Tue, 10 Feb 2004 01:49:29 -0500, don <do*@panix.com> wrote:
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a
prime
number or not...... all I'm asking for is Not the exact code ( well
maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #11
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:c0**********@sunnews.cern.ch...

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message

Here are two relevant links:

http://www.cse.iitk.ac.in/news/primality.html


This is indeed a very fine piece of work! However, I doubt that it

will be of much help to a newbie because if you're able to understand the approach to primality starting from Fermat's little theorem you can easily come up with more trivial solutions.
The link was a bit tongue-in-cheek, since the OP explicitly said it
was for a homework assignment. However, you can find a lot of good
links on primality testing starting from the one I gave.
BTW the Miller-Rabin-Probabilistic primality test is also nice :-)


My old professor Bob Solovay had a good one, too.

Jonathan

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.