Update
This commit is contained in:
parent
a7b086a309
commit
7a4c69819f
14 changed files with 59 additions and 18 deletions
31
algorithm.md
31
algorithm.md
|
@ -98,7 +98,7 @@ if divisors == 2:
|
|||
print("It is a prime!")
|
||||
```
|
||||
|
||||
and in [C](c.md) as:
|
||||
in [C](c.md) as:
|
||||
|
||||
```
|
||||
#include <stdio.h>
|
||||
|
@ -122,6 +122,35 @@ int main(void)
|
|||
}
|
||||
```
|
||||
|
||||
and in [comun](comun.md) as (for simplicity only works for numbers up to 9):
|
||||
|
||||
```
|
||||
<- "0" - # read X and convert to number
|
||||
0 # divisor count
|
||||
1 # checked number
|
||||
|
||||
@@
|
||||
$0 $3 > ? # checked num. > x ?
|
||||
!@
|
||||
.
|
||||
|
||||
$2 $1 % 0 = ? # checked num. divides x ?
|
||||
$1 ++ $:2 # increase divisor count
|
||||
.
|
||||
|
||||
++ # increase checked number
|
||||
.
|
||||
|
||||
0 "divisors: " --> # write divisor count
|
||||
$1 "0" + -> 10 ->
|
||||
|
||||
$1 2 = ?
|
||||
0 "It is a prime" --> 10 ->
|
||||
.
|
||||
```
|
||||
|
||||
This algorithm is however not very efficient and could be [optimized](optimization.md) -- for example there is no need to check divisors higher than the square root of the checked value (mathematically above square root the only divisor left is the number itself) so we could lower the number of the loop iterations and so make the algorithm finish faster.
|
||||
|
||||
## Study of Algorithms
|
||||
|
||||
Algorithms are the essence of [computer science](scompsci.md), there's a lot of theory and knowledge about them.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue