教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python怎么求因数

python怎么求因数

发布时间:2021-04-30   编辑:jiaochengji.com
教程集为您提供python怎么求因数等资源,欢迎您收藏本站,我们将为您提供最新的python怎么求因数资源

要想做到python语言求因数方法,首先要明白其中的原理:

1、对由123456789这九个数字组成的9位数进行分解质因数。

2、123457698=2x3x3x7x13x23x29x113,所以他的最大值因数是113。

3、总共有362880种可能,从中找出最大值因数中最小的数字和最大值因数中最大的数。

相关推荐:《Python入门教程》

好了,下面来看看python语言求因数方法的实现源码:

#coding:utf-8
 
import math
 
def generator(count, s):
    if count == 1:
        for i in s:
            yield i
    else:
        for i in s:
            _ = set(s)
            _.remove(i)
            for _ in generator(count-1, _):
                yield _ * 10   i
 
primes = [2, 3]
def prime(idx):
    if idx < len(primes):
        return primes[idx]
    new = primes[-1] 2
    while True:
        for i in primes:
            if new % i == 0:
                break
        else:
            primes.append(new)
            break
        new  = 2
    return prime(idx)
 
def probe(number, idx, value=0):
    if value > number:
        return value
    p = prime(idx)
    sqrt = math.sqrt(number)
    while number % p != 0 and sqrt >= p:
        idx  = 1
        p = prime(idx)
    if sqrt < p:
        return number
    return probe(number/p, idx, max(p, value)) 
if __name__ == '__main__':
    _min = 10000000000, 10000000000
    _max = 0, 0
    for number in generator(9, set(range(1, 10))):
        maxfactor = probe(number, 0)
        if maxfactor < _min[0]:
            _min = maxfactor, [number]
        elif maxfactor == _min[0]:
            _min[1].append(number)
        if maxfactor > _max[0]:
            _max = maxfactor, [number]
        elif maxfactor == _max[0]:
            _max[1].append(number)
    print _min
    print _max

您可能感兴趣的文章:
怎么使用python进行整除取余求幂
Python怎么算平方
python怎么求最大公约数和最小公倍数
怎么用python进行加法运算
python输出怎么保留两位小数
为什么大数据用python
python输入错误怎么删除
选Python好还是Java好 ?
一文了解Python虚拟环境
用python怎么判断字符串是纯英文?

[关闭]
~ ~