Sword refers to the offer face question 34: Ugly number Java Implementation __java

Source: Internet
Author: User
Tags reserved
title: Ugly number we call the inclusion factor 2,3, and the number 5 as the ugly number (Ugly numbers). Find the 1500th ugly number in order from small to large. For example, 6,8 are all ugly numbers, but 14 is not because it contains factor 7. In custom, we take 1 as the first ugly number.

Algorithm Analysis:1. A number M is a factor of another number n, which means n is divisible by M. which is n%m==0. According to the definition of the ugly number, the ugly number can only be divisible by 2,3,5. That is, if a number is divisible by 2, then we divide by 2 consecutively, divided by 3 if divisible by 3, and divide by 5 consecutively, if divisible by 5. If we can get 1 in the end, that number is the ugly number, otherwise not. The algorithm is intuitive and the code is concise, but the biggest problem is that every integer needs to be evaluated. Even if a number is not an ugly number, we still need to do the remainder and division operations, so the algorithm is not efficient time. 2. Then find a way to calculate the number of ugly. According to the definition of the ugliness number, the ugly number should be the result of another ugly number multiplied by the 2,3,5. So we can create an array, where the numbers are the ugly number of rows, each of which is preceded by 2,3 or 5. The key to this speed record is how to make sure that the number of ugly in the array is well sequenced. Suppose that there are already a number of ugly numbers in the array placed in the array, and the largest number of ugly to do m, we then analyze how to generate the next ugly number. The ugly number must be the result of a previous ugly number multiplied by 2,3 or 5, so we first consider multiplying each ugly number by 2. When multiplied by 2, a number of results that are less than or equal to M are obtained. Because it is generated in order, less than or equal to M is definitely already in the array, we don't need to think about it again; we'll get a number of results greater than m, but we only need the first one that is greater than M, because we want the ugly numbers to be generated in the order of the small to the large, and the other larger results later. The first one we get is multiplied by 2 and the result of greater than M is M2, and again, we multiply each ugly number by 3 and 5, and we get the first results greater than M. M3 and M5. Then the next ugly number should be the smallest of the 3 numbers of m2,m3 and M5. In the previous analysis, we mentioned multiplying each ugly number by 2,3,5. In fact this is not necessary, because the existing ugly numbers are stored sequentially in the array. For times 2, there must be an ugly number of T2, every ugly number before it multiplied by 2 results will be less than the maximum number of ugly, after which every ugly number multiplied by 2 results will be too large. We just need to write down the position of the ugly number and update the T2 each time we generate a new ugly number. For times 3 and 5, there is also the same T3 and T5.
Algorithm 1 source program:
/************************************************************** * Copyright (c) 2016, * All rights reserved. * Version Number: v1.0 * Description: Ugly number * We call the inclusion factor 2,3, and the number 5 as the ugly Number (Ugly).
Find the 1500th ugly number in order from small to large. 
* For example, 6,8 are ugly numbers, but 14 is not because it contains factor 7. In our habit, we take 1 as the first ugly number. * Input Description: Please enter the order of the ugly number: 1500 * Program output: The 1500th ugly number is: 859963392 * Problem Analysis: no * algorithm Description: The so-called one number m is another number n factor, refers to N can be divided by M. which is n%m==0. According to the definition of the number of ugliness, * * The ugliness can only be divisible by 2,3,5.
That is, if a number is divisible by 2, then we divide by 2 consecutively, and if divisible by 3, the * is divided continuously by 3, and if divisible by 5, divided consecutively by 5. If we can get 1 in the end, that's the ugly number, otherwise it's not. * Date of Completion: 2016-09-21 ***************************************************************/Package

Org.marsguo.offerproject34;

Import Java.util.Scanner;
		Class thenumberisugly{public int isuglyfunction (int #) {if (number <= 0) return 0;
		int sum = 0;
		int uglynum = 0;
			while (Uglynum < number) {sum++;
			if (uglynumberfunction (sum)) {uglynum++;
	return sum;
		Private Boolean uglynumberfunction (int number) {//Boolean flag = FALSE; while (number%2 = =0) {number/= 2;//flag = true;
		while (number%3 = = 0) {number/= 3;//flag = true;
		while (number%5 ==0) {number/= 5;//flag = true; return (number = = 1)?
	True:false;
		} public class isugly {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in);
		System.out.println ("Please enter the order of the Ugly Number:");
		int num = Scanner.nextint ();
		SYSTEM.OUT.PRINTLN ("num =" + num);
		Scanner.close ();
		thenumberisugly thenumberisugly = new thenumberisugly (); System.out.println ("first" + num + "an ugly number is:" + thenumberisugly.
	Isuglyfunction (num));
 }
}



algorithm 2 source program:
/************************************************************** * Copyright (c) 2016, * All rights reserved. * Version Number: v1.0 * Description: Ugly number * We call the inclusion factor 2,3, and the number 5 as the ugly Number (Ugly).
Find the 1500th ugly number in order from small to large. 
* For example, 6,8 are ugly numbers, but 14 is not because it contains factor 7. In our habit, we take 1 as the first ugly number. * Input Description: Please enter the order of the ugly number: 1500 * Program output: The 1500th ugly number is: 859963392 * Problem Analysis: no * algorithm Description: The so-called one number m is another number n factor, refers to N can be divided by M. which is n%m==0. According to the definition of the number of ugliness, * * The ugliness can only be divisible by 2,3,5.
That is, if a number is divisible by 2, then we divide by 2 consecutively, and if divisible by 3, the * is divided continuously by 3, and if divisible by 5, divided consecutively by 5. If we can get 1 in the end, that's the ugly number, otherwise it's not. * * See note algorithm description * completion date: 2016-09-21 ***************************************************************/Package

Org.marsguo.offerproject34;

Import Java.util.Scanner;
		Class getuglynumber_method2{public int getulynumberfun (int index) {if (index <= 0) {return 0;
		} int[] Uglynumbers = new Int[index];
		Uglynumbers[0] = 1;
		int nextuglyindex = 1;
		int i2=0;
		int i3=0;
		
		int i5=0; while (Nextuglyindex < index) {for (int i = 1; i < index; i++) {int min = min (UGLYNUMBERS[I2]*2,UGLYNUMBERS[I3]*3,UGLYNUMBERS[I5]*5);
			System.out.println ("min =" + min);				Uglynumbers[i] = min;
			The Min value is entered into the array uglynumbers each time, so that there is a value in the array before the comparison.
			while (Uglynumbers[i2] * 2 <= uglynumbers[i]) i2++;
			while (Uglynumbers[i3] * 3 <= uglynumbers[i]) i3++;
			while (UGLYNUMBERS[I5] * 5 <= uglynumbers[i]) i5++;
		++nextuglyindex;
		int ugly = uglynumbers[index-1];
	return ugly;
		private int min (int number1,int number2,int number3) {int min = (Number1 < number2)? Number1:number2; Min = (min < Number3)?
		Min:number3;
	return min; } public class Usespacechangetime {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in)
		;
		System.out.println ("Please enter the order of the Ugly Number:");
		int num = Scanner.nextint ();
		SYSTEM.OUT.PRINTLN ("num =" + num);
		
		Scanner.close ();
		GETUGLYNUMBER_METHOD2 getugly = new Getuglynumber_method2 (); System.out.println ("first" + num + "an ugly number is:" + getugly.
	Getulynumberfun (num));
 }
}


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.