Pandigital prime: Difference between revisions
Content added Content deleted
(Added Sidef) |
(→{{header|Wren}}: Now uses Wren-perm.) |
||
Line 671: | Line 671: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-perm}} |
|||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<br> |
<br> |
||
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks. |
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks. |
||
<lang ecmascript>import "/ |
<lang ecmascript>import "./perm" for Perm |
||
import "/ |
import "./math" for Int |
||
import "./fmt" for Fmt |
|||
// generates all permutations in lexicographical order |
|||
var permutations = Fn.new { |input| |
|||
var perms = [input] |
|||
var a = input.toList |
|||
var n = a.count - 1 |
|||
for (c in 1...Int.factorial(n+1)) { |
|||
var i = n - 1 |
|||
var j = n |
|||
while (a[i] > a[i+1]) i = i - 1 |
|||
while (a[j] < a[i]) j = j - 1 |
|||
a.swap(i, j) |
|||
j = n |
|||
i = i + 1 |
|||
while (i < j) { |
|||
a.swap(i, j) |
|||
i = i + 1 |
|||
j = j - 1 |
|||
} |
|||
perms.add(a.toList) |
|||
} |
|||
return perms |
|||
} |
|||
for (start in 1..0) { |
for (start in 1..0) { |
||
var outer = false |
var outer = false |
||
System.print("The largest pandigital decimal prime which uses all the digits %(start)..n once is:") |
System.print("The largest pandigital decimal prime which uses all the digits %(start)..n once is:") |
||
for (n in [7, 4]) { |
for (n in [7, 4]) { |
||
var perms = |
var perms = Perm.listLex((start..n).toList) |
||
for (i in perms.count - 1..0) { |
for (i in perms.count - 1..0) { |
||
if (perms[i][-1] % 2 == 0 || perms[i][-1] == 5 || (start == 0 && perms[i][0] == "0")) continue |
if (perms[i][-1] % 2 == 0 || perms[i][-1] == 5 || (start == 0 && perms[i][0] == "0")) continue |