

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


e. (^) ( i i ) i
2 1
30
Recall that an algorithm is a finite sequence of steps which will solve a given problem: C Each step must be well-defined and deterministic C Each step should take a finite amount of time C The algorithm must terminate after a finite number of steps (in a finite amount of time)
b. Given a list of integers a 1 , ..., an : i Set found to false ii Set k = 1 iii Consider the pair a (^) k and ak +1. If ak < ak +1, then swap them and set found to true iv If k < n ñ 1, then increment k and go to step ii. v if found is true then go back to step i.
c. Given a positive integer n i Set k = 0 ii If n is even, then cut it in half, otherwise triple it and add 1 iii Increment k iv If n > 1 then go to step i. For this problem, just give your best guess about what you think will happen. A sample sequence of what happens, if n = 3, is 3! 10! 5! 16! 8! 4! 2! 1.
d. Given an integer n > 1 which is congruent to 1 (mod 4): i Find all pairs of positive integers a and b , with a > b , such that a^2 + b^2 = n ii If there is exactly one such pair, then print ìPrimeî, otherwise print ìCompositeî
pos = n / jump = 1 dir = 1 while ( pos < n ){ if ( apos mod 2 = 0){ print “Position =“ pos exit } else { pos = pos + dir * jump jump = jump + 1 dir = – dir } print “No even numbers in list” }
while n is even n = n / 2 print n
for a = 1 to n { for b = 1 to n { if a 2 + b 2 = n print a and b } }