public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: Rolf Eike Beer <eike@sf-mail.de>
To: gentoo-dev@lists.gentoo.org
Subject: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime()
Date: Thu, 17 Jun 2021 17:17:17 +0200	[thread overview]
Message-ID: <11770691.O9o76ZdvQC@eto.sf-tec.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 1660 bytes --]

The previous algorithm would scan for all primes for a given number, which
takes needlessly long.

Signed-off-by: Rolf Eike Beer <eike@sf-mail.de>
---
 eclass/qmail.eclass | 45 ++++++++++++---------------------------------
 1 file changed, 12 insertions(+), 33 deletions(-)

diff --git a/eclass/qmail.eclass b/eclass/qmail.eclass
index f42f0491515..aaec205a6bd 100644
--- a/eclass/qmail.eclass
+++ b/eclass/qmail.eclass
@@ -20,46 +20,25 @@ GENQMAIL_S="${WORKDIR}"/genqmail-${GENQMAIL_PV}
 QMAIL_SPP_F=qmail-spp-${QMAIL_SPP_PV}.tar.gz
 QMAIL_SPP_S="${WORKDIR}"/qmail-spp-${QMAIL_SPP_PV}
 
-# @FUNCTION: primes
-# @USAGE: <min> <max>
-# @DESCRIPTION:
-# Prints a list of primes between min and max inclusive
-# Note: this functions gets very slow when used with large numbers.
-primes() {
-	local min=${1} max=${2}
-	local result= primelist=2 i p
-
-	[[ ${min} -le 2 ]] && result="${result} 2"
-
-	for ((i = 3; i <= max; i += 2))
-	do
-		for p in ${primelist}
-		do
-			[[ $[i % p] == 0 || $[p * p] -gt ${i} ]] && \
-				break
-		done
-		if [[ $[i % p] != 0 ]]
-		then
-			primelist="${primelist} ${i}"
-			[[ ${i} -ge ${min} ]] && \
-				result="${result} ${i}"
-		fi
-	done
-
-	echo ${result}
-}
-
-# @FUNCTION: is_prima
+# @FUNCTION: is_prime
 # @USAGE: <number>
 # @DESCRIPTION:
 # Checks wether a number is a prime number
 is_prime() {
 	local number=${1} i
-	for i in $(primes ${number} ${number})
+
+	if [[ $[number % 2] == 0 ]]; then
+		return 1
+	fi
+
+	for ((i = 3; i * i <= number; i += 2))
 	do
-		[[ ${i} == ${number} ]] && return 0
+		if [[ $[number % i ] == 0 ]]; then
+			return 1
+		fi
 	done
-	return 1
+
+	return 0
 }
 
 dospp() {
-- 
2.26.2


[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

             reply	other threads:[~2021-06-17 15:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-17 15:17 Rolf Eike Beer [this message]
2021-06-17 17:42 ` [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime() Peter Stuge
2021-06-17 20:27   ` Guilherme Amadio
2021-06-18 16:30     ` Ulrich Mueller
2021-06-18 19:10       ` Michael Orlitzky
2021-06-18 19:56         ` Rolf Eike Beer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11770691.O9o76ZdvQC@eto.sf-tec.de \
    --to=eike@sf-mail.de \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox