# Pseudo code for prime numbers?



## huggies12345

Hi,

If there are two numbers input into a program. 
How do i print out all of the prime numbers inbetween?

Example:

Num1 = 1

Num2 = 10


Answer = 2, 3, 5, 7


----------



## Special2God

Code:


// Loop through all the values between the two numbers the user entered
loop(possiblePrime : num1 through num2) {
   isPrime = true; // until proven otherwise

   // Loop through all values up to the possible prime
   loop(divisor : 1 through possiblePrime) {

      // If the prime is divisible by some value with no remainder, than it is not a prime
      if( (possiblePrime % divisor) == 0) {
         isPrime = false;
      }

   }

   // If the prime was not divisible by any value, it is a prime
   if(isPrime) {
      print(possiblePrime); // Display the prime
   }
}

Note that this code would be extremely slow for larger numbers, such as 2,000,000 through 8,000,000. 
The loops would have to iterate trillions of times making it very inefficient. 
You could improve this by only checking odd numbers (every other numbers starting at an odd number [possiblePrime+=2]). 
You could also skip every fifth number starting at a value divisible by 5. 
Skip (2, 4, 6...) (5, 10, 15...) (3, 6, 9...)
Skipping these three patterns would halve the number of values that need checking.


----------



## Special2God

Sorry to double post, I just tested my code and noticed this needs changing: 


Code:


loop(divisor : 1 through possiblePrime) {
-to>
loop(divisor : 2 through possiblePrime) {

because all numbers are divisible by one, meaning no numbers would be prime. 
Note, I can post this as C, Java, or JavaScript if you want, I don't have experience in other languages, although loops and if statements should be about the same in most languages. 
Also, sorry if the loop syntax is confusing, the first word is the variable name and the later part after ':' is what values to loop through, from the loop start value to the end value.


----------

