[Algorithm 01] Looking for ugly numbers

Source: Internet
Author: User

question: Let's include only factors 2 , 3 the numbers of and 5 are called ugly numbers ( ugly number ). For example, 6 and 8 are ugly numbers, 14 NO, because it contains factors 7 . Traditionally, we regard 1 as the first ugly number. Calculate the 1500 ugly number in the ascending order.

analysis: the simplest way to find whether a number meets a certain number (prime number, Narcissus number) is traversal, you can write any ugly number as 2 ^ m * 3 ^ N * 5 ^ P , therefore, an ugly number only contains 2 , 3 , 5 factor, this means the number Number % 2 = 0 ; Number % 3 = 0 ; Number % 5 = 0 , if a number can be 2 divisible, we divide it by 2 ; can be divisible by 3 , we divide it by 3 ; 5 division, we divide it by 5 ; if 1 is obtained, the number is a prime number, otherwise, it is an ugly number.

CodeAs follows:

 1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 // Determines whether a given number is an ugly number.
6 Bool Isuugly (Int Number)
7 {
8 While (Number % 2 = 0 )
9 {
10 Number/= 2 ;
11 }
12 While (Number %3 = 0 )
13 {
14 Number/= 3 ;
15 }
16 While (Number % 5 = 0 )
17 {
18 Number/=5 ;
19 }
20 Return (Number = 1 )? True : False ;
21 }
22
23 // Returns the index ugly number starting from 1.
24 Int Getuglynumber ( Int Index)
25 {
26 If (Index <= 0 )
27 {
28 Return 0 ;
29 }
30
31 Int Number = 0 ;
32 Int Count = 0 ;
33 While (Count <index)
34 {
35 ++ Number;
36 If (Isuugly (number ))
37 {
38 ++ Count;
39 }
40
41 }
42
43 Return Number;
44 }
45
46 Int Main ()
47 {
48 Cout < " Enter a number: " <Endl;
49 Int Idx = 0 ;
50 Cin> idx;
51 Cout <getuglynumber (idx) <Endl;
52 Return 0 ;
53 }

The main disadvantage in the above calculation is that traversal is performed one by one. This will result in a lot of time waste for judging the numbers that are not ugly, this can be avoided by calculating the next ugly number, and the efficiency from ugly to ugly is achieved.AlgorithmAccording to the definition, we can see that the ugly number is multiplied by the known ugly number2,3,5.

Suppose there are already several ugly numbers in an array, and these ugly numbers are arranged in order. Max , The next ugly number must be the first ugly number multiplied 2 , 3 , 5 . Multiply 2 In this case, we multiply each number in the array 2 Because the original array is ordered 2 And then it increases sequentially, so there must be a number. M2 , Each of the preceding values is less than or equal Max , Including M2 The number after the inner is greater Max Because we still need to keep the ascending order, so we take the first one greater Max Number M2 . Likewise, multiply 3 The first one can be greater Max Number M3 , Multiply 5 The first one can be greater Max Number M5 .

The next ugly number: Min {M2, M3, M5}You can.

The Code is as follows:

 1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 // Returns the smallest of the three numbers.
6 Int Min ( Int Number1, Int Number2, Int Number3)
7 {
8 Int Min = (number1 <number2 )? Number1: number2;
9 Min = (Min <Number3 )? Min: Number3;
10 Return Min;
11 }
12
13 // Returns the ugly Number of the index.
14 Int Getuglynumber ( Int Index)
15 {
16 If (Index <= 0 )
17 {
18 Return 0 ;
19 }
20
21 Int * Puglynumbers = New Int [Index];
22 Puglynumbers [ 0 ] = 1 ;
23 Int Nextuglyindex = 1 ;
24
25 Int * Pmultiply2 = puglynumbers;
26 Int * Pmultiply3 = puglynumbers;
27 Int * Pmultiply5 = puglynumbers;
28
29 While (Nextuglyindex <index)
30 {
31 Int Min = min (* pmultiply2 * 2 , * Pmultiply3 * 3 , * Pmultiply5 * 5 );
32 Puglynumbers [nextuglyindex] = min;
33
34 While (* Pmultiply2 * 2 <= Puglynumbers [nextuglyindex])
35 {
36 ++ Pmultiply2;
37 }
38 While (* Pmultiply3 * 3 <= Puglynumbers [nextuglyindex])
39 {
40 ++ Pmultiply3;
41 }
42 While (* Pmultiply5 * 5 <= Puglynumbers [nextuglyindex])
43 {
44 ++ Pmultiply5;
45 }
46
47 ++ Nextuglyindex;
48 }
49
50 Int Uugly = puglynumbers [nextuglyindex- 1 ];
51 Delete [] puglynumbers;
52 Return Uugly;
53
54 }
55
56 Int Main ()
57 {
58 Cout < " Enter a number: " <Endl;
59 Int Number = 0 ;
60 Cin> number;
61 Cout <getuglynumber (number) <Endl;
62 Return 0 ;
63 }

The second method does not need to spend time on non-ugly integers, so the time complexity is much smaller.Vc6 + win7On the platform,Indexed = 1500Time, Method1The running time of is40 s, Method2The time is1 s; Method2The memory needs to be dynamically allocated to occupy space.2There is no such memory overhead. To put it bluntly, the second method is to use space for time.

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.