initial commit
This commit is contained in:
76
bash/sieve/README.md
Normal file
76
bash/sieve/README.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# Sieve
|
||||
|
||||
Use the Sieve of Eratosthenes to find all the primes from 2 up to a given
|
||||
number.
|
||||
|
||||
The Sieve of Eratosthenes is a simple, ancient algorithm for finding all
|
||||
prime numbers up to any given limit. It does so by iteratively marking as
|
||||
composite (i.e. not prime) the multiples of each prime, starting with the
|
||||
multiples of 2. It does not use any division or remainder operation.
|
||||
|
||||
Create your range, starting at two and continuing up to and including the given limit. (i.e. [2, limit])
|
||||
|
||||
The algorithm consists of repeating the following over and over:
|
||||
|
||||
- take the next available unmarked number in your list (it is prime)
|
||||
- mark all the multiples of that number (they are not prime)
|
||||
|
||||
Repeat until you have processed each number in your range.
|
||||
|
||||
When the algorithm terminates, all the numbers in the list that have not
|
||||
been marked are prime.
|
||||
|
||||
The wikipedia article has a useful graphic that explains the algorithm:
|
||||
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
||||
|
||||
Notice that this is a very specific algorithm, and the tests don't check
|
||||
that you've implemented the algorithm, only that you've come up with the
|
||||
correct list of primes. A good first test is to check that you do not use
|
||||
division or remainder operations (div, /, mod or % depending on the
|
||||
language).
|
||||
|
||||
|
||||
Run the tests with:
|
||||
|
||||
```bash
|
||||
bats sieve_test.sh
|
||||
```
|
||||
|
||||
After the first test(s) pass, continue by commenting out or removing the
|
||||
`[[ $BATS_RUN_SKIPPED == true ]] || skip`
|
||||
annotations prepending other tests.
|
||||
|
||||
To run all tests, including the ones with `skip` annotations, run:
|
||||
|
||||
```bash
|
||||
BATS_RUN_SKIPPED=true bats sieve_test.sh
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
Sieve of Eratosthenes at Wikipedia [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
|
||||
|
||||
|
||||
## External utilities
|
||||
`Bash` is a language to write "scripts" -- programs that can call
|
||||
external tools, such as
|
||||
[`sed`](https://www.gnu.org/software/sed/),
|
||||
[`awk`](https://www.gnu.org/software/gawk/),
|
||||
[`date`](https://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html)
|
||||
and even programs written in other programming languages,
|
||||
like [`Python`](https://www.python.org/).
|
||||
This track does not restrict the usage of these utilities, and as long
|
||||
as your solution is portable between systems and does not require
|
||||
installation of third party applications, feel free to use them to solve
|
||||
the exercise.
|
||||
|
||||
For an extra challenge, if you would like to have a better understanding
|
||||
of the language, try to re-implement the solution in pure `Bash`,
|
||||
without using any external tools. Note that there are some types of
|
||||
problems that bash cannot solve, such as performing floating point
|
||||
arithmetic and manipulating dates: for those, you must call out to an
|
||||
external tool.
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others
|
||||
have completed the exercise.
|
24
bash/sieve/sieve.sh
Executable file
24
bash/sieve/sieve.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
usage() {
|
||||
echo "Usage: ./seave.sh <number>"
|
||||
exit 1
|
||||
}
|
||||
end=$1
|
||||
|
||||
[[ ! "$end" =~ ^[[:digit:]]*$ ]] && usage
|
||||
|
||||
declare -a numbers
|
||||
|
||||
sep=""
|
||||
for (( i=2; i<=end; ++i )); do
|
||||
if [[ ${numbers[$i]} == "" ]]; then
|
||||
echo -n "$sep$i"
|
||||
for (( j=i*2; j<=end; j+=i )); do
|
||||
numbers[$j]=t
|
||||
done
|
||||
sep=" "
|
||||
fi
|
||||
done
|
||||
echo
|
||||
exit 0
|
Reference in New Issue
Block a user