Php obtains the number that can be divisible by 1 and itself.

Source: Internet
Author: User
Php obtains the sum of values less than 89 that can be divided by 1 and itself. I cannot find a clue about this. I want to find a method to instruct php to obtain the number of divisible values of 1 and itself within 89. I cannot find a clue about this.

Reply content:

Php obtains the sum of values less than 89 that can be divided by 1 and itself. I cannot find a clue about this.

I have encountered this problem. I remember it was encountered when I went to a company for an interview. I specially sorted it out. you can refer to it for a good understanding.

Final function getPrimesNumber ($ number) {$ primes = array (); for ($ I = 1; $ I <$ number; $ I ++) {for ($ j = 2; $ j <$ I; $ j ++) {if ($ I % $ j = 0) {continue 2 ;}} $ primes [] = $ I;} return $ primes ;}

YesOnlyThe number that can be divided by 1 and itself, that is, the prime number.

$ Max = 89; for ($ I = 1; $ I <= $ max; $ I ++) {$ k = 0; for ($ j = 1; $ j <$ I; $ j ++) {if ($ I % $ j = 0) {$ k ++ ;}} if ($ k = 1) {echo $ I. '\ n ';}}

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.