Skip to main content
Logo image

Exercises 2.5 Programming Exercises

1. The Sieve of Eratosthenes.

One method of computing all of the prime numbers less than a certain fixed positive integer N is to list all of the numbers n such that 1<n<N. Begin by eliminating all of the multiples of 2. Next eliminate all of the multiples of 3. Now eliminate all of the multiples of 5. Notice that 4 has already been crossed out. Continue in this manner, noticing that we do not have to go all the way to N; it suffices to stop at N. Using this method, compute all of the prime numbers less than N=250. We can also use this method to find all of the integers that are relatively prime to an integer N. Simply eliminate the prime factors of N and all of their multiples. Using this method, find all of the numbers that are relatively prime to N=120. Using the Sieve of Eratosthenes, write a program that will compute all of the primes less than an integer N.

2.

Let N0=Nโˆช{0}. Ackermannโ€™s function is the function A:N0ร—N0โ†’N0 defined by the equations
A(0,y)=y+1,A(x+1,0)=A(x,1),A(x+1,y+1)=A(x,A(x+1,y)).
Use this definition to compute A(3,1). Write a program to evaluate Ackermannโ€™s function. Modify the program to count the number of statements executed in the program when Ackermannโ€™s function is evaluated. How many statements are executed in the evaluation of A(4,1)? What about A(5,1)?

3.

Write a computer program that will implement the Euclidean algorithm. The program should accept two positive integers a and b as input and should output gcd(a,b) as well as integers r and s such that
gcd(a,b)=ra+sb.