a) A prime number is a positive integer greater than 1 that cannot be formed by multiplying two smaller positive integers.

Question

Write pseudocode for a function isPrime(n) which accepts a positive integer n (where n > 1) as an argument and returns true if n is a prime number and false if n is not a prime number.
b) Draw a flowchart of the algorithm which is used in your answer to part a).
c) Twin primes are pairs of prime numbers of the form (p, p+2) for example: (3, 5), (5, 7), (11, 13), (17, 19).

Compare and contrast the following two pseudocode representations of an algorithm which finds all twin primes where both numbers are less than 40. Comment on their correctness and efficiency.

Algorithm 1

for i = 2 to 40 Step 1

if (isPrime(i) AND isPrime(i + 2))

Display i and i+2 as a twin prime pair

 

Algorithm 2

for i = 3 to 37 Step 2

if (isPrime(i) AND isPrime(i + 2))

Display i and i+2 as a twin prime pair

Leave an answer

Sorry, you do not have permission to answer to this question .