Question
DR racket worksheets S anbul Unne x tlockboard Content och x CMPE 100 10sotall worksheets x + 67 1 00 VERGEVEDO Fall 2019-2020 CMPE 100:
DR racket worksheets S anbul Unne x tlockboard Content och x CMPE 100 10sotall worksheets x + 67 1 00 VERGEVEDO Fall 2019-2020 CMPE 100: Introduction to Computing Worksheet08 1. (50 points) A number is prime if no number divides it except 1 and itself. To test if a wmber p is prime we can take its modulo of all numbers starting with 2 up to p-1, and none of these modulos are O then we can say that the number is prime. Let us define a helper function which tells if a number is divisible by numbers up to some other wumber, d{p.k), its value is true if any number from 2 to k divides p. We can write this function in a recursive manner as follows: Now, design the program to compute it. and P mod k) = 0 false if k 2 dip, k-1) otherwise Therefore primality test is converted into the following: isPrime(p)=not (d(p.p-1)) 2. (50 points) Design a Racket function named power to find an integer power of a number, r" seems to require -1 multiplications at first sight. However, this computation can be simplified greatly. Think about 24, which would require 7 multiplications to compute. Since the exponent 8 is an even number, this can be written as 28 - 21. Therefore we need 3 multiplications to compute 24 and another multiplication to compute its square. It can be further simplified as 20 - 21 - 23, which now requires only 3 multiplications. In case of an odd exponent, one can rewrite only for the even part: 2" = 2.2 A recursive formulation is: (1/6 ) if 20 ifo ifn=1 in mod 2 = 0 if n mod 2 + 0 ( -3)/2) Note: Ensure that your programs are fully documented, using comments.=>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started