Permuted multiples: Difference between revisions
m
syntax highlighting fixup automation
Not a robot (talk | contribs) (Add APL) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 9:
=={{header|APL}}==
<
{{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. :\
<
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
Line 73:
end task
task()</
{{output}}
Using 'set n to n10 + 26':
<
Nothing below 10000 (228 steps)
Nothing below 100000 (2442 steps)
Line 85:
4 * n = 571428
5 * n = 714285
6 * n = 857142"</
{{output}}
Using 'set n to n10 * 1.26 as integer':
<
Nothing below 10000 (150 steps)
Nothing below 100000 (1506 steps)
Line 97:
5 * n = 714285
6 * n = 857142"
</syntaxhighlight>
=={{header|C}}==
<
#include <stdbool.h>
Line 133:
printf("%d * n = %d\n", mult, n*mult);
return 0;
}</
{{out}}
<pre>1 * n = 142857
Line 143:
=={{header|C++}}==
<
#include <iostream>
Line 180:
}
}
}</
{{out}}
Line 193:
=={{header|CLU}}==
<
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</
{{out}}
<pre>1 * n = 142857
Line 238:
=={{header|Cowgol}}==
<
# Return the amount of times each digit appears in a number
Line 281:
print_nl();
i := i+1;
end loop;</
{{out}}
<pre>1 * n = 142857
Line 291:
=={{header|F_Sharp|F#}}==
<
// 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>
Line 305:
{{libheader|Factor-numspec}}
{{works with|Factor|0.99 2021-06-02}}
<
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</
{{out}}
<pre>
Line 333:
=={{header|FreeBASIC}}==
<
'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</
{{out}}<pre>
142857 285714 428571 571428 714285 857142
Line 371:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 423:
i = i + 1
}
}</
{{out}}
Line 438:
=={{header|Java}}==
<
public class PermutedMultiples {
Line 474:
return digits;
}
}</
{{out}}
Line 491:
The following uses a simple generate-and-test approach but with early backtracking, so it's quite reasonable.
<
first(range(1; infinite)
| . as $i
| (digits|sort) as $reference
| select(all(range(2;7); $reference == ((. * $i) | digits | sort))) )</
{{out}}
<pre>
Line 504:
=={{header|Julia}}==
<
println("n: $n, 2n: $(2n), 3n: $(3n), 4n: $(4n), 5n: $(5n), 6n: $(6n)")
</
<pre>
n: 142857, 2n: 285714, 3n: 428571, 4n: 571428, 5n: 714285, 6n: 857142
Line 512:
=={{header|MAD}}==
<
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</
{{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).
<
func search(): int =
Line 566:
echo " n = ", n
for k in 2..6:
echo k, "n = ", k * n</
{{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>
<
{$IFDEF FPC}
{$MOde DElphi} {$Optimization On,ALL}
Line 743:
readln;
{$ENDIF}
end.</
{{out}}
<pre>TIO.RUN
Line 790:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Permuted_multiples
Line 806:
};
printf " n %s\n", $n;
printf "%dn %s\n", $_ , $n * $_ for 2 .. 6;</
{{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.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 955:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
sub display ($n) { join "\n", " n: $n", (2..6).map: { "×$_: {$n×$_}" } }</
{{out}}
<pre> n: 142857
Line 968:
=={{header|REXX}}==
<
/*───────────────────────── 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 ?</
{{out|output|text= when using the internal default input:}}
<pre>
Line 1,008:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,046:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,061:
=={{header|Swift}}==
<
var n = num
var digits = Array(repeating: 0, count: 10)
Line 1,097:
}
p *= 10
}</
{{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.
<
// assumes l1 is sorted but l2 is not
Line 1,150:
}
i = i + 1
}</
{{out}}
Line 1,165:
=={{header|XPL0}}==
<
int N, Sums;
[Sums:= 0;
Line 1,185:
];
IntOut(0, N);
]</
{{out}}
|