### Writing a function to get the Prime Numbers between two numbers.

Hey everyone! So I am in my first c++ class and am starting to hit the part where I'm struggling to really grasp everything. For a small project we have to write a program to get two numbers from input and then take those two numbers and display the prime numbers between them. The instructions for the this functions is:

Let result be a Boolean variable.
Let limit be the integer (truncated) square root of the value sent in
If the number sent in is 0 or 1, set result to false
Otherwise, if the number sent in is 2, set result to true
Otherwise, if the number sent in is divisible by 2, set result to false
Otherwise (none of the above conditions has been met)
Assume the number sent in is prime (result is true)
Loop from 3 through limit, incrementing by 2
Check to see if the value sent in is divisible by the loop counter
If it is, then
Set result to false
End the loop and return.
Return the result.

The function to get the input from user is done and I have just about everything else done I just am stuck and can't figure where to start with the prime function. It seems so straight forward and layed out in instructions but i cant translate it into code. Any help or advice is greatly appreciated!
If needed i can attach some of the code i have written already if that will help.

code so far
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include #include using namespace std; int getInt(string prompt, int min, int max); int main(){ int startVal; int endVal; stringstream s; startVal = getInt("Enter starting integer (1-100): ",1,100); s << "Enter ending integer (" << (startVal+1) << " - 1000): "; endVal = getInt(s.str(), startVal+1,1000); cout << "Primes between " << startVal << " and " << endVal << '\n'; return 0; } int getInt(string prompt, int min, int max){ int num; bool valid = false; while (!valid) { cout << prompt; if (cin >> num) { if (num < min) { cout << "Error: The minimum value is " << min << endl; } else if (num > max) { cout << "Error: The maximum value is " << max << endl; } else { valid = true; } } else { cout << "Error: Invalid number\n"; cin.clear(); cin.ignore(1000, '\n'); } } return num; }``````
Last edited on
Yes, show what you have done so far.
I updated that. Sorry i should have just added in the first place.
You clearly know how to write a function. Good.

Should it return a value? If yes, which type?
 Let result be a Boolean variable. Return the result.

Should it take arguments?
 If the number sent in is 0 or 1, set result to false Otherwise, if the number sent in is 2, set result to true Otherwise, if the number sent in is divisible by 2, set result to false

At least one, a number, an int?

 Let limit be the integer (truncated) square root of the value sent in Loop from 3 through limit, incrementing by 2

A loop?

Was it already clear that the function takes one integer and returns `true`, if that integer is a prime?

The main loops from startVal to endVal and calls the above function with each value in that range.
this is what I came up with so far
 ``1234567891011`` ``````bool isPrime(testPrime){ bool result; if(testPrime = 0 || testPrime = 1){ result = false; } else if(testPrime=2){ result=true; } else if(testPrime % 2 == 0) { result =false }``````

I feel like im doing that wrong still. But i get even more confused when it gets to the
 Otherwise (none of the above conditions has been met) Assume the number sent in is prime (result is true) Loop from 3 through limit, incrementing by 2 Check to see if the value sent in is divisible by the loop counter If it is, then Set result to false End the loop and return.

The more i look at it the more i confuse myself unfortunately haha. Does the start atleast look like im heading in the right direction?
thank you for you help.
What is different on line 9 compared to lines 3 and 6?
A good compiler does warn about lines 3 and 6. It says something about assignment.
To compare equality (==) and to assign (=) are different operations.

Line 1, what is the type of function's argument?

The confusing part does have some wordings that might be ... confusing.
 ```Otherwise (is a synonym for 'else') Assume the number sent in is prime (Set result to true, you could do this on line 2) Let limit be the integer (truncated) square root of the value sent in (int limit = ...) LOOP counter from 3 through limit, incrementing by 2 (traditional 'for' loop) IF the value sent in is divisible by the loop counter THEN Set result to false End the loop and return. (use break;) END IF END LOOP END Otherwise Return the result.```
algorithm ideas:

the sieve algorithm is well known and oft-used. It is a little sluggish for large values.

the best algorithm I have found is to use gcd against the value and its factorial, but you need a way to get the factorial, store it, and GCD it. Ideally you would use the 'primorial' (all the primes multiplied together) rather that factorial to reduce the value's size.

you only need to check from 2 to the sqrt of N to find a factor. if it has none up to its root, it won't have any past it.