Permuted multiples: Difference between revisions

m
m (→‎{{header|J}}: 123456 cannot be n)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 3 users not shown)
Line 98:
6 * n = 857142"
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">permutable?: function [n]->
one? unique map 2..6 'x -> sort digits x*n
 
firstPermutable: first select.first 1..∞ => permutable?
 
print [firstPermutable join.with:" " to [:string] map 2..6 'x -> x*firstPermutable]</syntaxhighlight>
 
{{out}}
 
<pre>142857 285714 428571 571428 714285 857142</pre>
 
=={{header|C}}==
Line 289 ⟶ 302:
5 * n = 714285
6 * n = 857142</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
function IsPMultiple(N: integer): boolean;
{Test if N*2, N*3, N*4, N*5, N*6 have the same digits}
var NT: integer;
var SA: array [0..4] of string;
var I,J: integer;
var SL: TStringList;
var IA: TIntegerDynArray;
begin
SL:=TStringList.Create;
try
Result:=False;
for I:=0 to 4 do
begin
{Do N*2, N*3, N*4, N*5, N*6}
NT:=N * (I+2);
{Get digits}
GetDigits(NT,IA);
{Store each digit in String List}
SL.Clear;
for J:=0 to High(IA) do SL.Add(IntToStr(IA[J]));
{Sort list}
SL.Sort;
{Put sorted digits in a string}
SA[I]:='';
for J:=0 to SL.Count-1 do SA[I]:=SA[I]+SL[J][1];
end;
{Compare all strings}
for I:=0 to High(SA)-1 do
if SA[I]<>SA[I+1] then exit;
Result:=True;
finally SL.Free; end;
end;
 
procedure ShowPermutedMultiples(Memo: TMemo);
var I,J: integer;
begin
for I:=1 to high(integer) do
if IsPMultiple(I) then
begin
for J:=1 to 6 do
Memo.Lines.Add(Format('N * %D = %D',[J,I*J]));
break;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
N * 1 = 142,857
N * 2 = 285,714
N * 3 = 428,571
N * 4 = 571,428
N * 5 = 714,285
N * 6 = 857,142
 
Elapsed Time: 4.030 Sec.
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 438 ⟶ 520:
 
=={{header|J}}==
Because 1*n and 6*n have the same number of digits, and because 2*6 is 12, we know that the first digit of n must be 1. And, because 1*m is different for any m in 1 2 3 4 5 and 6, we know that n must contain at least 6 different digits. So n must be at least 123456. And, as mentioned on the talk page, n must be divisible by 3. (And, of course, 123456 is divisible by 3.)
 
In other words:
Line 1,163 ⟶ 1,245:
{{libheader|Wren-math}}
One thing that's immediately clear is that the number must begin with '1' otherwise the higher multiples will have more digits than it has.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
// assumes l1 is sorted but l2 is not
9,476

edits