The problem you are going to solve is the following Compute

The problem you are going to solve is the following: Compute the sum of n! and only output the sums divisible by 7 for the range 0 lessthanorequalto n lessthanorequalto 10^6. For example: n = 1: sum = 1! = 1 n = 2: sum = 1! + 2! = 1 + 2*1 = 3 n = 3: sum = 1! + 2! + 3! = 1 + 3 + 3*2*1 = 10 n = 4: sum = 10 + 4*3*2M = 34 n = 5: sum = 34 + 5*4*3*2*1 = 154 ... You are to provide these implementations: |Python using recursion (e.g., a function that calls itself) Python using a functional approach with the filter function (see the previous lecture). Your solution should contain the following: The source code (keep it simple and strip the comments so the code footprint is small) Proof that the code ran properly and produced the correct output (provide a screenshot showing a timestamp and the output) An analysis of how long the code took to run. A discussion comparing the approaches and explaining which one ran faster and why. Your solutions will be graded based on the simplicity of your code, the correctness of your output and the insightfulness of your analysis.

Solution

# sum of n! and only output the sums of divisible by 7

Number = int(input(\" Please Enter any Number: \"))

result=0

def factorial(number):

if number<=1:   # base case

return 1

else:

return result

#print(factorial(number))

# To check if divisible by 7

def checkdivisible(result):

Analysis:

The factorial of entered number is calculated the checks that factorial answer is divisible by 7.The code wil run upto the calculating factorial of entered number.

result is used for to store result

output

Please Enter any Number: 5

The inputed number is 5, it will be store in varible number

In the factorial function: factorial is calculted by using recursion.and the result id stored in result function.

and it will be pass to the function checkdivisible

In the checkdivisible function: The number is check wheater it is divisible by 7 by using mod operator. The mod operator return reminder. If reminder is zero then number is divisible by 7

 The problem you are going to solve is the following: Compute the sum of n! and only output the sums divisible by 7 for the range 0 lessthanorequalto n lessthan

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site