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.


import strformat
import strutils
import math
 
const cutOff = 200
const bigUn = 100000
const chunks = 50
const little = bigUn div chunks
 
echo fmt"The first {cutOff} cuban primes"
var primes: seq[int] = @[3, 5]
var c, u = 0
var showEach: bool = true
var v = 1
for i in 1..high(BiggestInt):
  var found: bool
  inc u, 6
  inc v, u
  var mx = int(ceil(sqrt(float(v))))
  for item in primes:
    if item > mx:
      break
    if v mod item == 0:
      found = true
      break
  if not found:
    inc c
    if showEach:
      for z in countup(primes[^1] + 2, v - 2, step=2):
        var fnd: bool = false
        for item in primes:
          if item > mx:
            break
          if z mod item == 0:
            fnd = true
            break
        if not fnd:
          primes.add(z)
      primes.add(v)
      write(stdout, fmt"{insertSep($v, ','):>11}")
      if c mod 10 == 0:
        write(stdout, "\n")
      if c == cutOff:
        showEach = false
        write(stdout, fmt"Progress to the {bigUn}th cuban prime: ")
        stdout.flushFile
    if c mod little == 0:
      write(stdout, ".")
      stdout.flushFile
      if c == bigUn:
        break
write(stdout, "\n")
echo fmt"The {c}th cuban prime is {insertSep($v, ',')}"