Example of the solution least common multiple algorithm implemented by Python

Source: Internet
Author: User
This article mainly introduces the implementation of Python least common multiple algorithm, involving Python numerical operations, judgments and other related operational skills, the need for friends can refer to the following

In this paper, the solution least common multiple algorithm for Python implementation is described. Share to everyone for your reference, as follows:

Simple analysis, the previous introduction of the greatest common divisor solution method is similar to the least common multiple solution method, only need to change a simple condition, and then do a little more simple calculation. The solution of the problem is also based on the decomposition of the quality of the procedure.

The program implementation and the test case code are as follows:

#!/usr/bin/pythonfrom Collections Import counterdef primenum (num): R_value =[] for I in range (2,num+1): for J in Range (2,i): If I% J = 0:break Else:r_value.append (i) return r_valuedef Primefactorso Lve (num,prime_list): for n in prime_list:if num% n = = 0:return [n,num/n]def primepisor (num): Num_tem P =num prime_range= primenum (num) ret_value =[] While Num does in prime_range:factor_list= primefactorsolve (num , Prime_range) Ret_value.append (factor_list[0]) num =factor_list[1] else:ret_value.append (num) return C Ounter (Ret_value) def leastcommonmultiple (num1,num2): Dict1 =primepisor (NUM1) dict2 =primepisor (num2) least_common_mu Ltiple= 1 for key in Dict1:if key in Dict2:if Dict1[key] > Dict2[key]: LEAST_COMMON_MULTIPL e*= (Key * * Dict1[key]) else:least_common_multiple*= (Key * * Dict2[key]) for key in Dict1:if K        EY not in Dict2: least_common_multiple*= (Key * * Dict1[key]) for key in Dict2:if key not in dict1:least_common_multiple*= (Key * * Dict2[key]) return Least_common_multipleprint (Leastcommonmultiple (12,18)) print (Leastcommonmultiple (7,2)) Print (Leastcommonmultiple (7,13)) print (Leastcommonmultiple (24,56)) print (Leastcommonmultiple (63,81))

Program execution Results:

E:\WorkSpace\01_ programming Language \03_python\math>pythonleast_common_multiple.py
36
14
91
168
567

Through verification, the calculation results are accurate.

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.