Update
This commit is contained in:
parent
69394ab8da
commit
d5e4b36718
11 changed files with 1790 additions and 1770 deletions
25
algorithm.md
25
algorithm.md
|
@ -2,7 +2,7 @@
|
|||
|
||||
Algorithm (from the name of Persian mathematician Muhammad ibn Musa al-Khwarizmi) is an exact step-by-step description of how to solve some type of a problem. Algorithms are basically what [programming](programming.md) is all about: we tell [computers](computer.md), in very exact ways (with [programming languages](programming_language.md)), how to solve problems -- we write algorithms. But algorithms don't have to be just computer programs, they are simply exact instruction for solving problems. Although maybe not as obvious, [mathematics](math.md) is also a lot about creating algorithms because it strives to give us exact instructions for solving problems -- a mathematical formula usually tells us what we have to do to compute something, so in a way it is an algorithm too.
|
||||
|
||||
Cooking recipes are commonly given as an example of a non-computer algorithm, though they rarely contain branching ("if condition holds then do...") and loops ("while a condition holds do ..."), the key features of algorithms. The so called wall-follower is a simple algorithm to get out of any [maze](maze.md) which doesn't have any disconnected walls: you just pick either a left-hand or right-hand wall and then keep following it. You may write a crazy algorithm basically for any kind of problem, e.g. for how to clean a room or how to get a [girl](woman.md) to bed, but it has to be **precise** so that anyone can execute the algorithm just by blindly following the steps; if there is any ambiguity, it is not considered an algorithm; a vague, imprecise "hint" on how to find a solution (e.g. "the airport is somewhere in this general direction.") we rather call a [heuristic](heuristic.md). Heuristics are useful too and they may be utilized by an algorithm, e.g. to find a precise solution faster, but from programmer's point of view algorithms, the PRECISE ways of finding solutions, are the basics of everything.
|
||||
Cooking recipes are commonly given as an example of a non-computer algorithm, though they rarely contain branching ("if condition holds then do...") and loops ("while a condition holds do ..."), the key features of algorithms. The so called wall-follower is a simple algorithm to get out of any [maze](maze.md) which doesn't have any disconnected walls: you just pick either a left-hand or right-hand wall and then keep following it. Long division of numbers which students are taught at school is also an algorithm. You may write a crazy algorithm basically for any kind of problem, e.g. for how to clean a room or how to get a [girl](woman.md) to bed, but it has to be **precise** so that anyone can execute the algorithm just by blindly following the steps; if there is any ambiguity, it is not considered an algorithm; a vague, imprecise "hint" on how to find a solution (e.g. "the airport is somewhere in this general direction.") we rather call a [heuristic](heuristic.md). Heuristics are useful too and they may be utilized by an algorithm, e.g. to find a precise solution faster, but from programmer's point of view algorithms, the PRECISE ways of finding solutions, are the basics of everything.
|
||||
|
||||
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](time_complexity.md)).
|
||||
|
||||
|
@ -98,6 +98,25 @@ if divisors == 2:
|
|||
print("It is a prime!")
|
||||
```
|
||||
|
||||
in [JavaScript](javascript.md) as:
|
||||
|
||||
```
|
||||
function main()
|
||||
{
|
||||
let x = parseInt(prompt("enter a number:"));
|
||||
let divisorCount = 0;
|
||||
|
||||
for (let i = 1; i <= x; ++i)
|
||||
if (x % i == 0) // i divides x?
|
||||
divisorCount++;
|
||||
|
||||
console.log("divisors: " + divisorCount);
|
||||
|
||||
if (divisorCount == 2)
|
||||
console.log("It is a prime!");
|
||||
}
|
||||
```
|
||||
|
||||
in [C](c.md) as:
|
||||
|
||||
```
|
||||
|
@ -148,7 +167,7 @@ variable divisorCount
|
|||
0 divisorCount !
|
||||
|
||||
x @ 1+ 1 do
|
||||
x @ i mod 0 = if
|
||||
x @ i mod 0 = if \ i divides x?
|
||||
1 divisorCount +!
|
||||
then
|
||||
loop
|
||||
|
@ -221,6 +240,8 @@ and in Scheme [Lisp](lisp.md) as (here notice the difference in [paradigm](parad
|
|||
(countDivisors (read) 1 0)
|
||||
```
|
||||
|
||||
TODO: assembly, haskell, unix shell, ...
|
||||
|
||||
This algorithm is however not very efficient and could be **[optimized](optimization.md)** -- for example not only we wouldn't have to check if a number is divisible by 1 and itself (as every number is), but there is also no need to check divisors higher than the [square root](sqrt.md) of the checked value (mathematically above square root there only remain divisors that are paired with the ones already found below the square root) so we could lower the number of the loop iterations and so make the algorithm finish faster. You may try to improve the algorithm this way as an exercise :-)
|
||||
|
||||
## Study of Algorithms
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue