Pandigital prime: Difference between revisions
Content added Content deleted
(→Algol 68: Digits 0 to n: fix comment) |
(→{{header|Wren}}: Combined the two versions into 1.) |
||
Line 468: | Line 468: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<br> |
|||
===Using digits 1 to n=== |
|||
This makes use of the optimization in the Factor entry. |
This makes use of the optimization strategy in the Factor entry to do both the basic and optional tasks. |
||
<lang ecmascript>import "/math" for Int |
<lang ecmascript>import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 483: | Line 483: | ||
while (a[i] > a[i+1]) i = i - 1 |
while (a[i] > a[i+1]) i = i - 1 |
||
while (a[j] < a[i]) j = j - 1 |
while (a[j] < a[i]) j = j - 1 |
||
a.swap(i, j) |
|||
⚫ | |||
a[j] = t |
|||
j = n |
j = n |
||
i = i + 1 |
i = i + 1 |
||
while (i < j) { |
while (i < j) { |
||
a.swap(i, j) |
|||
⚫ | |||
⚫ | |||
i = i + 1 |
i = i + 1 |
||
j = j - 1 |
j = j - 1 |
||
Line 500: | Line 496: | ||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
var perms = permutations.call((1..n).toList) |
|||
for ( |
for (n in [7, 4]) { |
||
var perms = permutations.call((start..n).toList) |
|||
for (i in perms.count - 1..0) { |
|||
⚫ | |||
⚫ | |||
var p = Num.fromString(perms[i].join()) |
|||
if (Int.isPrime(p)) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
} |
} |
||
}</lang> |
}</lang> |
||
Line 517: | Line 518: | ||
The largest pandigital decimal prime which uses all the digits 1..n once is: |
The largest pandigital decimal prime which uses all the digits 1..n once is: |
||
7,652,413 |
7,652,413 |
||
</pre> |
|||
===Using digits 0 to n=== |
|||
Applying the Factor logic for the digits 0..n, we only need to check 8 and 5 digit cases as it can't be any of the others. |
|||
<lang ecmascript>import "/math" for Int |
|||
import "/fmt" for Fmt |
|||
// generates all permutations in lexicographical order |
|||
var permutations = Fn.new { |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 |
|||
var t = a[i] |
|||
a[i] = a[j] |
|||
a[j] = t |
|||
j = n |
|||
i = i + 1 |
|||
while (i < j) { |
|||
t = a[i] |
|||
a[i] = a[j] |
|||
a[j] = t |
|||
i = i + 1 |
|||
j = j - 1 |
|||
} |
|||
perms.add(a.toList) |
|||
} |
|||
return perms |
|||
} |
|||
System.print("The largest pandigital decimal prime which uses all the digits 0..n once is:") |
|||
for (n in [7, 4]) { |
|||
var perms = permutations.call((0..n).toList) |
|||
for (i in perms.count - 1..0) { |
|||
⚫ | |||
var p = Num.fromString(perms[i].join()) |
|||
if (Int.isPrime(p)) { |
|||
⚫ | |||
return |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The largest pandigital decimal prime which uses all the digits 0..n once is: |
The largest pandigital decimal prime which uses all the digits 0..n once is: |
||
76,540,231 |
76,540,231 |