263. Ugly number && 264. Ugly number II && 313. Super Ugly Number

Source: Internet
Author: User

263. Ugly Numberwrite 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.

Hide TagsMathHide Similar Problems(e) Happy number (e) Count Primes (M) Ugly number II
 Public classSolution { Public Booleanisugly (intnum) {        if(num = = 0)            return false; Num= Removefactor (num, 2); Num= Removefactor (num, 3); Num= Removefactor (num, 5); returnnum = = 1; }        Private intRemovefactor (intNumintfactor) {         while(num%factor==0) {num= num/factor; }        returnnum; }}

Ugly number II

Write a program to find the n -th ugly number.

Ugly numbers is positive numbers whose prime factors only include 2, 3, 5 . For example, is the sequence of the first 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 10 ugly numbers.

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

Hint:

    1. The naive approach is to call for isUgly every number until you reach the nth one. Most numbers is not ugly. Try to focus your effort in generating only the ugly ones.
    2. An ugly number must is multiplied by either 2, 3, or 5 from a smaller ugly number.
    3. The key is what to maintain the order of the ugly numbers. Try A similar approach of merging from three sorted lists:l1, L2, and L3.
    4. Assume you have Uk, the kth ugly number. Then uk+1 must is Min (L1 * 2, L2 * 3, L3 * 5).

Hide Tags

Dynamic Programming Heap MathHide Similar Problems(H) Merge K Sorted Lists (E) Count Primes (E) Ugly number (m) Perfect squares (m) Super Ugly number
 Public classSolution { Public intNthuglynumber (intN) {if(n==1)return1; Priorityqueue<Long> q =NewPriorityqueue (); Q.add (1l);  for(LongI=1; i<n; i++) {            LongTMP =Q.poll ();  while(!q.isempty () && Q.peek () ==tmp) TMP =Q.poll (); Q.add (TMP* *); Q.add (TMP*); Q.add (TMPThe); }        returnQ.poll (). Intvalue (); }}

313. Super Ugly Number

Write a program to find the nth super ugly number.

Super ugly numbers is positive numbers whose all prime factors is in the given prime list of primes size k . For example, is the sequence of the first 4 [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]  super ugly numbers given primes = of [2, 7, 13, 19] size.

Note:
(1) is 1 a super ugly number for any given primes .
(2) The given numbers in is in primes ascending order.
(3) 0 < k ≤100, 0 < n ≤106, 0 < primes[i] < 1000.

Hide TagsMath HeapHide Similar Problems(M) Ugly number IISlight modify the solution above would get a timelimitexceeded solutions
 Public classSolution { Public intNthsuperuglynumber (intNint[] primes)        {Arrays.sort (primes); if(n==1)             return1; Priorityqueue<Long> q =NewPriorityqueue (); Q.add (1l);  for(LongI=1; i<n; i++) {            LongTMP =Q.poll ();  while(!q.isempty () && q.peek () = =tmp) TMP=Q.poll ();  for(Integer p:primes) {q.add (TMP*p); }        }        returnQ.poll (). Intvalue (); }}

263. Ugly number && 264. Ugly number II && 313. Super 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.