In: Computer Science
Write a function in Python 3 (v. 6175+) called
multiplicity00794. The function should receive alimit number and
return:
• how many multiples of 3 or 5 or 7 there are that are less than or
equal to the specified limit.
• the sum of the multiples of 3 or 5 or 7 that are less than or
equal to the specified limit.
• the product of the multiples of 3 or 5 or 7 that are less than or
equal to the specified limit.
For example, if the function is invoked with 15 it should return 9
(because there are 9 multiples of 3, 5 or 7 less than or equal
to
than 15: 3, 5, 6, 7, 9, 10, 12, 14 and 15), 81 (because 3 + 5 + 6 +
7 + 9 + 10 + 12 + 14 + 15 = 81) and 142884000 (because
3 * 5 * 6 * 7 * 9 * 10 * 12 * 14 * 15 = 142884000).
Python code:
#defining multiplicity00794 function
def multiplicity00794(limit):
#initializing count as 0
count=0
#initializing total as 0
total=0
#initializing product as 1
product=1
#looping from 1 to limit
for i in range(1,limit+1):
#checking if number is
divisible by 3 or 5 or 7
if(i%3==0 or i%5==0 or
i%7==0):
#incrementing count
count+=1
#adding current number to total
total+=i
#multiplying current number to product
product*=i
#returning count,sum and product
return(count,total,product)
#calling multiplicity00794 function and printing result
print(multiplicity00794(15))
Screenshot:
Output: