0% found this document useful (0 votes)
23 views

Unit 05 Extra English

The document describes 9 exercises involving algorithms to solve mathematical problems. Exercise 1 involves calculating basic arithmetic on two numbers. Exercise 2 solves simple linear equations. Exercise 3 solves quadratic equations. Exercises 4-6 involve finding divisors of numbers. Exercise 7 finds odd and even divisors. Exercise 8 checks if a number is perfect. Exercise 9 calculates the Nth Fibonacci number. Exercise 10 finds all perfect numbers between 1 and 10,000.

Uploaded by

tuankietduong660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Unit 05 Extra English

The document describes 9 exercises involving algorithms to solve mathematical problems. Exercise 1 involves calculating basic arithmetic on two numbers. Exercise 2 solves simple linear equations. Exercise 3 solves quadratic equations. Exercises 4-6 involve finding divisors of numbers. Exercise 7 finds odd and even divisors. Exercise 8 checks if a number is perfect. Exercise 9 calculates the Nth Fibonacci number. Exercise 10 finds all perfect numbers between 1 and 10,000.

Uploaded by

tuankietduong660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Exercise 1

• Calculate the addition, subtraction, multiplication and


division of two numbers a and b.
– Input: Two numbers a and b
– Output: addition, subtraction, multiplication and division of a
and b.
• Idea:
– Calculate addition, subtraction, multiplication of a and b
– If b equals 0, show the notification of “division by zero”. If not,
show the division a/b

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

• Problem: Solve the simple equation


– Input: coefficient a and b
– Output: Solution of the simple equation
– ax + b = 0
• Idea:
– Assume a=0, then b=0 to check these cases
of the equation

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

• Problem: Solve the polynomial equation


– Input: coefficient a, b and c
– Output: Solution of the polynomial equation
– ax2 + bx + c = 0
• Idea:
– Assume a=0, then b=0, and then c=0 to check these
cases of the equation

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
xy
yr
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

You might also like