263. uugly Number (C ++), 263 uglynumber

Source: Internet
Author: User

263. uugly Number (C ++), 263 uglynumber
263. Ugly Number

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

Uugly numbers are positive numbers whose prime factors only include2, 3, 5. For example,6, 8Are uugly while14Is not uugly since it has des another prime factor7.

Note that1Is typically treated as an uugly number.

The so-called ugly number is the number that cannot be divisible by other prime numbers other than 2, 3, and 5. 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15 are the first 11 ugly figures.

Question:

Write a program to determine whether a given number is an "ugly number" uugly number

An ugly number is a positive integer that contains only the prime factor 2, 3, and 5. For example, 6 and 8 are ugly numbers, but 14 are not because they contain additional quality factor 7.

Note that number 1 is also regarded as an ugly number.

 

Question completion method:

Division of 2, 3, and 5

 

 

C ++ code:
 1 class Solution { 2 public: 3     bool isUgly(int num) { 4         if(num<=0) return false; 5         vector<int> v{2,3,5}; 6          7         for(auto c:v) 8         { 9             while(num%c==0) num/=c;10         }11         return num==1;12     }13 };

 

Related Article

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.