Consecutive primes with ascending or descending differences: Difference between revisions

→‎{{header|Lua}}: added Lua solution
(Python example)
(→‎{{header|Lua}}: added Lua solution)
Line 511:
Descending: [322171, 322193, 322213, 322229, 322237, 322243, 322247, 322249] Diffs: [22, 20, 16, 8, 6, 4, 2]
</pre>
 
=={{header|Lua}}==
This task uses <code>primegen</code> from: [[Extensible_prime_generator#Lua]]
<lang lua>function findcps(primelist, fcmp)
local currlist = {primelist[1]}
local longlist, prevdiff = currlist, 0
for i = 2, #primelist do
local diff = primelist[i] - primelist[i-1]
if fcmp(diff, prevdiff) then
currlist[#currlist+1] = primelist[i]
if #currlist > #longlist then
longlist = currlist
end
else
currlist = {primelist[i-1], primelist[i]}
end
prevdiff = diff
end
return longlist
end
 
primegen:generate(nil, 1000000)
cplist = findcps(primegen.primelist, function(a,b) return a>b end)
print("ASC ("..#cplist.."): ["..table.concat(cplist, " ").."]")
cplist = findcps(primegen.primelist, function(a,b) return a<b end)
print("DESC ("..#cplist.."): ["..table.concat(cplist, " ").."]")</lang>
{{out}}
<pre>ASC (8): [128981 128983 128987 128993 129001 129011 129023 129037]
DESC (8): [322171 322193 322213 322229 322237 322243 322247 322249]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Anonymous user