Algorithm analysis---Looking for ugly number

Source: Internet
Author: User

What is ugly number:

The factor of a number consists only of the number of 2,3,5 called the ugly number. The number 1 is particularly treated as an ugly number, so 10 ugly numbers starting from 1 are 1, 2, respectively. 3. 4,5,6,8,9. 10. 12.


the concept of a factor:

If the integer m is divided by N and the quotient of the net number is obtained, then N is a factor of M. The factors such as 8 are 1, 2, 4, 8. The number of ugly factors required is only 2, 3, 5. So the factor in the ugly number should be understood as the mass factor.

That is, the factor is prime. Prime numbers are also called primes, which refer to a natural number greater than 1. The number that cannot be divisible by other natural numbers except 1 and itself.

The number corresponding to prime numbers is called composite.

It is now required to write a program that outputs the nth number of ugly numbers beginning with 1.

If the first ugly number is 1, the second ugly number is 2. The tenth ugly number is 12.


An algorithm that infers whether it is an ugly number :

Set to infer that the integer m,m loop is divided by 2 until it cannot be divisible. The loop is then divided by 3 until it is not divisible. The loop is then divided by 5 until the quotient is 1 or not divisible.

The quotient is 1 and the remainder is 0 is the ugly number, otherwise the non-ugly number.

such as: Ugly number 12

12/2 = 6

6/2 = 3;

3/2 not divisible

3/3 = 1; End, 12 is the number of ugly


Non-ugly number 26

26/2 = 13

13/2 not divisible

13/3 not divisible

13/5 not divisible

End, 26 is not an integer


looking for the ugly number algorithm 1:

(1) Set a counter to count the number of ugly numbers that appear

(2) Starting from 1 to traverse each integer, to infer whether it is ugly number, assuming that the number of ugly counter plus 1. Otherwise, the next integer is traversed.

(3) When the value of the counter =n, stop traversing. The number of ugly outputs.

#include <stdio.h> #include <time.h> #include <string.h>int isugly (int number) {//infer if numbers are ugly while (number%2==0) {//Infer whether the number can be divisible by 2 number=number/2;} while (number%3==0) {//inferred number can be divisible by 3 NUMBER=NUMBER/3;} while (number%5==0) {//inferred number can be divisible by 5 number=number/5;} if (number = = 1)//return 1;elsereturn 0;} int findugly (int N) {//search for Nth  ugly number starting from 1 int count=0;//for counting int number=1;//starting from 1 traversing while (1) {if (isugly (number)) {  // Suppose number is an ugly counter plus 1count++;} if (count = = N)  //Find the nth ugly number, return the number of ugly returns number;elsenumber++;}} void Main () {int n=0;scanf ("%d", &n), clock_t start = Clock ();p rintf ("%d\n", findugly (N)), clock_t stop = clock (); printf ("Time-consuming:%lf\n", (double) (Stop-start)/clocks_per_sec);}

The above algorithm starts from 1 to look for the nth number of ugly, and when n is very large it takes a lot of time. Consumes 23 seconds when N is 1400. With the increase of N. Time consuming is rather serious



looking for the ugly number algorithm 2:

Think of a way to judge the next ugly number from the last ugly number, and not need to start from 1 to iterate and judge. the 10 ugly numbers starting from 1 were 1, 2. 3,4,5,6,8. 9. 10. 12.

Can be found in addition to 1. The number of ugly is from some ugly number * * or * * or to get the.

If 2 is ugly number 1*2 get, 3 is ugly number 1*3 get. 4 is the ugly number 1*4 get. 5 is the ugly number 1*5 get. 6 is the ugly number 2*3 get ...

Detailed algorithm steps:

(1) Starting from the first ugly number 1, find out 1*2=2, 1*3=3, 1*5 = 5.

(2) Take the minimum value of the product above 1 is greater than 2, as the second ugly number (the ugly number is an ascending sequence. So the first i+1 ugly number must be more than the first ugly number)

(3) The number of ugly number 2 before the ugliness and 2, 3, 5 product:1*2=2, 1*3=3, 1*5 = 5; 2*2 = 4; 2*3 = 6. 2*5 = 10.

(4) Take the minimum value above 2 in the product above 3, as a third ugly number

......

......

(i) The number of ugly numbers before the ugly number I was taken is the product of 2, 3 and 5 respectively.

(i+1) A minimum value greater than I in the product as an ugly number

(i+2) Repeat (i) (i+1) step until counter equals n

#include <stdio.h> #include <time.h> #include <string.h> #define MaxLen 99999//to find the minimum value of 3 numbers int compare (int chentwo,int chenthree,int chenfive) {if (Chentwo <=chenthree) {if (chentwo <= chenfive) return Chentwo;elsereturn chenfive;} else if (chenthree <= chenfive) return Chenthree;elsereturn chenfive;} Find the nth ugly int findugly (int n) {int ugly[maxlen]={1};//To save an array of ugly numbers, put the number of ugly 1 in an array of int count=1;//The array has only the ugly number 1, so the counter is 1while (1) {int Chentwo = 0;int Chenthree = 0;int chenfive = 0;/*ugly The most recent ugly number in the array is ugly[count-1],ugly[count-1] before the ugly number multiplied by 2 to find the first product greater than ugly[ The value of Count-1] is saved in chentwo */for (int i = 0; i < count; i++) {if (ugly[i]*2 >ugly[count-1]) {chentwo = Ugly[i]*2;break;}} /*ugly the last ugly number in an array of ugly[count-1],ugly[count-1] is multiplied by 3, finding the value of the first product greater than ugly[count-1] saved in Chenthree */for (i = 0; i < Count; i++) {if (ugly[i]*3 >ugly[count-1]) {chenthree = Ugly[i]*3;break;}} /*ugly the last ugly number in an array of ugly[count-1],ugly[count-1] is multiplied by 5, finding the value of the first product greater than ugly[count-1] saved in chenfive */for (i = 0; I < count ; i++) {if (ugly[i]*5 >ugly[Count-1]) {chenfive = Ugly[i]*5;break;}} Chentwo,chenthree. The minimum value of chenfive is the new ugly number, which is deposited in the ugly array ugly[count]=compare (Chentwo, Chenthree, chenfive); Count++;if (count==n)//nth Ugly number return UGLY[COUNT-1];}} void Main () {int n=0;scanf ("%d", &n), clock_t start = Clock ();p rintf ("%d\n", findugly (N)), clock_t stop = clock (); printf ("Time-consuming:%lf\n", (double) (Stop-start)/clocks_per_sec);}

When you enter n=1400, it takes less than 0.1 seconds. It can be seen that the speed of algorithm 2 is not comparable to the algorithm 1, which is the result of using space to exchange efficiency.




Algorithm analysis---Looking for ugly number

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.