Lab 7 practice problems involving loops

  1. Write a procedure that takes a single positive integer n as input and outputs the second prime number bigger than n. For example if n is 5, the procedure should return 11 because the two primes after 5 are 7, 11.
  2. Twin primes: Two numbers are called twin primes if they differ by 2 and are primes.
    Write a procedure that outputs all twin primes between 1 and 1,000,000.

    Note: It is conjectured that there are infinitely many twin primes but this has not been resolved (proved or disproved) yet.

  3. Fibonacci numbers: Fibonacci numbers are defined as F_n, n=1,2,...., where F_1 = F_2 = 1 and for n>2, F_n = F_(n-1) + F_(n-2). In other words, the first 2 terms are 1 and every term after that is the sum of the two previous terms.

    Write a procedure that takes a single positive integer n as input and outputs the nth Fibonacci number.

  4. 3n+1 conjecture This is a famous unsolved problem in Mathematics. Consider any natural number n. If n is even, divide it by 2. If n is odd multiply it by 3 and add 1 to obtain 3n + 1. The conjecture is that for any n, this process always eventually result in 1. Write a procedure that takes a single positive integer n as input and outputs the number of steps required to reach 1.

    Notes:
    1. You do not have to worry about this computation going on for a long time. According to the wikipedia entry on this topic, all numbers less than 10 billion reach 1 in at most 1132 steps.
    2. This conjecture has been verified for very large numbers but has not been proved or disproved analytically (yet).