public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: Ulrich Mueller <ulm@gentoo.org>
To: Guilherme Amadio <amadio@gentoo.org>
Cc: gentoo-dev@lists.gentoo.org
Subject: Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime()
Date: Fri, 18 Jun 2021 18:30:05 +0200	[thread overview]
Message-ID: <uv96bi19u@gentoo.org> (raw)
In-Reply-To: <YMuwPrwX/KW0CnJu@gentoo.org> (Guilherme Amadio's message of "Thu, 17 Jun 2021 22:27:42 +0200")

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

>>>>> 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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 507 bytes --]

  reply	other threads:[~2021-06-18 16:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-17 15:17 [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime() Rolf Eike Beer
2021-06-17 17:42 ` Peter Stuge
2021-06-17 20:27   ` Guilherme Amadio
2021-06-18 16:30     ` Ulrich Mueller [this message]
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=uv96bi19u@gentoo.org \
    --to=ulm@gentoo.org \
    --cc=amadio@gentoo.org \
    --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