

Definitions   (as per number theory)

   The   prime(p)   is the    pth   prime.
   prime(1)   is   2
   prime(4)   is   7
   A    strong   prime   is when     prime(p)   is   >   [prime(p-1) + prime(p+1)] ÷ 2
   A     weak    prime   is when     prime(p)   is   <   [prime(p-1) + prime(p+1)] ÷ 2


Note that the definition for   strong primes   is different when used in the context of   cryptography.



Task

   Find and display (on one line) the first   36   strong primes.
   Find and display the   count   of the strong primes below   1,000,000.
   Find and display the   count   of the strong primes below 10,000,000.
   Find and display (on one line) the first   37   weak primes.
   Find and display the   count   of the weak primes below   1,000,000.
   Find and display the   count   of the weak primes below 10,000,000.
   (Optional)   display the   counts   and   "below numbers"   with commas.
Show all output here.



Related Task

   Safe primes and unsafe primes.


Also see

   The OEIS article A051634: strong primes.
   The OEIS article A051635: weak primes.


 
#include "isprime.bas"
 
function nextprime( n as uinteger ) as uinteger
    'finds the next prime after n, excluding n if it happens to be prime itself
    if n = 0 then return 2
    if n < 3 then return n + 1
    dim as integer q = n + 2
    while not isprime(q)
        q+=2
    wend
    return q
end function
 
function lastprime( n as uinteger ) as uinteger
    'finds the last prime before n, excluding n if it happens to be prime itself
    if n = 2 then return 0       'zero isn't prime, but it is a good sentinel value :)
    if n = 3 then return 2
    dim as integer q = n - 2
    while not isprime(q)
        q-=2
    wend
    return q
end function
 
function isstrong( p as integer ) as boolean
    if nextprime(p) + lastprime(p) >= 2*p then return false else return true
end function
 
function isweak( p as integer ) as boolean
    if nextprime(p) + lastprime(p) <= 2*p then return false else return true
end function
 
print "The first 36 strong primes are: "
dim as uinteger c, p=3
while p < 10000000
    if isprime(p) andalso isstrong(p) then 
        c += 1
        if c <= 36 then print p;" ";
        if c=37 then print
    end if
    if p = 1000001 then print "There are ";c;" strong primes below one million" 
    p+=2
wend
print "There are ";c;" strong primes below ten million"
print
 
print "The first 37 weak primes are: "
p=3 : c=0
while p < 10000000
    if isprime(p) andalso isweak(p) then 
        c += 1
        if c <= 37 then print p;" ";
        if c=38 then print
    end if
 
    if p = 1000001 then print "There are ";c;" weak primes below one million" 
    p+=2
wend
print "There are ";c;" weak primes below ten million"
print