Descending primes: Difference between revisions
Content added Content deleted
(→{{header|Lua}}: added Lua solution) |
|||
Line 412: | Line 412: | ||
8764321 8765321 9754321 9875321 97654321 98764321 98765431 |
8764321 8765321 9754321 9875321 97654321 98764321 98765431 |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
|||
Identical to [[Ascending_primes#Lua]] except for the order of <code>digits</code> list. |
|||
<lang lua>local function is_prime(n) |
|||
if n < 2 then return false end |
|||
if n % 2 == 0 then return n==2 end |
|||
if n % 3 == 0 then return n==3 end |
|||
for f = 5, n^0.5, 6 do |
|||
if n%f==0 or n%(f+2)==0 then return false end |
|||
end |
|||
return true |
|||
end |
|||
local function descending_primes() |
|||
local digits, candidates, primes = {9,8,7,6,5,4,3,2,1}, {0}, {} |
|||
for i = 1, #digits do |
|||
for j = 1, #candidates do |
|||
local value = candidates[j] * 10 + digits[i] |
|||
if is_prime(value) then primes[#primes+1] = value end |
|||
candidates[#candidates+1] = value |
|||
end |
|||
end |
|||
table.sort(primes) |
|||
return primes |
|||
end |
|||
print(table.concat(descending_primes(), ", "))</lang> |
|||
{{out}} |
|||
<pre>2, 3, 5, 7, 31, 41, 43, 53, 61, 71, 73, 83, 97, 421, 431, 521, 541, 631, 641, 643, 653, 743, 751, 761, 821, 853, 863, 941, 953, 971, 983, 5431, 6421, 6521, 7321, 7541, 7621, 7643, 8431, 8521, 8543, 8641, 8731, 8741, 8753, 8761, 9421, 9431, 9521, 9631, 9643, 9721, 9743, 9851, 9871, 75431, 76421, 76541, 76543, 86531, 87421, 87541, 87631, 87641, 87643, 94321, 96431, 97651, 98321, 98543, 98621, 98641, 98731, 764321, 865321, 876431, 975421, 986543, 987541, 987631, 8764321, 8765321, 9754321, 9875321, 97654321, 98764321, 98765431</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |