MECN2012: Computing Skills and Software Development: Summary of Algorithms and Translation of Logic Into Code
MECN2012: Computing Skills and Software Development: Summary of Algorithms and Translation of Logic Into Code
Algorith
m
Where used
Nonlooping
SwitchCase
If-ThenElse
Looping For
While
Translating Logic to
Algorithms
e.g. Identifying prime numbers by the trial
division method
Identify the conditions under which operations
must be performed
Develop code for the most basic operation(s)
you want to perform, possibly several of these
for different conditions [e.g. Checking if one
number is a prime number]
Adapt operation(s) for repetition if needed [e.g.
Checking a range of numbers for being prime]
Identify Conditions
Need to get limits from user (can be passed
to function)
If the range of numbers includes 2 and 3,
these numbers cannot be tested by the
trial division method and must be included
in the reported set
All numbers bigger than 3 can be tested by
the trial division method
If the upper limit is less than 5 no trial
division is necessary
Basic Operations
For a number to be a prime number, the
modulo of all numbers between 2 and its
square root and the number itself must NOT
be zero
Test number: n
Range of numbers: x = 2:1:floor(sqrt(n))
if min(mod(n,x)) ~= 0
(Prime number) Print to screen
else
(Not a prime number) Do nothing
end
Adapt Operation
If lower limit is less than 4 print default numbers:
if lowerlimit <= 3