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 |