In: Computer Science
imporant note (the language of compiler and programming llanguage)
Q1:
A positive integer number n is said to be perfect if the number is equal the sum of its divisors excluding the number itself.
Ex:
6 is perfect since the divisors are 1, 2, 3 & 1+2+3 6
28 is perfect since the divisors are 1, 2, 4, 7, 14 & 1+2+4+7+14 = 28
The function “mod” is defined in Xlisp, but the function “div” is not.
>(mod 18 7)
> 4
(a) Write a function “div” which when given two integers n, m and returns n div m.
Ex: > (div 18 7)
> 2
(b) Write a function “perfect” which receives a positive integer n and returns 1 if n is perfect and 0 otherwise.
That is:
>(perfect 28)
>1
(perfect 14)
>0
Hint: I believe you may need to define other functions in addition to div.
Q2 )The McLaurin series for ex as follows:
Write a function “EeX” which receives a number x and returns the value ex.
That is:
> (EeX 1)
> 2.71
Note: you need to define two functions:
That is,
> ( power 3 2)
>9
That is,
> ( factorial 5)
>120
Note: Stop the recursion when (xn/n!) < 0.001
(comp439)
ANSWER:
I have provided the properly
commented and indented code so you can easily copy the
code as well as check for correct indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE
; function div to divide to integers
(defun div (n1 n2)
; dividing two integers and converting
; it to integer using floor function
(floor(/ n1 n2))
)
; function perfect, input n
(defun perfect (n)
; for loop for 1 to n/2
; defining variable sum, to keep sum
(defvar s 0)
; define last variable till loop will run
(defvar l (div n 2))
; for loop
(loop for x from 1 to l
; if n divisible by x
if (= (mod n x) 0)
; add x to s counter
do (setq s (+ s x))
)
(if (= s n)
1
; else 0
0
)
)
; a. calling function div and displaying result
(print "div 18 7")
(print (div 18 7))
; b. calling function div and displaying result
(print "perfect 28")
(print (perfect 28))
(print "perfect 14")
(print (perfect 14))
; Q2
; function power
(defun power(base power)
; loop till power to multiply base power times
(setq result 1)
; for loop
(loop for i from 1 to power
do (setq result (* result base))
)
; return result
result
)
(print "power function")
(print (power 3 2))
; function factorial
(defun factorial(fact)
; loop till fact to multiply each no. with result
; define result variable
(setq resultfact 1)
; for loop
(loop for i from 1 to fact
do (setq resultfact (* resultfact i))
)
; return result
resultfact
)
; display result
(print "factorial function on 5")
(print (factorial 5))
; function EeX
(defun EeX(x)
; defining variable result
(setq re 1.0)
; seting loop counter i
(setq i 1)
; finding x^n/n!
(setq temp (/ (power x i) (factorial i)))
; while loop till x^n/n! < 0.001
(loop while (>= temp 0.001)
do (setq re (+ re temp))
; updating i
do (setq i (+ i 1))
; updating temp
do (setq temp (/ (power x i) (factorial i)))
)
; returning result
re
)
(print "ex(1)")
; calling function Eex
(print (EeX 1))
OUTPUT IMAGE