两个数的倍数之和

时间:2015-10-13 19:46:46

标签: python lambda

我试图用python找出两个数字的倍数之和。我已经完成了。我只想用lambda函数解决它。 没有lambda代码

   def sumMultiples(num, limit):

    sum = 0
    for i in xrange(num, limit, num):
        sum += i
    return sum

def sum(limit):
    return (sumMultiples(3, limit) +
            sumMultiples(5, limit) -
            sumMultiples(15, limit))

print sum(1000)

2 个答案:

答案 0 :(得分:1)

试试这段代码:

a = input("enter first number\n")
b= input("enter second number\n")
limit=[]
limit.append(a)
limit.append(b)
natNo=range(1,1000)
xyz = []
for i in limit:
    xyz +=filter(lambda x: x == i or x % i==0, natNo)   
set = {}
map(set.__setitem__, xyz, [])
nums=set.keys()
print "the multiples of the given numbers are: "+str(nums)
c=reduce(lambda x, y:x+y, nums)
print "the sum of the multiples of the given numbers is "+str(c)

答案 1 :(得分:0)

limit = 1000
a=lambda num, limit: sum([i for i in xrange(num, limit, num)])
print a(3,limit)+a(5,limit)-a(15,limit)