Cuban primes: Difference between revisions

→‎Snail Version: repaired copy and paste error
m (uncased various in sourcecode & output of C++, C#, & VB.NET)
(→‎Snail Version: repaired copy and paste error)
Line 725:
===Snail Version===
This one doesn't take any shortcuts. It could be sped up (Execution time about 15 seconds) by threading chunks of the search for the 100,000th cuban prime, but you would have to take a guess about how far to go, which would be hard-coded, so one might as well use the short-cut version if you are willing to overlook that difficulty.
<lang vbnet>'ImportsModule System.Threading.TasksProgram
 
'Module Program
 
' Dim primes As New List(Of Long)
' Dim ic As Long = 0
 
' Function tabulate(i As Long, amt As Integer) As Integer
' tabulate = 0
' For j As Long = i To i + amt - 1
' Dim v As Long = 3 * j : v = v * j + v + 1
' Dim found As Boolean = False, mx As Integer = Math.Ceiling(Math.Sqrt(v))
' For Each item In primes
' If item > mx Then Exit For
' If v Mod item = 0 Then found = True : Exit For
' Next : If Not found Then tabulate += 1
' Next : Console.Write(".")
' End Function
 
' Sub Main(args As String())
' Dim taskList As New List(Of Task(Of Integer))
' Const cutOff As Integer = 200, bigUn As Integer = 100000,
' chunks As Integer = 50
' Console.WriteLine("The first {0:n0} Cuban primes:", cutOff)
' primes.Add(3) : primes.Add(5)
' Dim c As Integer = 0, showEach As Boolean = True, para As Boolean = True
' Dim v As Long = 0
' Dim st As DateTime = DateTime.Now
' For i As Long = 1 To Long.MaxValue
' v = 3 * i : v = v * i + v + 1
' Dim found As Boolean = False, mx As Integer = Math.Ceiling(Math.Sqrt(v))
' For Each item In primes
' If item > mx Then Exit For
' If v Mod item = 0 Then found = True : Exit For
' Next : If Not found Then
' c += 1 : If showEach Then
' For z = primes.Last + 2 To v - 2 Step 2
' Dim fnd As Boolean = False
' For Each item In primes
' If item > mx Then Exit For
' If z Mod item = 0 Then fnd = True : Exit For
' Next : If Not fnd Then primes.Add(z)
' Next : primes.Add(v) : Console.Write("{0,11:n0}", v)
' If c Mod 10 = 0 Then Console.WriteLine()
' If c = cutOff Then showEach = False : _
' Console.Write("{0}Progress to the {1:n0}th Cuban prime: ", vbLf, bigUn)
' Else
' If para Then
' Const shortCutDistance As Integer = 770000
' Dim amt As Integer = shortCutDistance / chunks
' For ii As Integer = 0 To shortCutDistance - 1 Step amt
' Dim ti As Integer = If(ii = 0, i + 1, ii),
' ta As Integer = If(ii = 0, amt - i - 1, amt)
' taskList.Add(Task.Run(Function() tabulate(ti, ta)))
' Next
' Task.WhenAll(taskList)
' For Each item In taskList.Select(Function(t) t.Result)
' c += item : Next
' para = False : i = shortCutDistance - 1
' End If
' End If
' If c = bigUn Then Exit For
' End If
' Next
' Console.WriteLine("{1}The {2:n0}th Cuban prime is {0,17:n0}", v, vbLf, c)
' Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds)
' If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
' End Sub
 
'End Module
 
Module Program
Dim primes As List(Of Long) = {3L, 5L}.ToList()