Permuted multiples: Difference between revisions

m
syntax highlighting fixup automation
(Add APL)
m (syntax highlighting fixup automation)
Line 9:
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">{(⍳6)×{⍵+1}⍣{1=≢∪{⍵[⍋⍵]}¨⍕¨⍺×⍳6}⊢⍵} 123</langsyntaxhighlight>
{{out}}
<pre>142857 285714 428571 571428 714285 857142</pre>
Line 18:
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. :\
 
<langsyntaxhighlight 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>
 
Line 73:
end task
 
task()</langsyntaxhighlight>
 
{{output}}
Using 'set n to n10 + 26':
<langsyntaxhighlight lang="applescript">"Nothing below 1000 (14 steps)
Nothing below 10000 (228 steps)
Nothing below 100000 (2442 steps)
Line 85:
4 * n = 571428
5 * n = 714285
6 * n = 857142"</langsyntaxhighlight>
{{output}}
Using 'set n to n10 * 1.26 as integer':
<langsyntaxhighlight lang="applescript">"Nothing below 1000 (14 steps)
Nothing below 10000 (150 steps)
Nothing below 100000 (1506 steps)
Line 97:
5 * n = 714285
6 * n = 857142"
</syntaxhighlight>
</lang>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
Line 133:
printf("%d * n = %d\n", mult, n*mult);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1 * n = 142857
Line 143:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <array>
#include <iostream>
 
Line 180:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 193:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Get all digits of a number
digits = iter (n: int) yields (int)
while n>0 do
Line 228:
stream$putl(po, int$unparse(mult) || " * n = " || int$unparse(mult*n))
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>1 * n = 142857
Line 238:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Return the amount of times each digit appears in a number
Line 281:
print_nl();
i := i+1;
end loop;</langsyntaxhighlight>
{{out}}
<pre>1 * n = 142857
Line 291:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight 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>
</lang>
{{out}}
<pre>
Line 305:
{{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 321:
 
{ 2 3 4 5 6 } " n: " write smallest-permuted-multiple dup .
over n*v [ "×%d: %d\n" printf ] 2each</langsyntaxhighlight>
{{out}}
<pre>
Line 333:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function sort(s as string) as string
'quick and dirty bubblesort, not the focus of this exercise
dim as string t = s
Line 363:
print n, 2*n, 3*n, 4*n, 5*n, 6*n
end
loop</langsyntaxhighlight>
{{out}}<pre>
142857 285714 428571 571428 714285 857142
Line 371:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 423:
i = i + 1
}
}</langsyntaxhighlight>
 
{{out}}
Line 438:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class PermutedMultiples {
Line 474:
return digits;
}
}</langsyntaxhighlight>
 
{{out}}
Line 491:
 
The following uses a simple generate-and-test approach but with early backtracking, so it's quite reasonable.
<langsyntaxhighlight 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))) )</langsyntaxhighlight>
{{out}}
<pre>
Line 504:
 
=={{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
Line 512:
 
=={{header|MAD}}==
<langsyntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES TENMUL = 1,10,100,1000,10000,100000,
1 1000000,10000000,100000000,1000000000
Line 536:
THROUGH SHOW, FOR M=1, 1, M.G.6
SHOW PRINT FORMAT FMT, M, N*M
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre>1 * N = 142857
Line 548:
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 566:
echo " n = ", n
for k in 2..6:
echo k, "n = ", k * n</langsyntaxhighlight>
 
{{out}}
Line 581:
Using set of tdigit ,so no sort of digits is required.<BR>
Don't use the fact, that second digit must be < 6.Runtime negligible.<BR>
<langsyntaxhighlight lang="pascal">program euler52;
{$IFDEF FPC}
{$MOde DElphi} {$Optimization On,ALL}
Line 743:
readln;
{$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre>TIO.RUN
Line 790:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Permuted_multiples
Line 806:
};
printf " n %s\n", $n;
printf "%dn %s\n", $_ , $n * $_ for 2 .. 6;</langsyntaxhighlight>
{{out}}
<pre>
Line 819:
=={{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>
Line 859:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 955:
 
=={{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 968:
 
=={{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.*/
Line 996:
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 1,008:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,046:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,061:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">func getDigits(_ num: Int) -> Array<Int> {
var n = num
var digits = Array(repeating: 0, count: 10)
Line 1,097:
}
p *= 10
}</langsyntaxhighlight>
 
{{out}}
Line 1,112:
{{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 lang="ecmascript">import "/math" for Int
 
// assumes l1 is sorted but l2 is not
Line 1,150:
}
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
Line 1,165:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Digits(N); \Return counts of digits packed in 30 bits
int N, Sums;
[Sums:= 0;
Line 1,185:
];
IntOut(0, N);
]</langsyntaxhighlight>
 
{{out}}
10,327

edits