zoukankan      html  css  js  c++  java
  • 欧拉计划 第三题

    原题

    Largest prime factor

    Problem 3

    The prime factors of 13195 are 5, 7, 13 and 29.
    What is the largest prime factor of the number 600851475143 ?

    分析

    直接用筛素数法找number的因数,最大的因数就是要求的结果

    代码

    #coding=utf-8
    def getList(n):
         result = [] #定义空列表
         count = 3 #从3开始遍历,因为2单独判断
         max = n #max为初始数值
         while max%2 == 0: #以2作判断,如果能被2整除,则2加入列表,max除以2——把里面的2全部除完
             max /= 2
             result.append(2) # 表示有2这个素因数
    
         while count <= max: #以max大于count开始循环
            # 类似筛素数的方法,count一定是素数
             if max%count == 0: #如果max能被count整除,把count加入列表,max除以count,跟上面对2的判断类似 —— 并且一旦能整出,则把count除尽
                 result.append(count)
                 max /= count
             else:
                 count += 2 #因为2已经除完,所以排除偶数
         return result
    
    print getList(102) #得到所有素因数
    
    转载请保留原文链接及作者
    本文标题:
    文章作者: LepeCoder
    发布时间:
    原始链接:
  • 相关阅读:
    python中filter(),map()和reduce()的用法及区别
    Python中的单例模式的几种实现方式的及优化
    python标准库和第三方库的区别
    django和flask的区别
    wtforms
    protobuf学习
    人物FSM
    策略模式
    虚函数调用机制
    虚析构函数
  • 原文地址:https://www.cnblogs.com/lepeCoder/p/Euler_3.html
Copyright © 2011-2022 走看看