PHP calculates the number of fools

Source: Internet
Author: User

Description of the ugly number

A positive integer containing only the factor 2,3,5 is called an ugly number, such as 4,10,12 are ugly, and 7,23,111 is not an ugly number.

The method of judgment is first except 2, until it cannot be divisible, and then except 5 until it is divisible, and then 3 until it is not divisible. The final judgment is whether the remaining number is 1, if 1 is the ugly number, otherwise it is not the ugly number. For example 8 divided by 2 equals 4, 4 is divided by 2 equals 2, contains factor 2, so is the number of ugly.   In the example 14, 14 divided by 2 equals 7 because it contains a factor of 7, not an ugly number. The ugly number can only be divisible by 2,3,5. That is, if a number is divisible by 2, we divide it by 2, and if it is divisible by 3, it is divided by 3, and if it is divisible by 5, it is divided by 5. If the last thing we get is 1, then this number is the ugly number, otherwise it's not.
    1. 8/2 = 4 Ugly number
    2. 6/2 = 3 Ugly number
    3. 14/2 = 7 is not an ugly number

function _isuglynb ($nums) {    if (!is_numeric ($nums)) {        return ' It is not a number ';    }    Whether it can be divisible by 2    while ($nums%2 = = 0) {        $nums = $nums/2;    }    Whether it can be divisible by 3    while ($nums%3 = = 0) {        $nums = $nums/3;    }    Whether it can be divisible by 5    while ($nums%5 = = 0) {        $nums = $nums/5;    }    if ($nums = = 1) {//ugly       return 1;    } else{       return 0;}    } echo _isuglynb (8);   1 echo _isuglynb (6);   1 echo _isuglynb (+);  0

PHP calculates the number of fools

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.