

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.


-- Return a table of the primes up to n, then one more
function primeList (n)
  local function isPrime (x)
    for d = 3, math.sqrt(x), 2 do
      if x % d == 0 then return false end
    end
    return true
  end
  local pTable, j = {2, 3}
  for i = 5, n, 2 do
    if isPrime(i) then
      table.insert(pTable, i)
    end
    j = i
  end
  repeat j = j + 2 until isPrime(j)
  table.insert(pTable, j)
  return pTable
end
 
-- Return a boolean indicating whether prime p is strong
function isStrong (p)
  if p == 1 or p == #prime then return false end
  return prime[p] > (prime[p-1] + prime[p+1]) / 2 
end
 
-- Return a boolean indicating whether prime p is weak
function isWeak (p)
  if p == 1 or p == #prime then return false end
  return prime[p] < (prime[p-1] + prime[p+1]) / 2 
end
 
-- Main procedure
prime = primeList(1e7)
local strong, weak, sCount, wCount = {}, {}, 0, 0
for k, v in pairs(prime) do
  if isStrong(k) then
    table.insert(strong, v)
    if v < 1e6 then sCount = sCount + 1 end
  end
  if isWeak(k) then
    table.insert(weak, v)
    if v < 1e6 then wCount = wCount + 1 end
  end
end
print("The first 36 strong primes are:")
for i = 1, 36 do io.write(strong[i] .. " ") end
print("\n\nThere are " .. sCount .. " strong primes below one million.")
print("\nThere are " .. #strong .. " strong primes below ten million.")
print("\nThe first 37 weak primes are:")
for i = 1, 37 do io.write(weak[i] .. " ") end
print("\n\nThere are " .. wCount .. " weak primes below one million.")
print("\nThere are " .. #weak .. " weak primes below ten million.")