Multiple return paths in isPrime() function for prime number series – CSKC
Sign In

Search

Code Learning #CPP#2449    siteicon   siteicon  

Multiple return paths in isPrime() function for prime number series

Using a bool output function isPrime() which has multiple return paths for prime number check and then using this function to print a prime series for given number of terms

Learning Objectives

  • Learning the reasoning behind multiple return paths from user defined functions.
  • Learning to return the bool data type for functions that return true or false conditions.
  • The novel and efficient technique of generating a prime number series, assuming that more numbers are not prime.

Program Approach

  • Function returns false when input is less than 2.
  • Function also returns false if input is divisible by any number from 2 to half of the given number. (uses integer division to find the half of given number).
  • Assumes that if above two conditions are not met the given value is a prime number.
  • The function call is done in while condition and the looping keeps happening until a non-prime number is struck. As soon as a prime is found loop comes out.

Source Code

TC++ #2449

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Alternate (?) :

Code Understanding

bool isPrime(int n)
This is a function which returns true if the number passed in argument is a prime number. This function has been placed above the main program so no separate function prototype is necessary.
{
if(n<2) return false; 
The first return path is when number is less than 2. As per definition number 1 and below can not be prime.

for(int i=2;i<=n/2;i++)
This loop begins from 2 and ends at half of the number as any divisor other than 1 an n will be in this range only.

if(n%i==0) return false;
If any division from 2 to n/2 is found it is definitely not a prime number so function returns.

return true;
If none of the divisors in 2 to n/2 have been found, so the number must be prime.
}

int term=2; //This is the first number to be printed for prime number
for(int tc=1;tc<=no_of_terms;++tc)
This outer loop goes from 1 to no_of_terms which has been set to 100 here.
{
while(!isPrime(term)) term++;
If the given number is not prime the loop keeps on moving further and a new next possible term is being searched so term is post incremented.

cout<<term++<<” “;
If the above while search has failed then the number must be prime and thus it can be printed and it can be post incremented so the search of another term keeps happening until the desired number of terms are printed using the outer loop.

Notes

  • Prime number series can be very taxing for even a super computer. Here we are printing just 100 terms. However you may give number of terms as 10,00,000. You will find that computer the may take many minutes or some time hours to keep printing this series.
  • The challenge of finding largest prime number is a mathematicians delight. It is some times said that the largest prime number challenge will remain active till the mathematicians remain anywhere on this planet. You may be interested to know more about it from
    https://www.mersenne.org/

Common Errors

  • while(!isPrime(term)) term++;


Suggested Filename(s): fn-isprime.cpp



Share

Created: 10-Jan-2018 21:02 | Updated: 10-Jan-2018 21:31|