Gentoo Archives: gentoo-dev

From: Peter Stuge <peter@×××××.se>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime()
Date: Thu, 17 Jun 2021 17:42:21
Message-Id: 20210617174213.29826.qmail@stuge.se
In Reply to: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime() by Rolf Eike Beer
1 Rolf Eike Beer wrote:
2 > The previous algorithm would scan for all primes for a given number,
3 > which takes needlessly long.
4
5 Please also mention how this caused a problem for you in the commit
6 message, to help us understand why you're proposing to change this.
7
8
9 > +++ b/eclass/qmail.eclass
10 > -# @FUNCTION: is_prima
11 > +# @FUNCTION: is_prime
12 > # @USAGE: <number>
13 > # @DESCRIPTION:
14 > # Checks wether a number is a prime number
15
16 Maybe name the algorithm? Looks like an optimization of the sieve of
17 Eratosthenes?
18
19
20 > is_prime() {
21 > local number=${1} i
22 > - for i in $(primes ${number} ${number})
23 > +
24 > + if [[ $[number % 2] == 0 ]]; then
25 > + return 1
26 > + fi
27
28 While 2 itself is prime the above returns 1 for any even number.
29
30
31 > + for ((i = 3; i * i <= number; i += 2))
32 > do
33 > - [[ ${i} == ${number} ]] && return 0
34 > + if [[ $[number % i ] == 0 ]]; then
35
36 An inconsistent space after "% i" here. I don't know what style is correct.
37
38
39 //Peter

Replies

Subject Author
Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime() Guilherme Amadio <amadio@g.o>