Permuted multiples: Difference between revisions

Content added Content deleted
(Added AppleScript.)
Line 7: Line 7:
Find the smallest positive integer '''n''' such that, when expressed in decimal, 2*n, 3*n, 4*n, 5*n, and 6*n contain ''exactly'' the same digits but in a different order.
Find the smallest positive integer '''n''' such that, when expressed in decimal, 2*n, 3*n, 4*n, 5*n, and 6*n contain ''exactly'' the same digits but in a different order.
<br><br>
<br><br>

=={{header|AppleScript}}==
{{trans|Phix}} — except that the 'steps' figure here is cumulative. 'Steps' are the number of 'n's actually tried when n * 6 doesn't exceed the next power of 10. While no power of 10 is exactly divisible by 6, it ''is'' technically the point at which the number of digits increases, so I've pedantically adjusted the logic to reflect this. :) The number of steps between 100,000 and the final arrival at 142,857 is 14,286, which is interesting.
<lang applescript>use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>

on decDigits(n)
set digits to {n mod 10 as integer}
set n to n div 10
repeat until (n = 0)
set beginning of digits to n mod 10 as integer
set n to n div 10
end repeat
return digits
end decDigits

on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join

on task()
set {output, n, n10, steps} to {{}, 3, 10, 0}
repeat
if (n * 6 < n10) then
set steps to steps + 1
set nl to decDigits(n)
tell sorter to sort(nl, 1, -1)
set found to true
repeat with i from 2 to 6
set inl to decDigits(n * i)
tell sorter to sort(inl, 1, -1)
if (inl ≠ nl) then
set found to false
exit repeat
end if
end repeat
if (found) then exit repeat
set n to n + 3
else
set end of output to "Nothing below " & n10 & (" (" & steps & " steps)")
set n to n10 + 2
set n10 to n10 * 10
-- set steps to 0
end if
end repeat
set end of output to " n = " & n & (" (" & steps & " steps altogether)")
repeat with i from 2 to 6
set end of output to (i as text) & " * n = " & i * n
end repeat
return join(output, linefeed)
end task

task()</lang>

{{output}}
<lang applescript>"Nothing below 10 (0 steps)
Nothing below 100 (2 steps)
Nothing below 1000 (24 steps)
Nothing below 10000 (246 steps)
Nothing below 100000 (2468 steps)
n = 142857 (16754 steps altogether)
2 * n = 285714
3 * n = 428571
4 * n = 571428
5 * n = 714285
6 * n = 857142"</lang>


=={{header|Factor}}==
=={{header|Factor}}==