Title: Ugly number
* We refer to the number of 2,3,5 that contain only the factor (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 a factor of 7. We used to treat 1 as the first ugly number.
Method One: To judge each integer is not the number of ugly solution, intuitive but not efficient:
The so-called one number m is another number n factor, refers to n can be divisible by M, that is n%m==0. According to the definition of ugly number, the number of ugly can only be 2,3,5 divisible. That is, if a number is divisible by 2, we divide it by 2, and if it is divisible by 3, it is divided by 3, and if it is divisible by 5, it is divided by 5. If the last thing we get is 1, then this number is the ugly number, otherwise it's not.
Next, we just need to determine in sequence whether each integer is an ugly number,
For Java code implementations:
/** * Number of ugly * We call the number of 2,3,5 that contains only the factor (Ugly number). * 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. We used to treat 1 as the first ugly number */package swordforoffer;/** * @author Jinshuangqi * * August 9, 2015 */public class E 34UglyNumber {public Boolean isugly (int. number) {while (number% 2 = = 0) number/=2;while (number% 3 = = 0) number/=3;while (num ber% 5 = = 0) Number/=5;return (number ==1)? True:false;} public int getuglynumber (int index) {if (index <= 0) return 0;int number = 0;int Uglyfound = 0;while (Uglyfound < index) {number++;if (isugly (number)) {++uglyfound;}} return number;} public static void Main (string[] args) {int index = 150; E34uglynumber test = new E34uglynumber (); SYSTEM.OUT.PRINTLN (Test.getuglynumber (index));}}
We only need to pass the parameter 1500 in the function Getuglynumber to get the 1500th ugly number. The algorithm is very intuitive and the code is very brief, but the biggest problem is that each integer needs to be computed. Even if a number is not an ugly number, we still need to do the redundancy and division operation. Therefore, the time efficiency of the algorithm is not very high, the interviewer will not be satisfied, but also suggest that we have more efficient algorithm.
Method Two: Create an array to save the number of ugly numbers that have been found, using the space-time solution:
The previous algorithm is inefficient, largely because we have to calculate whether a number is an ugly number or not. Next we try to find a way to calculate the number of ugly numbers instead of spending time on the whole number of non-ugly numbers. According to the definition of the ugly number, the ugly number should be the result of another ugly number multiplied by 2,3,5. So we can create an array in which the numbers are sorted by the number of ugly, each ugly number is preceded by the number of ugly multiplied by 2,3,5.
The key to this approach is how to make sure that the number of ugly numbers inside the array is sorted. Assuming that there are already a number of ugly numbers in the array that are placed in the array, and that the largest number of ugly numbers that are already available are recorded as 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,5. So we're going to start by multiplying the number of ugly numbers we've got by 2. When multiplied by 2, you get several results that are less than or equal to M. Because it is generated sequentially, less than or equal to M must already be in the array, we do not need to consider again, we will also get a number of results greater than m, but we only need the first result greater than M, because we hope that the number of ugly is generated in order from small to large, other larger results later. The result that we get the first one multiplied by 2 is M2. Similarly, we multiply each ugly number we have by 3, 5, to get the result of the first greater than M M3 and M5. Then the next ugly number should be m2,m3,m5. The smallest of those 3 numbers.
In the previous analysis, it was mentioned that each of the existing ugly numbers was multiplied by 2,3,5. In fact, this is not necessary, because the existing number of ugly is stored in the array in order. For times 2, there must be an ugly number T2, and every ugly number that precedes it is multiplied by 2, resulting in a result that is less than the maximum number of ugly numbers already present, and the result will be too large for every ugly number multiplied by 2 after it. We just need to write down the location of this ugly number and update the T2 every time we generate new ugly numbers. For times 3 and 5, the same T3 and T5 are present.
Java Code Implementation:
public int getuglynumber_solution2 (int index) {if (index <0) return 0;int[] Uglyarray = new Int[index];uglyarray[0] = 1;i NT Multiply2 = 0;int Multiply3 = 0;int multiply5 = 0;for (int i = 1;i<index;i++) {int min = min (uglyarray[multiply2]*2,ug LYARRAY[MULTIPLY3]*3,UGLYARRAY[MULTIPLY5]*5) Uglyarray[i] = min;while (uglyarray[multiply2]*2 = = UglyArray[i]) Multiply2++;while (uglyarray[multiply3]*3 = = Uglyarray[i]) multiply3++;while (uglyarray[multiply5]*5 = = UglyArray[i]) multiply5++;} return uglyarray[index-1];} public int min (int number1,int number2,int number3) {int min = (number1<number2)? Number1:number2;return min < Number3?min:number3;}
Compared with the first idea, the second way of thinking does not need to do any calculation on the integer of the non-ugly number, so the time efficiency is obviously increased. However, it is also necessary to point out that the second algorithm needs an array because it needs to save the ugly numbers that have already been generated, thus increasing the space consumption. If you are asking for the 1500th ugly number, you will create an array that holds 1500 ugly numbers, which accounts for 6KB of memory. The first way of thinking does not have such memory overhead. In general, the second way of thinking is equivalent to using less space in exchange for time efficiency improvements.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The sword refers to the offer surface question (Java edition): Ugly number