Question

In: Computer Science

Read about the Sieve of Sundaram. Implement the algorithm using function composition. Given an integer n,...

Read about the Sieve of Sundaram. Implement the algorithm using function composition.
Given an integer n, your function should generate all the odd prime numbers up to 2n+2.

sieveSundaram :: Integer -> [Integer]
sieveSundaram = ...

To give you some help, below is a function to compute the Cartesian product of two lists.
This is similar to zip, but it produces all possible pairs instead of matching up the list
elements. For example,

cartProd [1,2] ['a','b'] == [(1,'a'), (1,'b'), (2,'a'), (2,'b')]

It's written using a list comprehension, which we haven't talked about in class (but feel free
to research them).
cartProd :: [a] -> [b] -> [(a,b)]
catProd xs ys = [(x,y) | x <- xs, y <- ys]

Solutions

Expert Solution

code

solution

Note:programming language is not given.I am using racket program code

//output

//output

//copyable code

1.

#lang racket

(define (sievesundaram num)

(define primes1 (make-vector (add1 num) #t))

(for* ([k (in-range 2 (add1 num))]

         #:when (vector-ref primes1 k)

         [j1 (in-range (* k k) (add1 num) k)])

    (vector-set! primes1 j1 #f))

(for/list ([num (in-range 2 (add1 num))]

             #:when (vector-ref primes1 num))

    num))

(sievesundaram 20)

2.

#lang racket

;require data

(require rackunit

(only-in racket/list cartesian-product))

;; check the data

(check-equal? (cartesian-product '(7 4) '(2 4))

             '((7 2) (7 4) (4 2) (4 4)))

;;test data

(cartesian-product '(1 2)'(a b))


Related Solutions

Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm...
Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm to find the largest value of ‘n’ for which f(n) is less than a target. Show all the work (including the values for the left index, right index, middle index and the function value at the middle index) for each iteration as well as write down the initial values of the left index and right index and the corresponding function values. f(n) = 3n^3...
Using C: Implement four versions of a function funSum that takes a positive integer n as...
Using C: Implement four versions of a function funSum that takes a positive integer n as input and returns the sum of all integers up to and including n that are divisible by 6 or 7: using a for loop in funSum1, using a while loop in funSum2, using a do-while loop in funSum3, and using recursion in funSum4. Your output for the included test code should be: funSum1(20) = 57 funSum2(20) = 57 funSum3(20) = 57 funSum4(20) = 57
1. Implement the faster algorithm for integer multiplication in C++ as a function, called “multiply”, that...
1. Implement the faster algorithm for integer multiplication in C++ as a function, called “multiply”, that takes 2 unsigned integers and returns their multiplication as an unsigned integer. 2. Test your code in main. Hints: Represent integers as strings. Write a utility function that pads integers with zeros, this will be useful If the 2 integers differ in length In calculating ??10^? and (??+??) 10^(?/2)
factorize the integer 2896753 by using the Quadratic Sieve method
factorize the integer 2896753 by using the Quadratic Sieve method
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?
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes...
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes and M resource types (N<10,M<10). Use C/C++/C# or Java for the implementation, with a simple text interface, where the user enters only the name of the input file (text only). The program reads all the necessary input data from that file. The input data and result is then displayed on the screen. You may use your program to validate the example you gave in...
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in...
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in the "How it works" section), where you take some user-defined number of steps that randomly step in W and I and are scaled by a step_size. This means you don't want to take steps of a discrete size, but instead use a random distribution of step sizes that are scaled by your step_size variable. You'll want to take steps that have an equal chance...
Envision an algorithm that when given any positive integer n, it will print out the sum...
Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. E.g. given 4 the algorithm would print 30 (because 1 + 4 + 9 + 16 = 30) You can use multiplication denoted as * in your solution and you do not have to define it (e.g. 2*2=4) Write pseudocode for this algorithm using iteration (looping). Create a flow chart Implement solution from flowchart in Python at...
PYTHON: Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient...
PYTHON: Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient Greeks. Choose an integer n. This function will compute all prime numbers up to n. First insert all numbers from 1 to n into a set. Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, . . . . Erase all multiples of 3, that is, 6, 9, 12, 15, ... . Go up to √ n. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT