The name   cuban   has nothing to do with   Cuba  (the country),   but has to do with the
fact that cubes   (3rd powers)   play a role in its definition.



Some definitions of cuban primes

   primes which are the difference of two consecutive cubes.
   primes of the form:   (n+1)3 - n3.
   primes of the form:   n3 - (n-1)3.
   primes   p   such that   n2(p+n)   is a cube for some   n>0.
   primes   p   such that   4p = 1 + 3n2.


Cuban primes were named in 1923 by Allan Joseph Champneys Cunningham.



Task requirements

   show the first   200   cuban primes   (in a multi─line horizontal format).
   show the   100,000th   cuban prime.
   show all cuban primes with commas   (if appropriate).
   show all output here.


Note that   cuban prime   isn't capitalized   (as it doesn't refer to the nation of Cuba).



Also see

   Wikipedia entry:     cuban prime.
   MathWorld entry:   cuban prime.
   The OEIS entry:     A002407.     The   100,000th   cuban prime can be verified in the   2nd   example   on this OEIS web page.


local primes = {3, 5}
local cutOff = 200
local bigUn = 100000
local chunks = 50
local little = math.floor(bigUn / chunks)
local tn = " cuban prime"
print(string.format("The first %d%ss", cutOff, tn))
local showEach = true
local c = 0
local u = 0
local v = 1
for i=1,10000000000000 do
    local found = false
    u = u + 6
    v = v + u
    local mx = math.ceil(math.sqrt(v))
    --for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance), 6/11/2020 db
    for _,item in ipairs(primes) do
        if item > mx then
            break
        end
        if v % item == 0 then
            --print("[DEBUG] :( i = " .. i .. "; v = " .. v)
            found = true
            break
        end
    end
    if not found then
        --print("[DEBUG] :) i = " .. i .. "; v = " .. v)
        c = c + 1
        if showEach then
            --local z = primes[table.getn(primes)] + 2 -- why: modernize (deprecated), 6/11/2020 db
            local z = primes[#primes] + 2
            while z <= v - 2 do
                local fnd = false
                --for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance), 6/11/2020 db
                for _,item in ipairs(primes) do
                    if item > mx then
                        break
                    end
                    if z % item == 0 then
                        fnd = true
                        break
                    end
                end
                if not fnd then
                    table.insert(primes, z)
                end
                z = z + 2
            end
            table.insert(primes, v)
            io.write(string.format("%11d", v))
            if c % 10 == 0 then
                print()
            end
            if c == cutOff then
                showEach = false
                io.write(string.format("\nProgress to the %dth%s: ", bigUn, tn))
            end
        end
        if c % little == 0 then
            io.write(".")
            if c == bigUn then
                break
            end
        end
    end
end
--print(string.format("\nThe %dth%s is %17d", c, tn, v)) -- why: correcting reported inaccuracy in output, 6/11/2020 db
print(string.format("\nThe %dth%s is %.0f", c, tn, v))