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 302DF1382C5 for ; Fri, 18 Jun 2021 16:30:22 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 17D8AE0893; Fri, 18 Jun 2021 16:30:18 +0000 (UTC) Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (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 B547BE0869 for ; Fri, 18 Jun 2021 16:30:17 +0000 (UTC) From: Ulrich Mueller To: Guilherme Amadio Cc: gentoo-dev@lists.gentoo.org Subject: Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime() In-Reply-To: (Guilherme Amadio's message of "Thu, 17 Jun 2021 22:27:42 +0200") References: <11770691.O9o76ZdvQC@eto.sf-tec.de> <20210617174213.29826.qmail@stuge.se> Date: Fri, 18 Jun 2021 18:30:05 +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: e7d04178-023d-47da-97d1-2d05633b6ea6 X-Archives-Hash: 1a76bd9a84e11d1d3c79e415422b0b70 --=-=-= Content-Type: text/plain >>>>> On Thu, 17 Jun 2021, Guilherme Amadio wrote: > There's actually a much simpler solution to this: > $ is_prime() { test $(factor $1 | cut -d: -f2 | wc -w) == 1; } > $ for n in $(seq 0 10); do is_prime $n && echo $n is prime; done > 2 is prime > 3 is prime > 5 is prime > 7 is prime > $ time factor 20187319083467888113 > 20187319083467888113: 20187319083467888113 > 0.00 This depends on the actual domain of numbers. If the primes involved have 20 digits as in your example, then factor should be used of course. I suspect though that we're talking about small numbers (below 100?) here, in which case a solution in pure bash would be preferable. Ulrich --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQFDBAEBCAAtFiEEtDnZ1O9xIP68rzDbUYgzUIhBXi4FAmDMyg0PHHVsbUBnZW50 b28ub3JnAAoJEFGIM1CIQV4uLWoIAIjo1XqSC6+Hm4LqEtA2IU5fS5Takmm5cIul 1tnZEyOWzjME/XCz64UKHPC8FdzX6FIWiF6cfgHMjPn5QscAD0yXuTfB4PEt8Tvg Oce3iaRaksQFSJPNbzHopcrT5MuN+I3HCGYUHVcqdeBzM5VVpTynWB6MPyxGYtz7 AHsIZ9yMn+JL4YcGNMdtvlRb8e4FSbzEB+y3QXt6dvKvz6zR42+L44h6VCkiHIWx 8KObXA/WbS8708yl0eji1e1Vm19D+x1L+jhZvfyV6ldLO2Xhgmt2/3DHVX7QSkVT 3A7VHKwC8B/RLL29KPWsh8517BLMWnszVeDUGtYD6mJqtpzjAA0= =XAwy -----END PGP SIGNATURE----- --=-=-=--