Question

###############recursion (Python) ##def facto( num ): ## if num == 0: ## return 1 ## else:...

###############recursion (Python)
##def facto( num ):
## if num == 0:
## return 1
## else:
## return num * facto( num - 1 )
##
##Task 2. N! = 1*2*3*...*(N-1)*N as we know. There is another "double factorial":
##N!! :
##1)if N is odd then N!! = 1*3*5*7*9*...*N
##2)if N is even then N!! = 2*4*6*8*...*N
##
####Task 2. Create a function facto2 which calculates facto2(N) = N!!
##Show that your function works.
######use input(...)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#############recursion (Python)
def facto(num):
    if num == 0:
        return 1
    else:
        return num * facto(num - 1)


# Task 2. N! = 1*2*3*...*(N-1)*N as we know. There is another "double factorial":
# N!! :
# 1)if N is odd then N!! = 1*3*5*7*9*...*N
# 2)if N is even then N!! = 2*4*6*8*...*N

##Task 2. Create a function facto2 which calculates facto2(N) = N!!
####use input(...)
def facto2(n):
    if n <= 1:
        return 1
    else:
        return n * facto2(n - 2)


print(facto2(10))  # prints 10*8*6*4*2
print(facto2(9))  # prints 9*7*5*3*1
n = int(input("Enter a value for n: "))
print("facto2({}) = {}".format(n, facto2(n)))
Add a comment
Know the answer?
Add Answer to:
###############recursion (Python) ##def facto( num ): ## if num == 0: ## return 1 ## else:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in python 3 1. A program contains the following function definition: def cube(num): return num *...

    in python 3 1. A program contains the following function definition: def cube(num): return num * num * num Write a statement that passes the value 4 to this function and assign its return value to the variable result. 2. Write a function named get_first_name that asks the user to enter his or her first name and returns it.

  • def ged(m,n) : if m n == 0: return n else: return god (n, møn) Without...

    def ged(m,n) : if m n == 0: return n else: return god (n, møn) Without typing the code into Python, what are the values of the input arguments of the second recursive call if the function gcd is called from the main program as gcd(537,44)? (Hint: do not count the initial call to the function from the main program) Om = 44, n = 9 Om = 44, n = n = 537 Om = 9, n = 8...

  • I'm trying to run the code in Python below and keep getting invalid syntax. What's wrong?...

    I'm trying to run the code in Python below and keep getting invalid syntax. What's wrong? Thanks for any help! def even(n): if n%2==0 #Enter a Number def main(): n=int(input("Enter a number: ")) #If even print "Number is even" if(even(n)): print("The number is even") #If odd print "Number is odd" else: print("The number is odd") main() #Call the main function

  • Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write...

    Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write a code for function main, that does the following: -creates a variable and assigns it the value True. - uses a while loop which runs as long as the variable of the previous step is True, to get a number from the user and if passing that number to the function of Q3 results in a True value returned, then add that number to...

  • Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write...

    Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write a code for function main, that does the following: -creates a variable and assigns it the value True. - uses a while loop which runs as long as the variable of the previous step is True, to get a number from the user and if passing that number to the function of Q3 results in a True value returned, then add that number to...

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [...

    convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [ ] if len(nums) == 1: return nums [0] # first find freq freq dict d = {} for num in nums: if num in d: d[num] -= 1 # reverse the sign on the freq for the heap's sake else: d[num] = -1 h = [] from heapq import heappush, heappop for key in di heappush(h, (d[key], key)) res = [] count = 0...

  • convert this python code to c++ code def sort(a,b): if a > b: return b,a else:...

    convert this python code to c++ code def sort(a,b): if a > b: return b,a else: return a,b def main(): set1 = input('Enter the first pair of integers: ') set2 = input('Enter the second pair of integers: ') set3 = input('Enter the third pair of integers: ') set1 = set1.split(',') set1_0 = int(set1[0]) set1_1 = int(set1[1]) set2 = set2.split(',') set2_0 = int(set2[0]) set2_1 = int(set2[1]) set3 = set3.split(',') set3_0 = int(set3[0]) set3_1 = int(set3[1])    print(sort(set1_0,set1_1)) print(sort(set2_0,set2_1)) print(sort(set3_0,set3_1))

  • Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return...

    Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return n*factorial(n-1) {output is n!} Trace the above algorithm when it is given n = 7 as input. That is, show all steps used by above algorithm to find 7!

  • def verifyMagic( matrix, num): "!" Given a list of lists called matrix, recursively verify that each...

    def verifyMagic( matrix, num): "!" Given a list of lists called matrix, recursively verify that each list sums to num. Return True if that's the case, otherwise, return false. If the list is empty, return None. Assume that the correct input types were given." # Base case 1: a matrix with no elements if return # Base case 2: a matrix with one element if # Check if the sum of the only element in the list is num. #...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT