Example of a mass factorization algorithm implemented by Python

Source: Internet
Author: User
This article mainly describes the Python implementation of the quality factorization algorithm, involving Python mathematical operations related operation skills, the need for friends can refer to the following

In this paper, we describe the quality factorization algorithm implemented by Python. Share to everyone for your reference, as follows:

Originally wanted to implement an other basic mathematical algorithm problem, but found in the implementation must first complete the decomposition of the mass-based algorithm.

Did not go online to find what is better than the solution algorithm, purely according to their own understanding of writing a tentative solution algorithm. This is the Guess-try method that Professor mentions in the MIT Open class. The code is implemented as follows:

#!/usr/bin/pythondef primenum (num):   r_value =[] for   i InRange (2,num+1): For      Jin Range (2,i):         ifi% J = = 0 :            break      Else:         r_value.append (i)   returnr_valuedef primefactorsolve (num,prime_list): For   N Inprime_list:      ifnum% n = = 0:         return[n,num/n]def primepisor (num):   prime_range= primenum (num)   ret_ Vale =[] and   numnot in Prime_range:      factor_list= primefactorsolve (num,prime_range)      ret_vale.append (Factor_list[0])      Num =factor_list[1]   else:      ret_vale.append (num)   printret_valeprimepisor (120)

The results of the program execution are as follows:

E:\WorkSpace\01_ programming Language \03_python\python_exp07>pythonpython_exp07.py
[2, 2, 2, 3, 5]

The extraction tests the other data, from the simple example calculation, the result is 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.