Step 4 Compute the product of all the common prime factors and return it as gcd (m,n )
Prime Factorisation Input: Integer x ≥ 2, Output: List F of prime factors of x P ← Sieve(x) while n > 1 do while n mod P[i] = 0 do F ← F + P[i] x ← x / P[i] i ← i + 1 Sieve of Eratosthenes Input: Integer x ≥ 2 Output: List of primes less than or equal to x for p ← 2 to x do A [ p ] ← p for p ← 2 to x do if A [ p ] 0 j ← p * p while j ≤ x do A [ j ] ← 0 j ← j + p
How does the performance of this algorithm compare to Euclid’s and Consecutive Integer Checking?
Note also that an algorithm will be required to find all common prime factors too…
We usually evaluate sorting algorithm complexity by the number of key comparisons .
Two important properties
Stability : A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input.
In place : A sorting algorithm is in place if it does not require extra memory, except, possibly for a few memory units.
Sorting Problems SelectionSort(A[0..n-1]) //Input: An array A[0..n-1] of orderable elements //Output: Array A[0..n-1] sorted in ascending order for i ← 0 to n – 2 do min ← i for j ← i + 1 to n – 1 do if A[j] < A[min] min ← j swap A[i] and A[min]
An example of a simple (and often low-performance) sorting algorithm is Selection Sort