Gentoo Archives: gentoo-dev

From: Ulrich Mueller <ulm@g.o>
To: Guilherme Amadio <amadio@g.o>
Cc: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime()
Date: Fri, 18 Jun 2021 16:30:22
Message-Id: uv96bi19u@gentoo.org
In Reply to: Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime() by Guilherme Amadio
1 >>>>> On Thu, 17 Jun 2021, Guilherme Amadio wrote:
2
3 > There's actually a much simpler solution to this:
4
5 > $ is_prime() { test $(factor $1 | cut -d: -f2 | wc -w) == 1; }
6 > $ for n in $(seq 0 10); do is_prime $n && echo $n is prime; done
7 > 2 is prime
8 > 3 is prime
9 > 5 is prime
10 > 7 is prime
11 > $ time factor 20187319083467888113
12 > 20187319083467888113: 20187319083467888113
13 > 0.00
14
15 This depends on the actual domain of numbers. If the primes involved
16 have 20 digits as in your example, then factor should be used of course.
17
18 I suspect though that we're talking about small numbers (below 100?)
19 here, in which case a solution in pure bash would be preferable.
20
21 Ulrich

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies

Subject Author
Re: [gentoo-dev] [PATCH] qmail.eclass: simplify is_prime() Michael Orlitzky <mjo@g.o>