The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

i'm not looking for someone to explicitly answer this but a few hints would be great. i'm trying to write a couple of functions for determining prime numbers. the first asks for the user to enter an integer which is passed to a function with a return value indicating whether the integer is a prime or not. before i pass the integer i determine if it's even using i%2 approach. if it is even (except 2) i don't send it, i just cout that it's not a prime.

once the int is passed to the function it's assumed to be odd and cannot be divided by an even number so i set up a for loop to modulus by only odd numbers 3,5,7 and 9 with 0 remainder returning to the calling function that the number is not prime.

second is an algorithm to calculate all primes between 2 and 10000.

my problem is that i get on occasion in the list of primes a few that are not primes.

can someone set me off in the right direction? thanks, mark

once the int is passed to the function it's assumed to be odd and cannot be divided by an even number so i set up a for loop to modulus by only odd numbers 3,5,7 and 9 with 0 remainder returning to the calling function that the number is not prime.

second is an algorithm to calculate all primes between 2 and 10000.

my problem is that i get on occasion in the list of primes a few that are not primes.

can someone set me off in the right direction? thanks, mark

so, use a queue or stack or something, initialize with the first few primes (like up to 11) and go through the odd numbers to 10000, testing by dividing by all the numbers in the queue, adding the number to the queue if it is prime and rejecting it if it is not...

(i hope i didnt say too much...)

mark

but you can narrow it down more than that... the numbers that divide it have to be less than its square root

i.e. say if you were checking if 5249 is a prime... take its square root (which is 72.45) and divide it by every odd number between 3 and the square root....so start at 3 and go to 71, if none of those divide it then it is prime

thanks for your input.

mark

All Courses

From novice to tech pro — start learning today.

(i hope i didnt say too much...)

ok... well, think about this, you weeded out all the even numbers,

so for any particular odd number, it will be divisible by only odd numbers

but you can narrow it down more than that... the numbers that divide it have to be less than its square root

i.e. say if you were checking if 5249 is a prime... take its square root (which is 72.45) and divide it by every odd number between 3 and the square root....so start at 3 and go to 71, if none of those divide it then it is prime...

to gothick... thats what i was going to teach him before i found out he hadnt go to any container classes yet... (i had just forgotten the name of it, but the algorithm is in my STL book if anyone wants it)