Jump to content

Successive prime differences: Difference between revisions

→‎{{header|Lua}}: added Lua solution
(→‎{{header|Lua}}: added Lua solution)
Line 1,409:
[6, 4, 2] 306 [31, 37, 41, 43]...[997141, 997147, 997151, 997153]
</pre>
 
=={{header|Lua}}==
This task uses <code>primegen</code> from: [[Extensible_prime_generator#Lua]]
<lang lua>function findspds(primelist, diffs)
local results = {}
for i = 1, #primelist-#diffs do
result = {primelist[i]}
for j = 1, #diffs do
if primelist[i+j] - primelist[i+j-1] == diffs[j] then
result[j+1] = primelist[i+j]
else
result = nil
break
end
end
results[#results+1] = result
end
return results
end
 
primegen:generate(nil, 1000000)
for _,diffs in ipairs{{2}, {1}, {2,2}, {2,4}, {4,2}, {6,4,2}} do
spdlist = findspds(primegen.primelist, diffs)
print("DIFFS: ["..table.concat(diffs," ").."]")
print("COUNT: "..#spdlist)
print("FIRST: ["..table.concat(spdlist[1]," ").."]")
print("LAST : ["..table.concat(spdlist[#spdlist]," ").."]")
print()
end</lang>
{{out}}
<pre>DIFFS: [2]
COUNT: 8169
FIRST: [3 5]
LAST : [999959 999961]
 
DIFFS: [1]
COUNT: 1
FIRST: [2 3]
LAST : [2 3]
 
DIFFS: [2 2]
COUNT: 1
FIRST: [3 5 7]
LAST : [3 5 7]
 
DIFFS: [2 4]
COUNT: 1393
FIRST: [5 7 11]
LAST : [999431 999433 999437]
 
DIFFS: [4 2]
COUNT: 1444
FIRST: [7 11 13]
LAST : [997807 997811 997813]
 
DIFFS: [6 4 2]
COUNT: 306
FIRST: [31 37 41 43]
LAST : [997141 997147 997151 997153]</pre>
 
=={{header|Kotlin}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.