Strong and weak primes: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 1,082: Line 1,082:
Number of weak primes below 1,000,000 = 37,780
Number of weak primes below 1,000,000 = 37,780
Number of weak primes below 10,000,000 = 321,750
Number of weak primes below 10,000,000 = 321,750
</pre>

=={{header|jq}}==
{{Works with|jq}}
'''Also works with gojq, the Go implementation of jq'''

The following assumes that `primes` generates a stream of primes less
than or equal to `.`
as defined, for example, at [[Sieve_of_Eratosthenes#jq|Sieve of Eratosthenes]]]].

<syntaxhighlight lang=jq>
def count(s): reduce s as $_ (0; .+1);

# Emit {strong, weak} primes up to and including $n
def strong_weak_primes:
. as $n
| primes as $primes
| ("\nCheck: last prime generated: \($primes[-1])" | debug) as $debug
| reduce range(1; $primes|length-1) as $p ({};
(($primes[$p-1] + $primes[$p+1]) / 2) as $x
| if $primes[$p] > $x
then .strong += [$primes[$p]]
elif $primes[$p] < $x
then .weak += [$primes[$p]]
else .
end );

(1e7 + 19)
| strong_weak_primes as {$strong, $weak}
| "The first 36 strong primes are:",
$strong[:36],
"\nThe count of the strong primes below 1e6: \(count($strong[]|select(. < 1e6 )))",
"\nThe count of the strong primes below 1e7: \(count($strong[]|select(. < 1e7 )))",

"\nThe first 37 weak primes are:",
$weak[:37],
"\nThe count of the weak primes below 1e6: \(count($weak[]|select(. < 1e6 )))",
"\nThe count of the weak primes below 1e7: \(count($weak[]|select(. < 1e7 )))"
</syntaxhighlight>
{{output}}
<pre>
The first 36 strong primes are:
[11,17,29,37,41,59,67,71,79,97,101,107,127,137,149,163,179,191,197,223,227,239,251,269,277,281,307,311,331,347,367,379,397,419,431,439]

The count of the strong primes below 1e6: 37723

The count of the strong primes below 1e7: 320991

The first 37 weak primes are:
[3,7,13,19,23,31,43,47,61,73,83,89,103,109,113,131,139,151,167,181,193,199,229,233,241,271,283,293,313,317,337,349,353,359,383,389,401]

The count of the weak primes below 1e6: 37780

The count of the weak primes below 1e7: 321750
</pre>
</pre>