Title: Ugly number
* We call the number that contains only factor 2,3,5 (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.
Method One: Judge each integer is not an ugly number of solutions, intuitive but not efficient:
A number M is a factor of another number n, which means that n can be divisible by M, that is, n%m==0. According to the definition of ugliness, the number of ugliness can only be divisible by 2,3,5. That is, if a number is divisible by 2, we divide it continuously by 2, and if divisible by 3, divide it by 3, and if divisible by 5, divide by 5. If the last thing we get is 1, then this number is the ugly number, otherwise it is not.
Next, we just need to determine in order that each integer is not an ugly number,
Applicable Java code implementation:
/**
* Ugly number
* We call the number that contains only the factor 2,3,5 (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 1 as the first ugly number
* * * *
package swordforoffer;
/**
* @author Jinshuangqi
* *
* August 9, 2015 * * Public
class E34uglynumber {public
Boolean isugly (int number) {While
(number% 2 = 0)
number/=2;
while (number% 3 = 0) number
/=3;
while (number% 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 =;
E34uglynumber test = new E34uglynumber ();
SYSTEM.OUT.PRINTLN (Test.getuglynumber (index));
}
We just need to pass 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 every integer needs to be evaluated. Even if a number is not an ugly number, we still need to make it redundant and division operations. Therefore, the time efficiency of the algorithm is not very high, the interviewer will not be satisfied, but also prompts us to have a more efficient algorithm.
Method Two: Create an array to save the ugly number that has been found, using space to change the time of the solution:
The previous algorithm is inefficient, in large part because we have to calculate whether a number is ugly or not. Next we try to find a way to calculate the number of ugly, not to spend time on an integer that is not an ugly number. 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 number is a good number of ugly, every ugly number is the front of the ugly number multiplied by 2,3,5.
The key to this kind of thinking is how to determine the number of ugly in the array is the sort of good. Assuming that there are already a number of ugly numbers in the array stored in the array, and the largest number of ugly numbers 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 the 2,3,5. So we're going to 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 result that is greater than M, because we want the ugly numbers to be generated in order of small to large, other larger results later. We take the first one multiplied by 2 and the result of greater than M is M2. Again, we multiply each ugly number by 3, 5, and get the first results greater than M M3 and M5. Then the next ugly number should be m2,m3,m5. The smallest of these 3 numbers.
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.
Java Code Implementation:
public int getuglynumber_solution2 (int index) {
if (index <0) return
0;
int[] Uglyarray = new Int[index];
Uglyarray[0] = 1;
int multiply2 = 0;
int multiply3 = 0;
int multiply5 = 0;
for (int i = 1;i<index;i++) {
int min = min (uglyarray[multiply2]*2,uglyarray[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 idea does not need to do any calculation on an integer that is not an 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 number that has been generated, thereby increasing the space consumption. If you are seeking the 1500th ugly number, you will create an array that holds 1500 ugly numbers, which account for memory 6KB. And the first idea has no such memory overhead. In general, the second approach is equivalent to using less space in exchange for increased time efficiency.