Question

In: Computer Science

imporant note (the language of compiler and programming llanguage) Q1: A positive integer number n is...

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:

  1. power” which receives x , n and returns xn

That is,

> ( power 3 2)

>9

  1. factorial” which receives an integer n≥0 and return n!.

That is,

> ( factorial 5)

>120

Note: Stop the recursion when (xn/n!) < 0.001

(comp439)

Solutions

Expert Solution

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


Related Solutions

//Java Language Read an integer number from the user. If the number is not positive, change...
//Java Language Read an integer number from the user. If the number is not positive, change its sign.    1   Count the digits of the number 2   Count the odd digits of the number 3   Calculate the sum of the digit values of the number
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Determine the number of permutations of {1,2,3,...,n-1,n} where n is any positive integer and no even...
Determine the number of permutations of {1,2,3,...,n-1,n} where n is any positive integer and no even integer is in its natural position.
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm?
ASSEMBLY LANGUAGE PROGRAMMING Q:Write a complete assembly program that inputs a small signed integer n, whose...
ASSEMBLY LANGUAGE PROGRAMMING Q:Write a complete assembly program that inputs a small signed integer n, whose value can fit within 8 bits, and outputs the value of the expression n2 – n + 6.
Let n be a positive integer. Prove that if n is composite, then n has a...
Let n be a positive integer. Prove that if n is composite, then n has a prime factor less than or equal to sqrt(n) . (Hint: first show that n has a factor less than or equal to sqrt(n) )
Translate the following pseudocode to MIPS assembly programming language cout << “\n Please input a number...
Translate the following pseudocode to MIPS assembly programming language cout << “\n Please input a number for $s0”; cin >> $s0; cout << “\n Please input a number for $s1”; cin >> $s1; cout << “\n Please input a number for $s2”; cin >> $s2; $t0 = $s0 / 8 - 2 * $s1 + $s2; cout << “\n the Value of the expression “$s0 / 8 - 2 * $s1 + $s2” is ”; cout >> $t0; return;
Prove or disprove that 3|(n 3 − n) for every positive integer n.
Prove or disprove that 3|(n 3 − n) for every positive integer n.
Prove that τ(n) < 2 n for any positive integer n. This is a question in...
Prove that τ(n) < 2 n for any positive integer n. This is a question in Number theory
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT