Permuted multiples: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
(31 intermediate revisions by 16 users not shown)
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.
<br><br>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">{(⍳6)×{⍵+1}⍣{1=≢∪{⍵[⍋⍵]}¨⍕¨⍺×⍳6}⊢⍵} 123</syntaxhighlight>
{{out}}
<pre>142857 285714 428571 571428 714285 857142</pre>
 
=={{header|AppleScript}}==
{{trans|Phix}} — except that the 'steps' figure here is cumulative. Also, for six different numbers to have the same digits, each must have at least three digits, none of which can be 0. So the lowest possible value of n in this respect is 123. But for a number beginning with 1 to stand any chance of containing the same digits as both a number that's 2 times it and another that's 6 times it, it must also contain at least one digit that's no less than 2 and another that's no less than 6. The lowest combination of these is 26, which also produces a multiple of 3 when added to a power of 10. So this makes a slightly better post-power start point than 2, saving eight steps per power. ;)
 
Shifting the 26 up against the 1 obviously keeps the "at least" condition satisfied for longer during the subsequent additions of 3 at the low end and gives a start point much closer to the next power. This more than halves the number of steps performed and thus the time taken. It also produces the correct result(s), but I can't see that it's logically bound to do so. :\
 
<syntaxhighlight 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 {{}, 126, 1000, 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 + 26 -- set n to n10 * 1.26 as integer
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()</syntaxhighlight>
 
{{output}}
Using 'set n to n10 + 26':
<syntaxhighlight lang="applescript">"Nothing below 1000 (14 steps)
Nothing below 10000 (228 steps)
Nothing below 100000 (2442 steps)
n = 142857 (16720 steps altogether)
2 * n = 285714
3 * n = 428571
4 * n = 571428
5 * n = 714285
6 * n = 857142"</syntaxhighlight>
{{output}}
Using 'set n to n10 * 1.26 as integer':
<syntaxhighlight lang="applescript">"Nothing below 1000 (14 steps)
Nothing below 10000 (150 steps)
Nothing below 100000 (1506 steps)
n = 142857 (7126 steps altogether)
2 * n = 285714
3 * n = 428571
4 * n = 571428
5 * n = 714285
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}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
/* Find the set of digits of N, expressed as a number
where the N'th digit represents the amount of times
that digit occurs. */
int digit_set(int n) {
static const int powers[] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
100000000, 1000000000
};
int dset;
for (dset = 0; n; n /= 10)
dset += powers[n % 10];
return dset;
}
 
/* See if for a given N, [1..6]*N all have the same digits */
bool is_permuted_multiple(int n) {
int dset = digit_set(n);
for (int mult = 2; mult <= 6; mult++)
if (dset != digit_set(n * mult)) return false;
return true;
}
 
/* Find the first matching number */
int main() {
int n;
for (n = 123; !is_permuted_multiple(n); n++);
for (int mult = 1; mult <= 6; mult++)
printf("%d * n = %d\n", mult, n*mult);
return 0;
}</syntaxhighlight>
{{out}}
<pre>1 * n = 142857
2 * n = 285714
3 * n = 428571
4 * n = 571428
5 * n = 714285
6 * n = 857142</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <array>
#include <iostream>
 
using digits = std::array<unsigned int, 10>;
 
digits get_digits(unsigned int n) {
digits d = {};
do {
++d[n % 10];
n /= 10;
} while (n > 0);
return d;
}
 
// Returns true if n, 2n, ..., 6n all have the same base 10 digits.
bool same_digits(unsigned int n) {
digits d = get_digits(n);
for (unsigned int i = 0, m = n; i < 5; ++i) {
m += n;
if (get_digits(m) != d)
return false;
}
return true;
}
 
int main() {
for (unsigned int p = 100; ; p *= 10) {
unsigned int max = (p * 10) / 6;
for (unsigned int n = p + 2; n <= max; n += 3) {
if (same_digits(n)) {
std::cout << " n = " << n << '\n';
for (unsigned int i = 2; i <= 6; ++i)
std::cout << i << "n = " << n * i << '\n';
return 0;
}
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
n = 142857
2n = 285714
3n = 428571
4n = 571428
5n = 714285
6n = 857142
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Get all digits of a number
digits = iter (n: int) yields (int)
while n>0 do
yield(n//10)
n := n/10
end
end digits
 
% Return the amount of times each digit occurs
digit_set = proc (n: int) returns (sequence[int])
ds: array[int] := array[int]$fill(0,10,0)
for d: int in digits(n) do
ds[d] := ds[d] + 1
end
return(sequence[int]$a2s(ds))
end digit_set
 
% See if for an integer N, [1..6]*N all have the same digits
permuted_multiple = proc (n: int) returns (bool)
ds: sequence[int] := digit_set(n)
for mult: int in int$from_to(2,6) do
if digit_set(mult*n) ~= ds then return(false) end
end
return(true)
end permuted_multiple
 
% Find the first number for which this holds
start_up = proc ()
n: int := 123
while ~permuted_multiple(n) do n := n+1 end
po: stream := stream$primary_output()
for mult: int in int$from_to(1,6) do
stream$putl(po, int$unparse(mult) || " * n = " || int$unparse(mult*n))
end
end start_up</syntaxhighlight>
{{out}}
<pre>1 * n = 142857
2 * n = 285714
3 * n = 428571
4 * n = 571428
5 * n = 714285
6 * n = 857142</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Return the amount of times each digit appears in a number
# (as long as none appears more than 9 times that is)
sub digit_set(n: uint32): (set: uint32) is
var ten_powers: uint32[] := {
1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000
};
set := 0;
while n>0 loop
var digit := (n % 10) as uint8;
n := n / 10;
set := set + ten_powers[digit];
end loop;
end sub;
 
# See if for an integer N, [1..6]*N all have the same digits
sub permuted_multiple(n: uint32): (ok: uint8) is
ok := 0;
var ds := digit_set(n);
var i: uint32 := 2;
while i<=6 loop
if ds != digit_set(i * n) then return; end if;
i := i + 1;
end loop;
ok := 1;
end sub;
 
# Find the first matching number
var n: uint32 := 123;
while permuted_multiple(n) == 0 loop
n := n + 1;
end loop;
 
# Print the number and its multiples
var i: uint32 := 1;
while i<=6 loop
print_i32(i);
print(" * n = ");
print_i32(n * i);
print_nl();
i := i+1;
end loop;</syntaxhighlight>
{{out}}
<pre>1 * n = 142857
2 * n = 285714
3 * n = 428571
4 * n = 571428
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#}}==
<syntaxhighlight lang="fsharp">
// Permuted multiples. Nigel Galloway: August 18th., 2021
let fG n g=let rec fN g=[if g>0 then yield g%10; yield! fN(g/10)] in List.sort(fN n)=List.sort(fN g)
let n=Seq.initInfinite((+)2)|>Seq.collect(fun n->seq{(pown 10 n)+2..3..(pown 10 (n+1))/6})|>Seq.find(fun g->let fN=fG g in fN(g*2)&&fN(g*3)&&fN(g*4)&&fN(g*5)&&fN(g*6))
printfn $"The solution to Project Euler 52 is %d{n}"
</syntaxhighlight>
{{out}}
<pre>
The solution to Project Euler 52 is 142857
</pre>
 
=={{header|Factor}}==
{{libheader|Factor-numspec}}
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel lists lists.lazy math math.ranges
math.vectors numspec present prettyprint sequences sets ;
 
Line 27 ⟶ 403:
 
{ 2 3 4 5 6 } " n: " write smallest-permuted-multiple dup .
over n*v [ "×%d: %d\n" printf ] 2each</langsyntaxhighlight>
{{out}}
<pre>
Line 36 ⟶ 412:
×5: 714285
×6: 857142
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function sort(s as string) as string
'quick and dirty bubblesort, not the focus of this exercise
dim as string t = s
dim as uinteger i, j, n = len(t)
dim as boolean sw
 
for i = n to 2 step -1
sw = false
for j = 1 to i-1
if asc(mid(t,j,1))>asc(mid(t,j+1,1)) then
sw = true
swap t[j-1], t[j]
end if
next j
if sw = false then return t
 
next i
return t
end function
 
dim as string ns(1 to 6)
dim as uinteger n = 0, i
do
n+=1
for i = 1 to 6
ns(i) = sort(str(i*n))
if i>1 andalso ns(i)<>ns(i-1) then continue do
next i
print n, 2*n, 3*n, 4*n, 5*n, 6*n
end
loop</syntaxhighlight>
{{out}}<pre>
142857 285714 428571 571428 714285 857142
</pre>
 
Line 41 ⟶ 453:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 93 ⟶ 505:
i = i + 1
}
}</langsyntaxhighlight>
 
{{out}}
Line 106 ⟶ 518:
6 x n = 857142
</pre>
 
=={{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:
<syntaxhighlight lang=J> D*/(3+])^:(D {{1<#~./:"1~10#.inv y*m}})^:_(10#.D=:1+i.6)
142857 285714 428571 571428 714285 857142</syntaxhighlight>
 
Here, we start with <code>123456</code>, and then add <code>3</code> to it until the digits appearing in its multiples by <code>D</code>, when sorted, are all the same. (<code>D</code> is <code>1 2 3 4 5 6</code>.)
 
It's worth noting here that
<syntaxhighlight lang=J> <.1e6%7
142857</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.util.*;
 
public class PermutedMultiples {
public static void main(String[] args) {
for (int p = 100; ; p *= 10) {
int max = (p * 10) / 6;
for (int n = p + 2; n <= max; n += 3) {
if (sameDigits(n)) {
System.out.printf(" n = %d\n", n);
for (int i = 2; i <= 6; ++i)
System.out.printf("%dn = %d\n", i, n * i);
return;
}
}
}
}
 
// Returns true if n, 2n, ..., 6n all have the same base 10 digits.
private static boolean sameDigits(int n) {
int[] digits = getDigits(n);
for (int i = 0, m = n; i < 5; ++i) {
m += n;
if (!Arrays.equals(getDigits(m), digits))
return false;
}
return true;
}
 
private static int[] getDigits(int n) {
int[] digits = new int[10];
do {
++digits[n % 10];
n /= 10;
} while (n > 0);
return digits;
}
}</syntaxhighlight>
 
{{out}}
<pre>
n = 142857
2n = 285714
3n = 428571
4n = 571428
5n = 714285
6n = 857142
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The following uses a simple generate-and-test approach but with early backtracking, so it's quite reasonable.
<syntaxhighlight lang="jq">def digits: tostring | explode;
 
first(range(1; infinite)
| . as $i
| (digits|sort) as $reference
| select(all(range(2;7); $reference == ((. * $i) | digits | sort))) )</syntaxhighlight>
{{out}}
<pre>
142857
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">n = minimum([n for n in 1:2000000 if sort(digits(2n)) == sort(digits(3n)) == sort(digits(4n)) == sort(digits(5n))== sort(digits(6n))])
println("n: $n, 2n: $(2n), 3n: $(3n), 4n: $(4n), 5n: $(5n), 6n: $(6n)")
</langsyntaxhighlight>{{out}}
<pre>
n: 142857, 2n: 285714, 3n: 428571, 4n: 571428, 5n: 714285, 6n: 857142
</pre>
 
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES TENMUL = 1,10,100,1000,10000,100000,
1 1000000,10000000,100000000,1000000000
VECTOR VALUES FMT = $I1,8H * N = ,I6*$
INTERNAL FUNCTION(XX)
ENTRY TO DIGSET.
X = XX
DSET = 0
DIGIT WHENEVER X.E.0, FUNCTION RETURN DSET
NXT = X/10
DSET = DSET + TENMUL(X-NXT*10)
X = NXT
TRANSFER TO DIGIT
END OF FUNCTION
N = 122
CAND N = N + 1
DS = DIGSET.(N)
THROUGH MUL, FOR M=2, 1, M.G.6
MUL WHENEVER DIGSET.(N*M).NE.DS, TRANSFER TO CAND
THROUGH SHOW, FOR M=1, 1, M.G.6
SHOW PRINT FORMAT FMT, M, N*M
END OF PROGRAM</syntaxhighlight>
{{out}}
<pre>1 * N = 142857
2 * N = 285714
3 * N = 428571
4 * N = 571428
5 * N = 714285
6 * N = 857142</pre>
 
=={{header|Nim}}==
Searching among multiples of 3 between 102 and 1_000 div 6, 1_002 and 10_000 div 6, 10_002 and 100_000 div 6, etc. (see discussion).
 
<langsyntaxhighlight Nimlang="nim">from algorithm import sorted
 
func search(): int =
Line 136 ⟶ 661:
echo " n = ", n
for k in 2..6:
echo k, "n = ", k * n</langsyntaxhighlight>
 
{{out}}
Line 145 ⟶ 670:
5n = 714285
6n = 857142</pre>
 
=={{header|Pascal}}==
Create an array of the digits fixed 1 as first digit and 0 "1023456789"<BR>
Adding done digit by digit, so no conversion needed.<BR>
Don't use the fact, that second digit must be < 6.Runtime negligible.
Using set of tdigit ,so no sort of digits is required.<BR>
<lang pascal>program euler52;
Don't use the fact, that second digit must be < 6.Runtime negligible.<BR>
<syntaxhighlight lang="pascal">program euler52;
{$IFDEF FPC}
{$MOde DElphi} {$Optimization On,ALL}
{$else}
{$Apptype console}
Line 157 ⟶ 685:
sysutils;
const
BaseConvDgt :array[0..35] of char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Base = 10;
MAXBASE = 12;//
type
TUsedDigits = array[0..BaseMAXBASE-1] of byte;
tDigitsInUse = set of 0..BaseMAXBASE-1;
var
{$ALIGN 16}
UsedDigits :tUsedDigits;
{$ALIGN 16}
gblMaxDepth : NativeInt;
gblMaxDepth,
steps,
base,maxmul : NativeInt;
found : boolean;
function AddOne(var SumDigits:tUsedDigits;const UsedDigits: tUsedDigits):NativeInt;forward;
function ConvBaseToStr(const UsedDigits :tUsedDigits):string;
var
i,j:NativeUint;
Begin
setlength(result,gblMaxdepth+1);
j := 1;
For i := 0 to gblMaxdepth do
begin
result[j] := BaseConvDgt[UsedDigits[i]];
inc(j);
end;
end;
 
procedure Out_MaxMul(const UsedDigits :tUsedDigits);
var
j : NativeInt;
SumDigits :tUsedDigits;
begin
writeln('With ',gblMaxdepth+1,' digits');
sumDigits := UsedDigits;
write(' 1x :',ConvBaseToStr(UsedDigits));
For j := 2 to MaxMul do
Begin
AddOne(SumDigits,UsedDigits);
write(j:2,'x:',ConvBaseToStr(SumDigits));
end;
writeln;
writeln('steps ',steps);
end;
 
procedure InitUsed;
Var
i : NativeInt;
Begin
For i := 2 to BaseBASE-1 do
UsedDigits[i] := i;
UsedDigits[0] := 1;
Line 184 ⟶ 749:
include(result,UsedDigits[i]);
end;
 
function AddOne(var SumDigits:tUsedDigits;const UsedDigits: tUsedDigits):NativeInt;
//add and return carry
var
s,i: NativeUint;
begin
result := 0;
For i := gblMaxdepth downto 0 do
Begin
s := UsedDigits[i]+SumDigits[i]+result;
result := ord(s >= BASE);// 0 or 1
// if result >0 then s -= base;//runtime Base=12 Done in 2.097 -> Done in 1.647
s -= result*base;
SumDigits[i] := s;
end;
end;
 
function CheckMultiples(const UsedDigits: tUsedDigits;OrgInUse:tDigitsInUse):NativeInt;
var
{$ALIGN 16}
SumDigits :tUsedDigits;
i,c,s,j : integer;
begin
result := 0;
SumDigits := UsedDigits;
j := 2;// first doubled
repeat
if AddOne(SumDigits,UsedDigits) >0 then
c := 0;
For i := gblMaxdepth downto 0 do
Begin
s := UsedDigits[i]+SumDigits[i]+c;
c := ord(s >= base);
SumDigits[i] := s-c*base;
end;
IF (c > 0) then
break;
if GetUsedSet(SumDigits) <> OrgInUse then
break;
inc(j);
until j > 6MaxMul;
found := j > MaxMul;
IFif j > 6found then
Out_MaxMul(UsedDigits);
Begin
result := 0;
//Output in Base 10
For i := 0 to gblMaxdepth do
result := result * Base +UsedDigits[i];
For i := 1 to 6 do
writeln(i*result);
writeln;
end;
end;
 
procedure Check;
Begin
CheckMultiples(UsedDigits,GetUsedSet(UsedDigits))
end;
procedure GetNextUsedDigit(StartIdx:NativeInt);
var
Line 231 ⟶ 792:
DigitTaken: Byte;
Begin
For i := StartIDx to BaseBASE-1 do
Begin
//swapStop iafter withfirst Startidxfound
if found then BREAK;
DigitTaken := UsedDigits[i];
//swap i with Startidx
UsedDigits[i]:= UsedDigits[StartIdx];
UsedDigits[StartIdx] := DigitTaken;
 
// write(StartIdx:3,i:3,DigitTaken:3,' ');
inc(steps);
IF StartIdx <gblMaxDepth then
GetNextUsedDigit(StartIdx+1)
else
CheckMultiples(UsedDigits,GetUsedSet(UsedDigits));
check;
//undo swap i with Startidx
UsedDigits[StartIdx] := UsedDigits[i];
Line 247 ⟶ 812:
end;
end;
 
var
T : INt64;
Begin
T := GetTickCount64;
// For gblMaxDepthbase := 24 to Base-1MAXBASE do
For base := 4 to 10 do
Begin
Writeln('Base ',base);
InitUsed;
MaxMul := Base-2;
writeln('With ',gblMaxdepth+1,' digits');
If base = 10 then
GetNextUsedDigit(1);
MaxMul := 6;
InitUsed;
steps := 0;
For gblMaxDepth := 1 to BASE-1 do
Begin
found := false;
GetNextUsedDigit(1);
end;
writeln;
end;
T := GetTickCount64-T;
write('Done in ',T/1000:0:3,' s');
{$IFDEF WINdows}
readln;
{$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre>TIO.RUN
Base 4
 
With 3 digits
1x :102 2x:210
steps 5
With 4 digits
1x :1032 2x:2130
steps 10
 
Base 5
 
Base 6
With 5 digits
1x :10432 2x:21304 3x:32140 4x:43012
steps 139
With 6 digits
1x :105432 2x:215304 3x:325140 4x:435012
142857
steps 197
285714
428571
571428
714285
857142
 
Base 7
 
Base 8
With 7 digits
1x :1065432 2x:2153064 3x:3240516 4x:4326150 5x:5413602 6x:6501234
1428570
steps 5945
2857140
With 8 digits
4285710
1x :10765432 2x:21753064 3x:32740516 4x:43726150 5x:54713602 6x:65701234
5714280
steps 7793
7142850
8571420
 
Base 9
1429857
2859714
4289571
5719428
7149285
8579142
 
Base 10
With 6 digits
1x :142857 2x:285714 3x:428571 4x:571428 5x:714285 6x:857142
steps 10725
With 7 digits
1x :1428570 2x:2857140 3x:4285710 4x:5714280 5x:7142850 6x:8571420
steps 37956
With 8 digits
1x :14298570 2x:28597140 3x:42895710 4x:57194280 5x:71492850 6x:85791420
14298570
steps 128297
28597140
42895710
57194280
71492850
85791420
 
Done in 0.044 s</pre>
With 9 digits
 
With 10 digits
=={{header|Perl}}==
Done in 0.054</pre>
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Permuted_multiples
use warnings;
 
my $n = 3;
1 while do {
length($n += 3) < length 6 * $n and $n = 1 . $n =~ s/./0/gr + 2;
my $sorted = join '', sort split //, $n * 6;
$sorted ne join '', sort split //, $n * 1 or
$sorted ne join '', sort split //, $n * 2 or
$sorted ne join '', sort split //, $n * 3 or
$sorted ne join '', sort split //, $n * 4 or
$sorted ne join '', sort split //, $n * 5
};
printf " n %s\n", $n;
printf "%dn %s\n", $_ , $n * $_ for 2 .. 6;</syntaxhighlight>
{{out}}
<pre>
n 142857
2n 285714
3n 428571
4n 571428
5n 714285
6n 857142
</pre>
 
=={{header|Phix}}==
Maintain a limit (n10) and bump the iteration whenever *6 increases the number of digits, which (as [was] shown) cuts the number of iterations by a factor of nearly thirteen and a half times (as in eg [as was] 67 iterations instead of 900 to find nothing in 100..1,000). Also as noted on the talk page, since sum(digits(3n)) is a multiple of 3 and it uses the same digits as n, then sum(digits(n)) will also be the very same multiple of 3 and hence n must (also) be divisible by 3, so we can start each longer-digits iteration on 10^k+2 (since remainder(10^k,3) is always 1) and employ a step of 3, and enjoy a better than 40-fold overall reduction in iterations.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n10</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">steps</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">fmt</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"""
%s positive integer n for which (2..6)*n contain the same digits:
n = %,d (%,d steps, hmmm...)
2 x n = %,d
3 x n = %,d
4 x n = %,d
5 x n = %,d
6 x n = %,d
"""</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1e9</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">nowtelse</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Nothing"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">smother</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Smallest"</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">6</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">n10</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Nothing%s less than %,d (%,d steps)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nowtelse</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">steps</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n10</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span>
<span style="color: #000000;">n10</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span>
Line 322 ⟶ 943:
<span style="color: #008080;">if</span> <span style="color: #000000;">ins</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">ns</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">7</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">smother</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">steps</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">&</span> <span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)))</span>
<span style="color: #000000;">nowtelse</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Nothing else"</span>
<span style="color: #000000;">smother</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Another"</span>
<span style="color: #008080;">exit</span> <span style="color: #000080;font-style:italic;">-- (see below)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">3</span>
<span style="color: #000000;">steps</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0080800000FF;">constant?</span> <span style="color: #0000007060A8;">fmtelapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0080000000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Smallest positive integer n for which (2..6)*n contain the same digits:
n = %d
2 x n = %d
3 x n = %d
4 x n = %d
5 x n = %d
6 x n = %d
"""</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)))</span>
<!--</lang>-->
{{out}}
<pre>
Line 346 ⟶ 963:
Nothing less than 100,000 (2,222 steps)
Smallest positive integer n for which (2..6)*n contain the same digits:
n = 142857142,857 (14,285 steps, hmmm...)
2 x n = 285714285,714
3 x n = 428571428,571
4 x n = 571428571,428
5 x n = 714285714,285
6 x n = 857142857,142
"0.1s"
</pre>
=== extended output ===
If we comment out that "exit -- (see below)", as per the AppleScript comments and the Pascal output,
some patterns start to emerge in the values and number of steps: *10 is a bit of a given, whereas
"insert 9s before the 8" is (for me) a bit more unexpected. Be warned: on the desktop, 1e8 takes
about 9s, 1e9 about 90s, so I'll predict 1e10 would take 15mins (and need 64bit) and I'll not try
to compete with Pascal in terms of performance, though I am getting very different results above 1e7.
Under pwa/p2js 1e8 takes about 30s (meh) so I've limited it to 1e7 (2.3s).
<pre>
Nothing else less than 1,000,000 (22,222 steps)
Another positive integer n for which (2..6)*n contain the same digits:
n = 1,428,570 (142,856 steps, hmmm...)
2 x n = 2,857,140
3 x n = 4,285,710
4 x n = 5,714,280
5 x n = 7,142,850
6 x n = 8,571,420
Another positive integer n for which (2..6)*n contain the same digits:
n = 1,429,857 (143,285 steps, hmmm...)
2 x n = 2,859,714
3 x n = 4,289,571
4 x n = 5,719,428
5 x n = 7,149,285
6 x n = 8,579,142
Nothing else less than 10,000,000 (222,222 steps)
Another positive integer n for which (2..6)*n contain the same digits:
n = 14,285,700 (1,428,566 steps, hmmm...)
2 x n = 28,571,400
3 x n = 42,857,100
4 x n = 57,142,800
5 x n = 71,428,500
6 x n = 85,714,200
Another positive integer n for which (2..6)*n contain the same digits:
n = 14,298,570 (1,432,856 steps, hmmm...)
2 x n = 28,597,140
3 x n = 42,895,710
4 x n = 57,194,280
5 x n = 71,492,850
6 x n = 85,791,420
Another positive integer n for which (2..6)*n contain the same digits:
n = 14,299,857 (1,433,285 steps, hmmm...)
2 x n = 28,599,714
3 x n = 42,899,571
4 x n = 57,199,428
5 x n = 71,499,285
6 x n = 85,799,142
Nothing else less than 100,000,000 (2,222,222 steps)
Another positive integer n for which (2..6)*n contain the same digits:
n = 142,857,000 (14,285,666 steps, hmmm...)
2 x n = 285,714,000
3 x n = 428,571,000
4 x n = 571,428,000
5 x n = 714,285,000
6 x n = 857,142,000
Another positive integer n for which (2..6)*n contain the same digits:
n = 142,985,700 (14,328,566 steps, hmmm...)
2 x n = 285,971,400
3 x n = 428,957,100
4 x n = 571,942,800
5 x n = 714,928,500
6 x n = 857,914,200
Another positive integer n for which (2..6)*n contain the same digits:
n = 142,998,570 (14,332,856 steps, hmmm...)
2 x n = 285,997,140
3 x n = 428,995,710
4 x n = 571,994,280
5 x n = 714,992,850
6 x n = 857,991,420
Another positive integer n for which (2..6)*n contain the same digits:
n = 142,999,857 (14,333,285 steps, hmmm...)
2 x n = 285,999,714
3 x n = 428,999,571
4 x n = 571,999,428
5 x n = 714,999,285
6 x n = 857,999,142
Nothing else less than 1,000,000,000 (22,222,222 steps)
</pre>
I believe that last pattern will be continue to be valid no matter how many 9s are inserted in the middle, and I doubt that any further patterns would emerge.
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
[ true swap
dup digits sort
swap
5 times
[ dup i 2 + *
digits sort
dip over != if
[ rot not unrot
conclude ] ]
2drop ] is permult ( n --> b )
 
0
[ 1+
dup permult until ]
6 times
[ dup
i^ 1+ dup echo
say " * n = "
* echo cr ]
drop</syntaxhighlight>
 
{{out}}
 
<pre>1 * n = 142857
2 * n = 285714
3 * n = 428571
4 * n = 571428
5 * n = 714285
6 * n = 857142</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>put display (^∞).map(1 ~ *).race.map( -> \n { next unless [eq] (2,3,4,5,6).map: { (n × $_).comb.sort.join }; n } ).first;
 
sub display ($n) { join "\n", " n: $n", (2..6).map: { "×$_: {$n×$_}" } }</langsyntaxhighlight>
{{out}}
<pre> n: 142857
Line 368 ⟶ 1,101:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the smallest positive integer n such that ··· */
/*───────────────────────── 2*n, 3*n, 4*5, 5*6, and 6*n contain the same decimal digits.*/
do n=1 /*increment N from unity. 'til answer.*/
b= 2*n /*calculate the product of: 2*n */
t= 3*n t= 3*n /* " " /* " " " 3*n */
if verify(t, b)>0 then iterate /*productT doesn't have req. digsrequired digits? Skip*/
q= 4*n /*calculate the product of: 4*n */
if verify(q, b)>0 then iterate /*productQ doesn't have req. digsrequired digits? Skip*/
if verify(q, t)>0 then iterate /*" " " " " " */
v= 5*n /*calculate the product of: 5*n */
if verify(v, b)<0 then iterate /*productV doesn't have req. digsrequired digits? Skip*/
if verify(v, t)>0 then iterate /*" " if verify(v,t)>0 " then iteratr /* " " " " "*/
if verify(v, q)>0 then iterate /*" " if verify(v,q)>0 " then iteratr /* " " " " "*/
s= 6*n /*calculate the product of: 6*n */
if verify(s, b)>0 then iterate /*productS doesn't have req. digsrequired digits? Skip*/
if verify(s, t)>0 then iterate /*" " " " " " */
if verify(s, q)>0 then iterate /*" " " " " " */
if verify(s, v)>0 then iterate /*" " " " " " */
say ' n =' commas(n) leave /*display the value of: n /*found the numbers, show.*/
say ' 2*n =' commas(b) end /* " " " " 2*n */
_= left('', 9) say ' 3*n =' commas(t) /* " " " " / 3*usedn for indentation. */
say _ ' n = say ' commas(n) 4*n =' commas(q) /* " " " " / 4*displayn value of n. */
say _ '2*n =' commas(b) say ' 5*n =' commas(v) /* " " /*" " 5*n " " 2*n. */
say _ '3*n =' commas(t) say ' 6*n =' commas(s) /* " " /*" " 6*n " " 3*n. */
say _ '4*n =' commas(q) leave /*found the " N number, time to " " 4*nleave. */
end /*n*/
say _ '5*n =' commas(v) /* " " " 5*n. */
say _ '6*n =' commas(s) /* " " " 6*n. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 409 ⟶ 1,141:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 447 ⟶ 1,179:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 459 ⟶ 1,191:
6*n = 857142
done...
</pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">func getDigits(_ num: Int) -> Array<Int> {
var n = num
var digits = Array(repeating: 0, count: 10)
while true {
digits[n % 10] += 1
n /= 10
if n == 0 {
break
}
}
return digits
}
 
// Returns true if n, 2n, ..., 6n all have the same base 10 digits.
func sameDigits(_ n: Int) -> Bool {
let digits = getDigits(n)
for i in 2...6 {
if digits != getDigits(i * n) {
return false
}
}
return true
}
 
var p = 100
loop: while true {
for n in stride(from: p + 2, through: (p * 10) / 6, by: 3) {
if sameDigits(n) {
print(" n = \(n)")
for i in 2...6 {
print("\(i)n = \(i * n)")
}
break loop
}
}
p *= 10
}</syntaxhighlight>
 
{{out}}
<pre>
n = 142857
2n = 285714
3n = 428571
4n = 571428
5n = 714285
6n = 857142
</pre>
 
Line 464 ⟶ 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.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
// assumes l1 is sorted but l2 is not
Line 502 ⟶ 1,283:
}
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
Line 514 ⟶ 1,295:
5 x n = 714285
6 x n = 857142
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func Digits(N); \Return counts of digits packed in 30 bits
int N, Sums;
[Sums:= 0;
repeat N:= N/10;
Sums:= Sums + 1<<(rem(0)*3);
until N = 0;
return Sums;
];
 
int N, Sums;
[N:= 1;
loop [Sums:= Digits(N*2);
if Digits(N*3) = Sums then
if Digits(N*4) = Sums then
if Digits(N*5) = Sums then
if Digits(N*6) = Sums then
quit;
N:= N+1;
];
IntOut(0, N);
]</syntaxhighlight>
 
{{out}}
<pre>
142857
</pre>
9,477

edits