Fundamentals of
algorithmization and programming
Algorithm analysis
What is an algorithm?
Algorithm is a sequence of actions used to solve a problem.
In programming, algorithms are described in different
languages (Python, C++, Pascal, etc.).
Algorithms contain
variables operations branches data output
Algorithm analysis
Problem statement
Example: Program:
Imagine we have two numbers and we want s = 0
to perform different calculations on them a = int(input())
depending on their divisibility. b = int(input())
if a % b == 0:
What does this program do? s = a // b + 10
Let's analyse it step by step. else:
s = a / b + 5
print(s)
Algorithm analysis
Variables
these are named memory cells where data is stored. Variables can be
numbers, strings, lists, etc.
In our example, there are variables: a, b, s.
Program:
The s variable is set to 0.
s = 0
a = int(input()) a and b are the variables entered by user.
b = int(input()) They can be any numbers.
if a % b == 0:
s = a // b + 10
else: Input example:
s = a / b + 5 a = 5 # a stores the number 5
print(s) b = 3 # b stores the number 3
*Now the program uses them for calculations.
Algorithm analysis
Input and output data
Input data Output data
getting values from displaying the program
user results (the program
(user enters two calculates s and
numbers, a and b) outputs it)
Algorithm analysis
Operations on numbers Program:
s = 0
There are mathematical operations a = int(input())
in the code b = int(input())
if a % b == 0:
s = a // b + 10
+ (addition) else:
s = a / b + 5
- (subtraction) print(s)
* (multiplication)
Example:
/ (division with a fractional part) 10 % 3 = 1 # the remainder is 1
10 % 2 = 0 #divided without
// (integer division) remainder
* This is the key point that determines
% (remainder of division) which formula will be used.
Algorithm analysis
The mod and % operators
In the Pascal language, mod is used to find the remainder of a division.
In Python and C++, % is used.
Example:
Both operators output the same result:
10 % 3 # Returns 1, since 10 is divisible by 3 with a remainder of 1
6 % 2 # Returns 0, since 6 is divisible by 2 without remainder
Algorithm analysis
Conditional constructs (branches)
Allow execution of different actions depending on the stated conditions.
In the example, the code uses if to check whether a is divisible by b without
remainder.
If a % b == 0, then a specific formula is executed. Otherwise, another one is
executed.
Program:
Example:
s = 0
if 10 % 2 == 0: a = int(input())
s = 10 // 2 + 10 # The first formula is executed b = int(input())
if a % b == 0:
else:
s = a // b + 10
s = 10 / 2 + 5 # This part is skipped else:
s = a / b + 5
Result: 15.
print(s)
Algorithm analysis
Let's analyze the case when the
remainder is NOT equal to 0
Program:
Suppose the user enters: a = 7; b = 3.
7% 3 = 1 (there is a remainder, which means s = 0
it goes to else). a = int(input())
The formula a / b + 5 is executed. b = int(input())
if a % b == 0:
Counting: s = a // b + 10
7 / 3 + 5 else:
s = a / b + 5
Program output: 7. print(s)
Algorithm analysis
Conclusion
● We have analyzed a program that calculates the value of s depending
on the divisibility of a by b.
● The key point is the % operator, which helps you choose the right
formula.
● Code analysis helps you understand how variables,
operations, and conditions work.
Algorithm analysis