Look up Sieve of Eratosthenes: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

That will allow you to build an array of prime numbers. Then you just need to see if your number is divisible by Prime(N) where N <= 1/2 X where X is your number. Another algorithm is if your number is even, divide by two until it isn't, then if the result ends in 5, divide by 5 until the last digit isn't 5, then factor that result. Example:

X = 100

X is even, so 2 is a factor.

X / 2 = 50

50 / 2 = 25, ends in 5 so 5 is a factor

25 / 5 = 5, 5 is prime, so no more factors

Results: factor(100) = {2, 5}

What about X = 101?

101 / 2 = 50 (drop fraction), but 50 is not prime

Is 50 prime? No, because it is divisible by 2 and 5

Is 49 prime? Yes. Is 101 divisible by 49? No

Is 47 prime? Yes. Is 101 ... ? No.

Is 43 prime? Yes. ... No.

Is 41 prime? Yes. ... No.

Is 39 prime? No. Divisible by 3 and 13.

Is 37 prime? Yes. ... No.

Is 33 prime? No. Divisible by 3 and 11.

Is 31 prime? Yes. Is 101 divisible by 31? No.

Is 29 prime? Yes. ... ? No.

Is 27 prime? No ... divisible by 3

23? Yes. Divisible? No.

21? No. 3 and 7

19? Yes. Divisible? No.

17? Yes. Divisible? No (6 x 17 = 102 - close but no banana)

13? Yes. Divisible? No.

11? Yes. Divisible? No.

7? Yes. Divisible? No.

5? Yes. Divisible? No.

3? Yes. Divisible? No.

Ok - done. 101 is prime, has no factors other than 1 and self.

There is a much more efficient binary search using a reasonably sized sieve of erathosthenes, but that is too advanced to cover here. I implemented that once and could determine if any number 15 digits or less was prime in 3-4 lookups or less. Very fast when combined with the heuristic rules I outlined above (even? Divide by 2 until remainder is not even. If remainder ends in 5, divide by 5 until not. Check remainder if prime using sieve. If not prime, find largest prime divisor <= 1/2 remainder, divide and check remainder again until remainder is prime... done).

Anyway, there is one important lesson here. ALWAYS work out your algorithm first, then write pseudo-code, then convert pseudo-code into the language of choice.

@john

we are not here to write code for you. it is very necessary to show us your efforts in order to get assistance.

so , post your effors(your code) and then any assistance will be possible.

And , don't forget to create a new thread for a question. don't load too much in a single thread. right ?

thanks.