Project Euler Problem5

Smallest multiple

Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

 

From problem 3, we get a list of prime factors of a number . Base on this, we get the prime factors for number from 1 to 20.

And we merge them together. The rule is, we get the largest times for each prime factor. Then we mutiply them.

 The python code is as follows:

def getPrimeFactorDic(data):
    dic = {}
    i = 2
    while i <= math.sqrt(data):
    #while i <= data/2:
        if data%i == 0 :
            putIntoDic(dic, i)
            data = (int)(data/i)
            #i = 2
        else:
            i += 1
    putIntoDic(dic, data)
    return dic

def putIntoDic(dic, key):
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1


def mergeIntoDic(dic, key, value):
    if key in dic:
        if value > dic[key]:
            dic[key] = value
    else:
        dic[key] = value

print(getPrimeFactorDic(10))
myDic = {}
for i in range(1, 21):
    tempDic = getPrimeFactorDic(i)
    for key in tempDic.keys():
        mergeIntoDic(myDic, key, tempDic[key])
print(myDic)

mathProduct = 1
for key in myDic.keys():
    mathProduct *= pow(key, myDic[key])
print(mathProduct)

  

转载于:https://www.cnblogs.com/tianxiaozz/p/3471606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值