Rolf Eike Beer wrote:
> The previous algorithm would scan for all primes for a given number,
> which takes needlessly long.
Please also mention how this caused a problem for you in the commit
message, to help us understand why you're proposing to change this.
> +++ b/eclass/qmail.eclass
> -# @FUNCTION: is_prima
> +# @FUNCTION: is_prime
> # @USAGE: <number>
> # @DESCRIPTION:
> # Checks wether a number is a prime number
Maybe name the algorithm? Looks like an optimization of the sieve of
Eratosthenes?
> is_prime() {
> local number=${1} i
> - for i in $(primes ${number} ${number})
> +
> + if [[ $[number % 2] == 0 ]]; then
> + return 1
> + fi
While 2 itself is prime the above returns 1 for any even number.
> + for ((i = 3; i * i <= number; i += 2))
> do
> - [[ ${i} == ${number} ]] && return 0
> + if [[ $[number % i ] == 0 ]]; then
An inconsistent space after "% i" here. I don't know what style is correct.
//Peter