Cuban primes: Difference between revisions
Content added Content deleted
Line 1,358: | Line 1,358: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
===Original=== |
|||
{{trans|D}} |
{{trans|D}} |
||
<lang lua>local primes = {3, 5} |
<lang lua>local primes = {3, 5} |
||
Line 1,375: | Line 1,376: | ||
v = v + u |
v = v + u |
||
local mx = math.ceil(math.sqrt(v)) |
local mx = math.ceil(math.sqrt(v)) |
||
--for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance) |
--for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance), 6/11/2020 db |
||
for _,item in ipairs(primes) do |
for _,item in ipairs(primes) do |
||
if item > mx then |
if item > mx then |
||
Line 1,390: | Line 1,391: | ||
c = c + 1 |
c = c + 1 |
||
if showEach then |
if showEach then |
||
--local z = primes[table.getn(primes)] + 2 -- why: modernize (deprecated) |
--local z = primes[table.getn(primes)] + 2 -- why: modernize (deprecated), 6/11/2020 db |
||
local z = primes[#primes] + 2 |
local z = primes[#primes] + 2 |
||
while z <= v - 2 do |
while z <= v - 2 do |
||
local fnd = false |
local fnd = false |
||
--for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance) |
--for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance), 6/11/2020 db |
||
for _,item in ipairs(primes) do |
for _,item in ipairs(primes) do |
||
if item > mx then |
if item > mx then |
||
Line 1,427: | Line 1,428: | ||
end |
end |
||
end |
end |
||
--print(string.format("\nThe %dth%s is %17d", c, tn, v)) -- why: correcting reported inaccuracy in output |
--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))</lang> |
print(string.format("\nThe %dth%s is %.0f", c, tn, v))</lang> |
||
{{out}} |
{{out}} |
||
Line 1,454: | Line 1,455: | ||
Progress to the 100000th cuban prime: .................................................. |
Progress to the 100000th cuban prime: .................................................. |
||
The 100000th cuban prime is 1792617147127</pre> |
The 100000th cuban prime is 1792617147127</pre> |
||
===Alternate=== |
|||
Perhaps a more "readable" structure, and with specified formatting.. |
|||
<lang lua>-- cuban primes in Lua (alternate version 6/12/2020 db) |
|||
------------------ |
|||
-- PRIME SUPPORT: |
|||
------------------ |
|||
local sqrt, sieve, primes, N = math.sqrt, {false}, {}, 1400000 |
|||
for i = 2,N do sieve[i]=true end |
|||
for i = 2,N do if sieve[i] then for j=i*i,N,i do sieve[j]=false end end end |
|||
for i = 2,N do if sieve[i] then primes[#primes+1]=i end end; sieve=nil |
|||
local function isprime(n) |
|||
if (n <= 1) then return false end |
|||
local limit = sqrt(n) |
|||
for i,p in ipairs(primes) do |
|||
if (n % p == 0) then return false end |
|||
if (p > limit) then return true end |
|||
end |
|||
error("insufficient list of primes") |
|||
end |
|||
------------------ |
|||
-- PRINT SUPPORT: |
|||
------------------ |
|||
local write, format = io.write, string.format |
|||
local function commafy(i) return tostring(i):reverse():gsub("(%d%d%d)","%1,"):reverse():gsub("^,","") end |
|||
---------------- |
|||
-- ACTUAL TASK: |
|||
---------------- |
|||
local COUNT, DOTAT, DOTPER, count, n = 100000, 200, 2000, 0, 0 |
|||
while (count < COUNT) do |
|||
local h = 3 * n * (n + 1) + 1 |
|||
if (isprime(h)) then |
|||
count = count + 1 |
|||
if (count <= DOTAT) then |
|||
write(format("%11s%s", commafy(h), count%10==0 and "\n" or "")) |
|||
elseif (count == COUNT) then |
|||
print(format("\n%s", commafy(h))) |
|||
elseif (count % DOTPER == 0) then |
|||
write(".") |
|||
end |
|||
end |
|||
n = n + 1 |
|||
end |
|||
</lang> |
|||
{{out}} |
|||
<pre> 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 |
|||
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719 |
|||
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117 |
|||
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897 |
|||
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211 |
|||
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661 |
|||
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269 |
|||
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019 |
|||
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669 |
|||
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919 |
|||
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001 |
|||
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219 |
|||
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071 |
|||
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627 |
|||
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177 |
|||
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411 |
|||
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471 |
|||
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671 |
|||
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 |
|||
................................................. |
|||
1,792,617,147,127</pre> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |