Well there are several ways to calculate prime numbers. Although it is not really the fastest, of the standard algorithms is the
Sieve of Eratosthenes.
To implement it is actually really easy. You create an array filled with numbers from 2 to N and then:
a. get the first number in the array and add it to your list of primes.
b. Iterate though the array by multiples of that last prime found setting those numbers to 0.
c. As long as your top prime is less then sqrt(N) then goto step a.
d. If your largest prime is greater than sqrt(N) then add all remaining non-zero numbers in the array to your list of primes.
Once you have an good long list of primes you can use that to check the primarily of all numbers up to square of the largest prime in your list. To do this you just make sure than a number 'A' is not divisible by all primes less than sqrt(A).