Question

In: Computer Science

(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n...

(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n is an int >= 0).

(2) Write a Lisp function factorial2 to computee factorial number of n (where n is an int >=0) recursivaly with a global variable (e.g., a list, an array, or a hash tble) to save the result to be used later (memorization) to compute next factorial number. Run the program with test case.

For your test run, the following cases: factorial of 0, 1, 10, 100, 1000, 10000, 100000.

3) Write a Lisp function factorial3 to computee factorial of n (where n is an int >= 0) with a loop (iteratively) with a global variable (e.g., a list, an array, or a hash table, etc.) to save the previous result to be used later. You may use a global variable or hash table or an array to save the results to be used later (memorization) to compute the next factorial number.

Solutions

Expert Solution

Question 1:

;(1) Write a simple Lisp function factorial1 to computee factorial number of n
;recursivaly (where n is an int >= 0).

; defining function factorial1
; parameter: n - integer greater than 0 for
;            for which the factorial should be calculated
; description: calculates the factorial for integer n

(defun factorial1 (n)
  
    ; case for negative answer
   (if (< n 0)
        (return-from factorial1 0)
    )
  
    ;condition for n equal to 0
   (if (= n 0)
        (return-from factorial1 1) ; executed when n equals to 0
        (return-from factorial1 (* n (factorial1 (decf n 1)))) ; executed when n is not equal to 0
                                                               ; i.e. n maybe greater than or lesser to 0
   )
)

(write (factorial1 5))

Output:
120

Question 2:

; (2) Write a Lisp function factorial2 to computee
; factorial number of n (where n is an int >=0) recursivaly
; with a global variable (e.g., a list, an array, or a hash tble)
; to save the result to be used later (memorization) to compute next
; factorial number. Run the program with test case.

; Hash table for storing the factorials
(setq memory (make-hash-table))

; defining function factorial2
; parameter: n - integer greater than 0 for
;            for which the factorial should be calculated
; description: calculates the factorial for integer n

(defun factorial2 (n)

    ; when n is a negative number
    ; return 0
    (if (< n 0)
        (return-from factorial2 0)
    )

    ; when n is equal to 0
    ; return 1
   (if (= n 0)
        (return-from factorial2 1)
   )
  
   ; searching the hash table for the factorial of n
   ; if it is already calculated it will return the factorial
   ; else it will return NIL
    (if (gethash n memory)
        (return-from factorial2 (gethash n memory))
   )
  
   ; recursively calculating the factorial for n
   ; and storing in variable result
   (setq result (* n (factorial2 (- n 1))))
  
    (write "without memory")
    (format t "~C~C" #\return #\linefeed)
  
    ; storing the factorial in Hash map
    (setf (gethash n memory) result)
  
   (return-from factorial2 result)
  
)

(write (factorial2 3))
(format t "~C~C" #\return #\linefeed)
(format t "~C~C" #\return #\linefeed)
(write (factorial2 2))

Output:
"without memory"
"without memory"
"without memory"
6

2

Question 3:

; 3) Write a Lisp function factorial3 to computee
; factorial of n (where n is an int >= 0) with a loop (iteratively)
; with a global variable (e.g., a list, an array, or a hash table, etc.)
; to save the previous result to be used later. You may use a global variable or
; hash table or an array to save the results to be used later (memorization)
; to compute the next factorial number.

; Hash map for storing the factorials
(setq memory (make-hash-table))

; defining function factorial1
; parameter: n - integer greater than 0 for
;            for which the factorial should be calculated
; description: calculates the factorial for integer n

(defun factorial3 (n)

    ; if n is a negative number
    ; return 0
    (if (< n 0)
        (return-from factorial3 0)
    )
  
    ; if n is equal to 0
    ; return 1
    (if (= n 0)
        (return-from factorial3 1)
   )
  
   ; if n is equal to 1
    ; return 1
   (if (= n 1)
        (return-from factorial3 1)
   )
  
   ; declaring a variable result = 0
   (defvar result 1)
  
   ; Loop to calculate factorial
    (loop
        ; going out of the loop when n = 1
        (when (= n 1) (return result))
      
        ; checking if the factorial for n is already calculated
        ; if it is already calculated multiplying the result with the factorial
        ; and going out of the loop
        (if (gethash n memory)
            (return (* result (gethash n memory)))
        )
      
        ; multiplying for factorial
        (setq result (* result n))
      
        ;decrementing n by 1
        (decf n 1)
  
        (write "without memory")
        (format t "~C~C" #\return #\linefeed)
  
        ; saving the factorial value into hash map
        (setf (gethash (+ n 1) memory) result)
    )
  
    (return-from factorial3 result)
  
)

(write (factorial3 5))
(format t "~C~C" #\return #\linefeed)
(format t "~C~C" #\return #\linefeed)
(write (factorial3 6))


Output:

"without memory"
"without memory"
"without memory"
"without memory"
120

"without memory"
720


Related Solutions

Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
Write a function in lisp XIT that counts the number of items in each sub-list of...
Write a function in lisp XIT that counts the number of items in each sub-list of a list and returns the count in a list. It should only work if there are two levels of brackets in the parameter (simple lists inside the parent list). Thus:             (XIT '((A B C)))        -> (3)             (XIT '((A) (A B) (A B C))) -> (1 2 3)             (XIT '((1 2)))              -> (2)             (XIT '(1 (2 3)))...
Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
Let G be a simple undirected graph with n vertices where n is an even number....
Let G be a simple undirected graph with n vertices where n is an even number. Prove that G contains a triangle if it has at least (n^2 / 4) + 1 edges using mathematical induction.
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT