can someone please write a small programs showing how to find if the number you entered is prime.If the number entered is not prime it will tell you the next highest prime number. Thanks in advance.

Printable View

- 11-14-2002Clean KillaHelp with prime numbers
can someone please write a small programs showing how to find if the number you entered is prime.If the number entered is not prime it will tell you the next highest prime number. Thanks in advance.

- 11-14-2002master5001
Well no I can't. I was going to give you a program that generates prime numbers but I can't find in on my computer :( Sorry.

But you just do a search. Once you have a list of primes you can make a lookup table in order to accomplish the task you desire. - 11-14-2002Clean Killa
Thanks for the help man....anyone else please i need this to help pass my college class. Thanks again

- 11-14-2002Prelude
>anyone else please i need this to help pass my college class.

No, do your own homework. And before you post again, be sure to read this.

-Prelude - 11-14-2002...
this is a homework problem, and the people on this board dont take too kindly to doing other people's homework.

but i will help guide you on the path to enlightenment. you will have to write the code yourself.

let us forget about code for a moment and consider the definition of a prime number. a prime number is a number divisible only by 1 and itself.

now, let us say that we have been given a number as input. how would we determine if it is a prime number.

how about we just throw away the number 2, since it is divisible by 1 and every number in between itself and 1. 2 will always be prime, so:

if ( "input" == 2 ) cout << "the number is prime";

ok, that is taken care of. now, as for every number above 2. well, a number will never be divisible by over half of itself, so we will start there.

( pseudocode: )

is "input" % ( "input" / 2 ) equal to 0? if so, then its not prime. (dont worry about odd numbers. as long as you are using ints, it will round down)

now we put ("input" / 2 ) into a variable, say "x"

we now go down the line:

"input" % x = ?

"input" % x - 1 = ?

"input" % x - 2 = ?

"input" % x - 3 = ?

all the way until x = 2. if the answer to any of the above is ever 0 then it is not a prime number. if it gets all the way to the bottom of the list without getting a 0,then it is a prime number.

and there you have it. a program to check for primes.

as far as the next highest prime: its a pretty simple solution, so dont get overwhelmed. but you are going to have to figure it out yourself. use the method described above. dont think about the code until you know how you want the problem solved in plain english. - 11-14-2002Clean Killa
thx i hope this works