From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 70DE8139360 for ; Thu, 12 Aug 2021 11:25:19 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 92B21E0A7C; Thu, 12 Aug 2021 11:25:15 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 3B832E0A64 for ; Thu, 12 Aug 2021 11:25:15 +0000 (UTC) From: Ulrich Mueller To: Rolf Eike Beer Cc: gentoo-dev@lists.gentoo.org Subject: Re: [gentoo-dev] [PATCH 3/5] qmail.eclass: simplify is_prime() In-Reply-To: <4644122.GXAFRqVoOG@eto.sf-tec.de> (Rolf Eike Beer's message of "Thu, 12 Aug 2021 11:55:41 +0200") References: <11806398.O9o76ZdvQC@eto.sf-tec.de> <4644122.GXAFRqVoOG@eto.sf-tec.de> Date: Thu, 12 Aug 2021 13:25:09 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" X-Archives-Salt: 1ad9e5fe-50a8-420e-9f5d-2512d6efaf97 X-Archives-Hash: 3547af4b675b40fcab46b626daa80ad2 --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable >>>>> On Thu, 12 Aug 2021, Rolf Eike Beer wrote: > -# @FUNCTION: primes > -# @USAGE: > +# @FUNCTION: is_prime > +# @USAGE: > # @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=3D${1} max=3D${2} > - local result=3D primelist=3D2 i p > +# Checks wether a number is a valid prime number for queue split > +is_prime() { > + local number=3D${1} i > + > + if [[ ${number} < 7 ]]; then > + # too small > + return 0 > + fi So e.g. all numbers between 100 and 699 qualify as primes? I doubt that this is what was intended. :) >=20=20 > - [[ ${min} -le 2 ]] && result=3D"${result} 2" > + if [[ $[number % 2] =3D=3D 0 ]]; then > + return 1 > + fi >=20=20 > - for ((i =3D 3; i <=3D max; i +=3D 2)) > + # let i run up to the square root of number > + for ((i =3D 3; i * i <=3D number; i +=3D 2)) > do > - for p in ${primelist} > - do > - [[ $[i % p] =3D=3D 0 || $[p * p] -gt ${i} ]] && \ > - break > - done > - if [[ $[i % p] !=3D 0 ]] > - then > - primelist=3D"${primelist} ${i}" > - [[ ${i} -ge ${min} ]] && \ > - result=3D"${result} ${i}" > + if [[ $[number % i ] =3D=3D 0 ]]; then > + return 1 > fi > done >=20=20 > - echo ${result} > -} This function asks for a unit test in eclass/tests/. Ulrich --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQFDBAEBCAAtFiEEtDnZ1O9xIP68rzDbUYgzUIhBXi4FAmEVBRUPHHVsbUBnZW50 b28ub3JnAAoJEFGIM1CIQV4urw4H/RXmz068l/6wqgHweZ1/WE3wFiyOClugYUgS 4YBvXZe8Id+iE5TX41yzR3orYiT2ORgMuY9HtbdEjLT1xt6aDaaV1dkE0zA5sngk p9Ct7nwXh9RHu4aXTaGYoi0MI/Od69CLEhLMAOmxRN5eKXu326x0Ddh/7JvZOpM0 6Su+7hHnniRwOoL6BvuLBu5HUKkyQaEgHs31Utz3YRDV2CMsfctj3RzxpCjHpr/s jBaYRVuGVXmr8N9iDr95VGxl+P74bhn31wdtRh0PrCaOIzG6ymJ+ytS3uUTGphoH 7Ez/EcJeeq4q/Oc2xEGLWuBZACaLeoK667nMwyvYDbgQoLJEO78= =YSoj -----END PGP SIGNATURE----- --=-=-=--