You are on page 1of 2

Algorithm 1 EuclidGCD(m, n) Output: true or false (表示 n 是否為質數)

Input: 二個正整數 m 及 n 1. for i = 2 to √ n do


Output: m, n 的最大公因數 (GCD) 2. if (n % i) == 0 then return false
1. r ← m % n 3. end if
2. while r = 0 do 4. end for
3. m←n 5. return true
4. n←r
5. r←m%n
6. end while
7. return n
Algorithm 4 BubbleSort(A, n)
Input: 具有 n 個整數的陣列 A
Output: 陣列 A(其中的整數已依其數值由小到大排列)
Algorithm 2 PrimeCheck1(n)
1. for i = n-1 to 1 do
Input: 一個大於 2 的正整數 n
2. for j = 0 to i-1 do
Output: true or false (表示 n 是否為質數)
3. if A[j] > A[j + 1] then
1. for i = 2 to n-1 do
4. swap(A[j], A[j + 1])
2. if (n % i) == 0 then return false
5. end if
3. end if
6. end for
4. end for
7. end for
5. return true
8. return A

Algorithm 5 InsertionSort(A, n)
Input: 具有 n 個整數的陣列 A
Algorithm 3 PrimeCheck2(n) Output: 陣列 A(其中的整數已依其數值由小到大排列)
Input: 一個大於 2 的正整數 n 1. for i = 1 to n-1 do
2. j←i–1
3. temp ← A[i]
4. while (t < A[j] & j >= 0) do
5. A[j+1] ← A[j]
6. j←j–1
7. end while
8. A[j+1] ← temp
9. end for
10. return A

You might also like