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 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 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.
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
If S = 1-x/1! + x^2/2! - x^3/3! + .....   where n! means factorial(n) and x...
If S = 1-x/1! + x^2/2! - x^3/3! + .....   where n! means factorial(n) and x is a variable that will be assigned. Use matlab to compute S for x = 7 and n (number of terms) = 5.   Write the value below as the one displayed when you issue "format short" in matlab. Explain the process and result of the question.
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
Write a function that inputs the, n, the number of random trials for a standard normal...
Write a function that inputs the, n, the number of random trials for a standard normal distribution and then generates an array of n random observations (ie generate a random sample of n observations from a N(0,1) distribution. Then have the function perform a simple t-test on the sample, testing the null hypothesis that is mean 0. Have the function return two things: the results of the ttest and another variable that is set to the string= “reject” if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT