Python Programming Algorithm Practice _001

Source: Internet
Author: User

statement: The title is from the "point of the sword". Algorithm principle please find your own book, not nonsense. Directly on the Python version code.

Title: The 1500th Ugly number in order from small to large. We call the numbers that contain only the factors 2, 3, and 5 as ugly (Ugly number)

1 #Calculate the 1500th number of ugly2 #1,2,3,4,53 Importdatetime4 5StartTime =Datetime.datetime.now ()6 7Uglynumberlist = [1]8Indexlist = [[0,2], [0,3], [0,5]]9 Ten  whileLen (uglynumberlist) <= 1500: Onevar = min (uglynumberlist[indexlist[0][0]]*2, uglynumberlist[indexlist[1][0]]*3, uglynumberlist[indexlist[2][0]]*5) A      forIinchRange (0, 3): -         ifUGLYNUMBERLIST[INDEXLIST[I][0]]*INDEXLIST[I][1] = =var: -Indexlist[i][0] + = 1 the     Else: - Uglynumberlist.append (Var) - Else:    -Deltatime = Datetime.datetime.now ()-StartTime +     Print("The ugly number is {}\ntime consuming is {} MS". Format (Uglynumberlist[-1], deltatime.total_seconds () *1000)) 

Python Programming Algorithm Practice _001

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.