This commit is contained in:
Miloslav Ciz 2022-11-28 20:07:33 +01:00
parent 246d228d98
commit cd6c3a313d
6 changed files with 28 additions and 16 deletions

View file

@ -6,7 +6,7 @@ Cooking recipes are sometimes given as an example of a non-computer algorithm. T
Interesting fact: contrary to intuition there are problems that are mathematically proven to be unsolvable by any algorithm, see [undecidability](undecidability.md), but for most practically encountered problems we can write an algorithm (though for some problems even our best algorithms can be unusably slow).
Algorithms are mostly written as a **series of steps** (or instructions); these steps may be specific actions (such as adding two numbers or drawing a pixel to the screen) or **conditional jumps** to other steps ("if condition X holds then jump to step N, otherwise continue"). These jumps can be used to create **[branches](branch.md)** (in programming known as *if-then-else*) and **[loops](loop.md)** (these two constructs are known as [control structures](control_structure.md) -- they don't express an action but control where we move in the algorithm itself). All in all, **any algorithm can be written with only these three constructs**:
Algorithms are mostly (possibly [not always](declarative.md), depending on definitions) written as a **series of steps** (or instructions); these steps may be specific actions (such as adding two numbers or drawing a pixel to the screen) or **conditional jumps** to other steps ("if condition X holds then jump to step N, otherwise continue"). These jumps can be used to create **[branches](branch.md)** (in programming known as *if-then-else*) and **[loops](loop.md)** (these two constructs are known as [control structures](control_structure.md) -- they don't express an action but control where we move in the algorithm itself). All in all, **any algorithm can be written with only these three constructs**:
- **sequence**: A series of steps, one after another.
- **selection** (branches, *if-then-else*): Two branches (sequences of steps) preceded by a condition; the first branch is executed only if the condition holds, the second ("else") branch is executed only if the condition doesn't hold (e.g. "If user password is correct, log the user in, otherwise print out an error.").
@ -26,8 +26,8 @@ Let's write a simple algorithm that counts the number of divisors of given numbe
2. Set the *divisor counter* to 0.
3. Set *currently checked number* to 1.
4. While *currently checked number* is lower or equal than *x*:
a. If *currently checked number* divides *x*, increase *divisor counter* by 1.
b. Increase *currently checked number*.
- a: If *currently checked number* divides *x*, increase *divisor counter* by 1.
- b: Increase *currently checked number*.
5. Write out the *divisor counter*.
6. If *divisor counter* is equal to 2, write out the number is a prime.
@ -60,8 +60,14 @@ Notice that *x*, *divisor counter* and *currently checked number* are [variables
| increase divisor | |
| count by 1 | |
| | | |
| V | |
--------------<------------- |
| | | |
| |<------------- |
| | |
| V |
| increase checked V
| number by 1 print divisor count
| | |
-------------- |
V no
divisor count = 2 ? ------
| |
@ -69,7 +75,7 @@ Notice that *x*, *divisor counter* and *currently checked number* are [variables
V |
print "number is prime" |
| |
|<---------------|
|<----------------
V
END
@ -83,7 +89,7 @@ x = int(input("enter a number: "))
divisors = 0
for i in range(1,x + 1):
if x % i == 0:
if x % i == 0: # i divides x?
divisors = divisors + 1
print("divisors: " + str(divisors))
@ -104,7 +110,7 @@ int main(void)
scanf("%d",&x); // read a number
for (int i = 1; i <= x; ++i)
if (x % i == 0)
if (x % i == 0) // i divides x?
divisors = divisors + 1;
printf("number of divisors: %d\n",divisors);