Update
This commit is contained in:
parent
3a465aea74
commit
69394ab8da
13 changed files with 1928 additions and 1822 deletions
102
algorithm.md
102
algorithm.md
|
@ -101,12 +101,13 @@ if divisors == 2:
|
|||
in [C](c.md) as:
|
||||
|
||||
```
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int x, divisors = 0;
|
||||
|
||||
|
||||
printf("enter a number: ");
|
||||
scanf("%d",&x); // read a number
|
||||
|
||||
for (int i = 1; i <= x; ++i)
|
||||
|
@ -114,20 +115,68 @@ int main(void)
|
|||
divisors = divisors + 1;
|
||||
|
||||
printf("number of divisors: %d\n",divisors);
|
||||
|
||||
|
||||
if (divisors == 2)
|
||||
puts("It is a prime!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
and in [comun](comun.md) as (for simplicity only works for numbers up to 9):
|
||||
in [Forth](forth.md) as:
|
||||
|
||||
```
|
||||
<- "0" - # read X and convert to number
|
||||
0 # divisor count
|
||||
1 # checked number
|
||||
variable x
|
||||
variable divisorCount
|
||||
|
||||
: main
|
||||
." enter a number: "
|
||||
0 \ number to read
|
||||
|
||||
begin \ read the number by digits
|
||||
key
|
||||
|
||||
dup dup 48 >= swap 57 <= and
|
||||
while
|
||||
|
||||
48 - swap 10 * +
|
||||
repeat
|
||||
drop
|
||||
|
||||
dup . cr
|
||||
x !
|
||||
0 divisorCount !
|
||||
|
||||
x @ 1+ 1 do
|
||||
x @ i mod 0 = if
|
||||
1 divisorCount +!
|
||||
then
|
||||
loop
|
||||
|
||||
." number of divisors: " divisorCount @ . cr
|
||||
|
||||
divisorCount @ 2 = if
|
||||
." It is a prime!" cr
|
||||
then
|
||||
;
|
||||
|
||||
main
|
||||
bye
|
||||
```
|
||||
|
||||
in [comun](comun.md) as:
|
||||
|
||||
```
|
||||
0
|
||||
@@ # read X and convert to number
|
||||
<-
|
||||
$0 $0 "0" < >< "9" > | ? !@ .
|
||||
"0" - >< 10 * +
|
||||
.
|
||||
^
|
||||
|
||||
0 # divisor count
|
||||
1 # checked number
|
||||
|
||||
@@
|
||||
$0 $3 > ? # checked num. > x ?
|
||||
|
@ -138,24 +187,47 @@ and in [comun](comun.md) as (for simplicity only works for numbers up to 9):
|
|||
$1 ++ $:2 # increase divisor count
|
||||
.
|
||||
|
||||
++ # increase checked number
|
||||
++ # increase checked number
|
||||
.
|
||||
|
||||
0 "divisors: " --> # write divisor count
|
||||
$1 "0" + -> 10 ->
|
||||
0 "divisors: " --> # write divisor count
|
||||
|
||||
$1 10 >= ?
|
||||
$1 10 / "0" + ->
|
||||
.
|
||||
|
||||
$1 10 % "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.
|
||||
and in Scheme [Lisp](lisp.md) as (here notice the difference in [paradigm](paradigm.md) -- loop is replaced with [recursion](recursion.md), as it's done in [functional](functional.md) programming):
|
||||
|
||||
```
|
||||
(define (countDivisors x countFrom currentCount) ; recursive function
|
||||
(if (> countFrom x)
|
||||
(begin
|
||||
(display "divisor count: ")
|
||||
(display currentCount)
|
||||
(newline)
|
||||
(if (= currentCount 2)
|
||||
(display "It is a prime!\n")))
|
||||
(countDivisors x (1+ countFrom)
|
||||
(+ currentCount (if (zero? (remainder x countFrom)) 1 0)))))
|
||||
|
||||
(display "enter a number: ")
|
||||
(countDivisors (read) 1 0)
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
Algorithms are the essence of [computer science](compsci.md), there's a lot of theory and knowledge about them.
|
||||
Algorithms are the core topic of [computer science](compsci.md), there's a lot of theory and knowledge about them.
|
||||
|
||||
[Turing machine](turing_machine.md), a kind of mathematical bare-minimum computer, created by [Alan Turing](turing.md), is the traditional formal tool for studying algorithms, though many other [models of computation](model_of_computation.md) exist. From theoretical computer science we know not all problems are [computable](computability.md), i.e. there are problems unsolvable by any algorithm (e.g. the [halting problem](halting_problem.md)). [Computational complexity](computational_complexity.md) is a theoretical study of resource consumption by algorithms, i.e. how fast and memory efficient algorithms are (see e.g. [P vs NP](p_vs_np.md)). [Mathematical programming](mathematical_programming.md) is concerned, besides others, with optimizing algorithms so that their time and/or space complexity is as low as possible which gives rise to algorithm design methods such as [dynamic programming](dynamic_programming.md) (practical [optimization](optimization.md) is a more pragmatic approach to making algorithms more efficient). [Formal verification](formal_verification.md) is a field that tries to mathematically (and sometimes automatically) prove correctness of algorithms (this is needed for critical software, e.g. in planes or medicine). [Genetic programming](generic_programming.md) and some other methods of [artificial intelligence](ai.md) try to automatically create algorithms (*algorithms that create algorithms*). [Quantum computing](quantum.md) is concerned with creating new kinds of algorithms for quantum computers (a new type of still-in-research computers). [Programming language](programming_language.md) design is the art and science of creating languages that express computer algorithms well.
|
||||
[Turing machine](turing_machine.md), a kind of mathematical bare-minimum computer, created by [Alan Turing](turing.md), is the traditional formal tool for studying algorithms, though many other [models of computation](model_of_computation.md) exist -- for example [lambda calculus](lambda_calculus.md) that's a basis of [functional programming](functional.md) under which we already see algorithms in a bit different light: not as a series of steps but rather as evaluating mathematical functions. From theoretical computer science we know not all problems are [computable](computability.md), i.e. there are problems unsolvable by any algorithm (e.g. the [halting problem](halting_problem.md)). [Computational complexity](computational_complexity.md) is a theoretical study of resource consumption by algorithms, i.e. how fast and memory efficient algorithms are (see e.g. [P vs NP](p_vs_np.md)). [Mathematical programming](mathematical_programming.md) is concerned, besides others, with optimizing algorithms so that their time and/or space complexity is as low as possible which gives rise to algorithm design methods such as [dynamic programming](dynamic_programming.md) (practical [optimization](optimization.md) is a more pragmatic approach to making algorithms more efficient). [Formal verification](formal_verification.md) is a field that tries to mathematically (and sometimes automatically) prove correctness of algorithms (this is needed for critical software, e.g. in planes or medicine). [Genetic programming](generic_programming.md) and some other methods of [artificial intelligence](ai.md) try to automatically create algorithms (*algorithms that create algorithms*). [Quantum computing](quantum.md) is concerned with creating new kinds of algorithms for quantum computers (a new type of still-in-research computers). [Programming language](programming_language.md) design is the art and science of creating languages that express computer algorithms well. Etcetc.
|
||||
|
||||
## Specific Algorithms
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue