Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3630774
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:18:45+00:00 2026-05-19T00:18:45+00:00

Numbers whose only prime factors are 2, 3, or 5 are called ugly numbers

  • 0

Numbers whose only prime factors are 2, 3, or 5 are called ugly numbers.

Example:

1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... 

1 can be considered as 2^0.

I am working on finding nth ugly number. Note that these numbers are extremely sparsely distributed as n gets large.

I wrote a trivial program that computes if a given number is ugly or not. For n > 500 – it became super slow. I tried using memoization – observation: ugly_number * 2, ugly_number * 3, ugly_number * 5 are all ugly. Even with that it is slow. I tried using some properties of log – since that will reduce this problem from multiplication to addition – but, not much luck yet. Thought of sharing this with you all. Any interesting ideas?

Using a concept similar to Sieve of Eratosthenes (thanks Anon)

    for (int i(2), uglyCount(0); ; i++) {
        if (i % 2 == 0)
            continue;
        if (i % 3 == 0)
            continue;
        if (i % 5 == 0)
            continue;
        uglyCount++;
        if (uglyCount == n - 1)
            break;
    }

i is the nth ugly number.

Even this is pretty slow. I am trying to find the 1500th ugly number.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:18:46+00:00Added an answer on May 19, 2026 at 12:18 am

    A simple fast solution in Java. Uses approach described by Anon..
    Here TreeSet is just a container capable of returning smallest element in it. (No duplicates stored.)

        int n = 20;
        SortedSet<Long> next = new TreeSet<Long>();
        next.add((long) 1);
    
        long cur = 0;
        for (int i = 0; i < n; ++i) {
            cur = next.first();
            System.out.println("number " + (i + 1) + ":   " + cur);
    
            next.add(cur * 2);
            next.add(cur * 3);
            next.add(cur * 5);
            next.remove(cur);
        }
    

    Since 1000th ugly number is 51200000, storing them in bool[] isn’t really an option.

    edit
    As a recreation from work (debugging stupid Hibernate), here’s completely linear solution. Thanks to marcog for idea!

        int n = 1000;
    
        int last2 = 0;
        int last3 = 0;
        int last5 = 0;
    
        long[] result = new long[n];
        result[0] = 1;
        for (int i = 1; i < n; ++i) {
            long prev = result[i - 1];
    
            while (result[last2] * 2 <= prev) {
                ++last2;
            }
            while (result[last3] * 3 <= prev) {
                ++last3;
            }
            while (result[last5] * 5 <= prev) {
                ++last5;
            }
    
            long candidate1 = result[last2] * 2;
            long candidate2 = result[last3] * 3;
            long candidate3 = result[last5] * 5;
    
            result[i] = Math.min(candidate1, Math.min(candidate2, candidate3));
        }
    
        System.out.println(result[n - 1]);
    

    The idea is that to calculate a[i], we can use a[j]*2 for some j < i. But we also need to make sure that 1) a[j]*2 > a[i - 1] and 2) j is smallest possible.
    Then, a[i] = min(a[j]*2, a[k]*3, a[t]*5).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want the messagebox to only show if the number is equal to 0.
We manage a site for a medical charity. They have a number of links
I have several USB mass storage flash drives connected to a Ubuntu Linux computer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.