Leetcode 263 Ugly Number

Source: Internet
Author: User

Problem:

Write a program to check whether a given number was an ugly number.

Ugly numbers is positive numbers whose prime factors only include 2, 3, 5 . For example, was ugly while was not 6, 8 14 ugly since it includes another prime factor 7 .

Note that's 1 typically treated as an ugly number.

Summary:

Determine if a number is ugly number, which is only 2, 3, 5 of the mass factor.

Analysis:

The method of judging is to divide continuously by 2, 3, 5, see whether the final can get 1. The following two methods are similar.

1 classSolution {2  Public:3     BOOLisugly (intnum) {4         if(Num <=0)return false;5         6          while(num! =1) {7             if(num%2==0) Num/=2;8             Else if(num%3==0) Num/=3;9             Else if(num%5==0) Num/=5;Ten             Else return false; One         } A          -         return true; -     } the};

1 classSolution {2  Public:3     BOOLisugly (intnum) {4         if(Num <=0)return false;5         6          while(Num >=2&& num%2==0) Num/=2;7          while(Num >=3&& num%3==0) Num/=3;8          while(Num >=5&& num%5==0) Num/=5;9         Ten         returnnum = =1; One     } A};

Leetcode 263 Ugly Number

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.