首页 > 编程语言 > 详细

[算法]分解因数

时间:2017-09-16 23:54:14      阅读:259      评论:0      收藏:0      [点我收藏+]

将101到200间的数分解因数

log = {}

def prime_log(num):
    if num < 3:
        return 0
    flags = [True] * num 
    flags[0], flags[1] = False, False
    for i in range(2, int(num**0.5+1)):
        if flags[i]:
            flags[i*i:num:i] = [False]*len(flags[i*i:num:i])
    return flags

primes = prime_log(201)
# print primes

def parse_int(num):
    if num in log:
        return log[num]
    if primes[num]:
        log[num] = [num]
        return [num]
    res = []
    for i in range(2, num/2+1):
        if not num%i:
            if primes[i]:
                res.append(i)
                e = log[num/i] if num/i in log and len(log[num/i]) else parse_int(num/i)
                for j in e:
                    res.append(j)
                break
    for i in res:
        if num in log:
            log[num].append(i)
        else:
            log[num] = []
            log[num].append(i)
    return res



def main():
    for i in range(101, 200):
        parse_int(i)
        if not len(log[i]):
            print str(i)+={}\n.format(i)
        else:
            print *.join(map(str, log[i]))+={}\n.format(i)

if __name__ == __main__:
    main()

# print parse_int(108)
# # print log
            



                

 

[算法]分解因数

原文:http://www.cnblogs.com/fcyworld/p/7533474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!