Sword refers to the offer: (Java implementation) ugly number __java

Source: Internet
Author: User

* We include only factor 2,3, and 5 is called the ugly number. Find the 1500th ugly number in order from small to large. For example, 6, 8 are ugly, but 14 is not because it contains factor 7. We used to think of 1 as the first ugly number *

This procedure includes the general method and the optimal method. The contribution of this blog is to use Java to achieve the sword refers to the offer face test questions 34: Ugly number.
Related explanation here no longer repeat (and the sword refers to the question 34 algorithm thinking the same).
The focus is: the use of space in exchange for time, the efficiency of 10,000 times times.
General Method: to determine whether each number is an ugly number.
Using the Auxiliary space method : Use an array to save, fast read.

public class Jz34_uglynumber {public static void main (string[] args) {Long begin = System.currenttimemillis ();
        This code is placed before the program executes//The normal method executes more than 10 seconds//system.out.println (Getoneuglynumber (1500));
        The use of auxiliary space to submit efficiency, execution time of 1 milliseconds, the efficiency of 10,000 times times.

        SYSTEM.OUT.PRINTLN (GetResult (1500)); Long end = System.currenttimemillis ()-Begin;
    This code is placed after program execution System.out.println ("Time consuming:" + end + "milliseconds");
        public static Boolean isuglynumber (Long num) {if (Index < 1) {return 0;
        while (num% 2 ==0) num/= 2;
        while (num% 3 ==0) num/= 3;
        while (num% 5 ==0) num/= 5; return num = 1?
    True:false;
        public static long Getoneuglynumber (int index) {long i = 0;
        Long ci = 0;
            while (CI < index) {i++;
            if (Isuglynumber (i)) {ci++;
    } return i; } public static Long getresult (int index) {if (Index < 1) {return 0;
        } long[] array = new Long[index];
        Array[0] = 1;
        int nextindex = 1;
        Long tar2 = 1;
        Long tar3 = 1;
        Long TAR5 = 1; int t2i = 0,t3i=0,t5i=0; The subscript that holds the last minimum larger than the maximum number of ugly numbers is used for the next time to look for ugly times, avoiding duplication of calculations and comparisons while (Nextindex < index) {for (int i = T2i; i< = Nextindex;
                    i++) {if (Array[i]*2 > Array[nextindex-1]) {tar2 = array[i]*2;
                    T2i = i;
                Break for (int i = T3i; i<= nextindex i++) {if (Array[i]*3 > Array[nextindex
                    -1]) {tar3 = array[i]*3;
                    T3i = i;
                Break for (int i = t5i; i<= nextindex i++) {if (Array[i]*5 > Array[nextindex
          -1]) {TAR5 = array[i]*5;          t5i = i;
                Break

        } array[nextindex++] = Getminfromthree (TAR2,TAR3,TAR5);

    return array[index-1];
    public static long Getminfromthree (long A,long b,long c) {return math.min (Math.min (A, B), c); }
}
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.