Cuban primes: Difference between revisions

m
uncased various in sourcecode & output of C++, C#, & VB.NET
m (changed some spellings of "cuban" (should be lower case) in some comments (but not any programs)..)
m (uncased various in sourcecode & output of C++, C#, & VB.NET)
Line 38:
#include <climits>
#include <cmath>
 
using namespace std;
 
vector <long long> primes{ 3, 5 };
 
int main()
{
cout.imbue(locale(""));
const int cutOff = 200;, bigUn = 100000,
const int chunks = 50, little = bigUn / chunks;
const int bigUn = 100000;
const intchar chunkstn[] = 50" cuban prime";
cout << "The first " << cutOff << "tn Cuban<< primes"s:\n" << endl;
const int little = bigUn / chunks;
cout << "The first " << cutOff << " Cuban primes:\n";
primes.push_back(3); primes.push_back(5);
int c = 0;
bool showEach = true;
long long vu = 0, v = 1;
auto st = chrono::system_clock::now();
 
for (long long i = 1; i <= LLONG_MAX; i++)
{
v = 3 * i; v = v * i + v + 1;
bool found = false;
intlong long mx = (long long)(ceil(sqrt(v += (u += 6))));
for (long long item : primes)
{
if (item > mx) break;
Line 81 ⟶ 78:
if (!fnd) primes.push_back(z);
}
primes.push_back(v); std::cout.width(11); cout << v;
if (c % 10 == 0) cout << '\n'endl;
if (c == cutOff)
{
showEach = false;
cout << "\nProgress to the " << bigUn << "th" Cuban<< primetn << ": ";
}
}
Line 92 ⟶ 89:
}
}
std::cout << "\nThe " << c << "th" Cuban<< primetn << " is " << v;
autochrono::duration<double> endelapsed_seconds = chrono::system_clock::now() - st;
chrono::duration<double> elapsed_seconds = end - st;
cout << "\nComputation time was " << elapsed_seconds.count() << " seconds" << endl;
return 0;
}</lang>
{{out}}
<pre>The first 200 Cubancuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
Line 121 ⟶ 117:
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
Progress to the 100,000th Cubancuban prime: ..................................................
The 100,000th Cubancuban prime is 1,792,617,147,127
Computation time was 4435.31735644 seconds</pre>
 
=={{header|C#}}==
Line 134 ⟶ 130:
static class Program
{
static List<long> primes = new List<long>() { 3, 5 };
 
static void Main(string[] args)
Line 142 ⟶ 138:
const int chunks = 50;
const int little = bigUn / chunks;
Console.WriteLine("Theconst firststring {0:n0}tn Cuban= primes:", cutOff)cuban prime";
primesConsole.AddWriteLine(3);"The primes.Add(5first {0:n0}{1}s:", cutOff, tn);
int c = 0;
bool showEach = true;
long vu = 0, v = 1;
DateTime st = DateTime.Now;
for (long i = 1; i <= long.MaxValue; i++)
{
v = 3 * i; v = v * i + v + 1;
bool found = false;
int mx = System.Convert.ToInt32(Math.Ceiling(Math.Sqrt(v += (u += 6))));
foreach (varlong item in primes)
{
if (item > mx) break;
Line 165 ⟶ 160:
{
bool fnd = false;
foreach (varlong item in primes)
{
if (item > mx) break;
Line 177 ⟶ 172:
{
showEach = false;
Console.Write("\nProgress to the {0:n0}th Cuban prime{1}: ", bigUn, tn);
}
}
Line 183 ⟶ 178:
}
}
Console.WriteLine("\nThe {1:n0}th Cuban prime{2} is {0,17:n0}", v, c, tn);
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds);
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
Line 189 ⟶ 184:
}</lang>
{{out}}
<pre>The first 200 Cubancuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
Line 211 ⟶ 206:
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
Progress to the 100,000th Cubancuban prime: ..................................................
The 100,000th Cubancuban prime is 1,792,617,147,127
Computation time was 6663.5824166578673 seconds</pre>
 
=={{header|Go}}==
Line 665 ⟶ 660:
=={{header|Visual Basic .NET}}==
===Corner Cutting Version===
This language doesn't have a built-in for a ''IsPrime()'' function, so I was surprised to find that this runs so quickly. It builds a list of primes while it is creating the output table. Since the last item on the table is larger than the square root of the 100,000th cuban prime, there is no need to continue adding to the prime list while checking up to the 100,000th cuban prime. I found a bit of a shortcut, if you skip the iterator by just the right amount, only one value is tested for the final result. It's hard-coded in the program, so if another final cuban prime were to be selected for output, the program would need a re-write. If not skipping ahead to the answer, it takes a few seconds over a minute to eventually get to it (see Snail Version below).
<lang vbnet>Module Module1
Dim primes As New List(Of Long) = {3L, 5L}.ToList()
 
Sub Main(args As String())
Const cutOff As Integer = 200, bigUn As Integer = 100000,
tn As String = " cuban prime"
primes.Add(3) : primes.Add(5)
Console.WriteLine("The first {0:n0} Cuban primes{1}s:", cutOff, tn)
Dim c As Integer = 0, showEach As Boolean = True, skip As Boolean = True,
v As Long = 0, st As DateTime = DateTime.Now
Line 698 ⟶ 693:
End If
Next
Console.WriteLine("{1}The {2:n0}th Cuban prime{3} is {0,17:n0}", v, vbLf, c, tn)
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds)
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
Line 704 ⟶ 699:
End Module</lang>
{{out}}
<pre>The first 200 Cubancuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
Line 726 ⟶ 721:
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th Cubancuban prime is 1,792,617,147,127
Computation time was 0.29680642989494 seconds</pre>
===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>Module'Imports ProgramSystem.Threading.Tasks
 
Dim primes As New List(Of Long)
'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 * i;j : v = v * ij + 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.push_backAdd(3); : primes.push_backAdd(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()
 
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, little As Integer = bigUn / chunks,
tn As String = " cuban prime"
Console.WriteLine("The first {0:n0} Cuban primes:", cutOff)
primesConsole.AddWriteLine(3)"The first {0:n0}{1}s:", primes.Add(5cutOff, tn)
Dim c As Integer = 0, showEach As Boolean = True,
Dim v u As Long = 0, v As Long = 1,
Dim st As DateTime = DateTime.Now
For i As Long = 1 To Long.MaxValue
vu += 3 * i6 : v += v * i + v + 1u
Dim found As Boolean = False, mx As Integer = Math.Ceiling(Math.Sqrt(v))
For Each item In primes
Line 759 ⟶ 825:
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{2}: ", vbLf, bigUn, tn)
End If
If c Mod little = 0 Then Console.Write(".") : If c = bigUn Then Exit For
End If
Next
Console.WriteLine("{1}The {2:n0}th Cuban prime{3} is {0,17:n0}", v, vbLf, c, tn)
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds)
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
Line 770 ⟶ 836:
End Module</lang>
{{out}}
<pre>The first 200 Cubancuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
Line 792 ⟶ 858:
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
Progress to the 100,000th Cubancuban prime: ..................................................
The 100,000th Cubancuban prime is 1,792,617,147,127
Computation time was 6449.54893125868152 seconds</pre>