Lab 2 (Year 2 Sem 2)

Lab 2 is the third lab in semester 2 of year 2, and the 16th lab in the total of year 2. It is a lab for "Algorithms", and is considered easy.

Description
No description was provided for this lab.

Unlocked after completion

 * Optional2 (Score at least 75% for Lab2 - Algorithm)

Question
Question 1: Primality Test

Given the positive number n, output the number of steps need to reach 1

Implementation Sample 
 * Implement a function p_test
 * The input n is non-negative integer
 * The output "True" is n is a prime and "False" otherwise
 * p_test(2) = True
 * p_test(4) = False

Solution
This is like Year 2 Sem 1 Lab 6.2 Question 2.

Question
Question 2: Sieve of Eratosthenes

Implementation
 * Implement a function sieve
 * The input n is positive integer, n is up to 500,000
 * The output number of primes up to and including n

Sample
 * sieve(2) = 1
 * sieve(6) = 3

Solution
Use algorithm described in notes. For each prime number (ie prime_list[number] is True), modify all divisors from number**2 to N, that are a factor of number, to be False.

Question
Question 3: Q3

The sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to N. One version of the algorithm is

Given the values of N and K, find out the K-th integer to be marked.
 * 1) write down all integers from 2 to N, both inclusive.
 * 2) Find the smallest number not already marked; call this value P and P is prime.
 * 3) Mark out P and all its multiples that aren’t already marked.
 * 4) If not all numbers have been marked, go to step 2

Implementation

·       Implement a function Q3(N, K), where the N and K are positive integers as described above, K < N

·       This function returns an integer, which is the K-th integer to be marked.

Sample

·       Q3( 10, 6 ) = 3

·       The integers marked, in order, are 2, 4, 6, 8, 10, 3, 9, 5, 7

Solution
Similar to previous qn, just that you will need a separate list to store the marked numbers.

Question
Question 4: Prime factorization

Implementation

Sample
 * Implement a p_fac
 * The input n is positive integer greater than 1,
 * The output is a 2D list . each row is in the form of [a, b], representing one prime factor and its power in the prime factorization expression.


 * 2 = 21,, so p_fac (2) = [ [2, 1] ]
 * 6 = 21x 31, so p_fac(6) = [ [2,1], [3,1] ]
 * 360 = 23x32x51, so p_fac(360) = [ [2,3], [3,2] , [5,1] ]

Solution
Follow algorithm in notes. (mainly, for each prime number up to sqrt(n), test how many times it can be divided into the input n, if the resulting number is more than 1, also add it to the list)

You will then need to process the list. For example, the list created by the algorithm for 180 is [2,2,3,3,5].

You will need to count how many times these numbers appear using list.count. Once you have the new 2d list (for 180 it is 2,2], [3,2], [5,1), return it.

Do note that the resulting list must be ordered by its first number. (eg for 18, it should be 2,1], [3,2 and not the other way around)