Unit 05 Extra English
Unit 05 Extra English
1
• Step 1: Input a and b.
• Step 2: s a + b; d a – b; p a * b
• Step 3: Show
– Addition is s
– Subtraction is d
– Multiplication is p
• Step 4: If b = 0, show “division by zero”
Else show "division is a/b"
• Step 5: End
2
Exercise 2
3
• Step 1: Input a and b
• Step 2: If a ≠ 0 then x ← -b/a. Show “The equation has a
single solution x”.
Else goto Step 3
• Step 3: If b ≠ 0 then show “The equation has no available
solutions”.
Else show “the equation has infinite solutions”
• Step 4: End
4
Exercise 3
5
• Step 1: Input a,b, and c
• Step 2: If a ≠ 0 then delta = b2 – 4ac, goto step 3
Else goto step 5
• Step 3: If delta <0 then show “the equation has no solution”
Else goto step 4
• Step 4: If delta = 0 then show “the equation has a solution x = -b/(2*a)
Else show “the equation has 2 solutions, x1= …”
• Step 5: If b ≠ 0 then x ← -c/b. Show “The equation has a single solution x”.
Else goto Step 6
• Step 6: If c ≠ 0 then show “The equation has no available solutions”.
Else show “the equation has infinite solutions”
• Step 7: End
6
Exercise 4
• Given a positive integer p. How to check whether
p is a prime number or not?
– Input: p is a positive integer number
– Output: showing that p is a prime number or not
• Idea?
– p<1 Invalid number
– p = 1? p is not a prime number
– p > 1?
• Could we find a divisor of p from range 2 to p-1?
• If found, p is not a prime number. If not, p is a prime
number
7
Exercise 4
Input p
if p=1 then begin
Output: p is not a prime number;
Stop the algorithm;
end
flag := TRUE
for k:=2 to p-1 do
if (k is a divisor of p) then begin
flag:=FALSE;
break; { stop the FOR loop }
end
if flag=TRUE then
Output : p is a prime number
else
Output : p is not a prime number 8
Exercise 5
• Find the greatest common divisor (GCD) and the least common factor (LCF)
of two variables a and b
• Step 1 : Input a and b
• Step 2 : x a, y b
• Step 3 : If y ≠ 0, goto Step 4
If not, GCD x, LCF (a*b)/ GCD. Show GCD and LCF. End
• Step 4 : r x mod y
xy
yr
Goto Step 3.
9
Exercise 6
1. Find all disivors of a positive integer N
• Idea: Check all values from 1 to N and print out values that are divisors of
N.
Algorithm
• Step 1 : Input N
• Step 2 : Assign i 1
• Step 3 : If i ≤ N then goto B4
Else end
• Step 4 : If N mod i = 0, output i.
• Step 5 : Increase 1 into i. Goto step 3
10
Exercise 7
1. Find all odd disivors of a positive integer N
• Idea: Check all values from 1 to N and print out values that are divisors of
N.
Algorithm
• Step 1 : Input N
• Step 2 : Assign i 1
• Step 3 : If i ≤ N then goto B4
Else end
• Step 4 : If N mod i = 0, output i.
• Step 5 : Add 2 into i value. Goto step 3
11
Exercise 7.2
1. Find all even disivors of a positive integer N
• Idea: Check all even values from 1 to N and print out values that are
divisors of N.
Algorithm
• Step 1 : Input N
• Step 2 : Assign i 2
• Step 3 : If i ≤ N then goto B4
Else end
• Step 4 : If N mod i = 0, output i.
• Step 5 : Add 2 into i value. Goto step 3
12
Exercise 8
3. Check the perfection of a positive integer N.
• Step 1 : Input N
• Step 2 : Assign i 1, TongUoc 0,
• Step 3 : If i < N goto Step 4
Else goto Step 6
• Step 4 : If N mod i = 0, Assign TongUoc TongUoc + i
• Step 5 : Increase i by 1. Goto Step 3
• Step 6 : If TongUoc = N, Output “N is a perfect number”.
Else, output “N is not a perfect number”
• Step 7 : End
13
Exercise 9
5. Find the N Fibonacci number
Algorithm
•Step 1 : Input N
•Step 2 : Assign i 2, Fn-1 1, Fn-2 1, Fn 1
•Step 3 : If i < N goto Step 4.
Else output Fn. End.
•Step 4 : Assign Fn Fn-1 + Fn-2
Assign Fn-2 Fn-1
Assign Fn-1 Fn
Assign i i + 1
Goto Step 3
14
Exercise 10
4. Show all perfect number from the range [1,10000]
15