You are on page 1of 8

Algorithm exercises

Exercise 1
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

2
Excercise 1 request:

• Draw a flowchart
• Run the algorithm with N=6 and draw a table of all the
variables

i
Fn-2
Fn-1
Fn

3
Exercise 2: Make an algorithm in psudocode and flowchart

• 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

4
Exercise 3
• Find the greatest common divisor (GCD) and the least common factor (LCF)
of two variables a and b

5
Exercise 4
Find all divisors of a positive integer N

6
Exercise 5
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.

7
Exercise 6
Create an algorithm (by flowchart and psudocode) that:
Show all perfect number from the range [1,10000]

You might also like