Wondering how to use a function to obtain the prime number

Source: Internet
Author: User
Use a function to obtain the question of Prime Number // & nbsp; obtain the prime number function & nbsp; main () {for ($ I = 100; & nbsp; $ I & lt; 201; & nbsp; $ I ++) {$ B & nbsp ;=& nbsp; false; for questions about getting prime numbers using a function

// Obtain the prime number between and
Function main ()
{
For ($ I = 100; $ I <201; $ I ++ ){
$ B = false;
For ($ j = 2; $ j <$ I-1; $ j ++ ){
$ K = $ I % $ j;
If ($ k = 0 ){
$ B = true;
// Break;
}
}
If (! $ B ){
Echo $ I ."
";
}
}
}

// My doubts are at the break location. The final result of the test is the same if there is any break.
// But if you want to use a program to explain the mathematical definition, you have to add break, right?
Share:
------ Solution --------------------
Whether or not break does not affect the print result, because the print condition is the $ I prime number.
But for operational efficiency, when there is no break, it needs to be relatively complete data ($ i-1) to end, and when there is break, it ends when the first sum is found, the subsequent ineffective work will not have to be done again.
------ Solution --------------------
Reference:
Whether or not break does not affect the print result, because the print condition is the $ I prime number.
But for operational efficiency, when there is no break, it needs to be relatively complete data ($ i-1) to end, and when there is break, it ends when the first sum is found, the subsequent ineffective work will not have to be done again.

+ 1

When you try to calculate a prime number within 20000 or greater, you will know the difference between a break and a non-break. you might as well test it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.