Smallest multiple: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 13: Line 13:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>F f(n)
<syntaxhighlight lang="11l">F f(n)
V ans = BigInt(1)
V ans = BigInt(1)
L(i) 1..n
L(i) 1..n
Line 20: Line 20:


L(n) [10, 20, 200, 2000]
L(n) [10, 20, 200, 2000]
print(n‘: ’f(n))</lang>
print(n‘: ’f(n))</syntaxhighlight>


{{out}}
{{out}}
Line 35: Line 35:
Uses Algol 68G's LONG LONG INT which has specifiable precision.
Uses Algol 68G's LONG LONG INT which has specifiable precision.
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find the smallest number that is divisible by each of the numbers 1..n #
<syntaxhighlight lang="algol68">BEGIN # find the smallest number that is divisible by each of the numbers 1..n #
# translation of the Wren sample #
# translation of the Wren sample #
PR precision 1000 PR # set the precision of LONG LONG INT #
PR precision 1000 PR # set the precision of LONG LONG INT #
Line 73: Line 73:
print( ( whole( tests[ i ], -5 ), ": ", commatise( lcm( tests[ i ] ) ), newline ) )
print( ( whole( tests[ i ], -5 ), ": ", commatise( lcm( tests[ i ] ) ), newline ) )
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 83: Line 83:


=={{header|Asymptote}}==
=={{header|Asymptote}}==
<lang Asymptote>int temp = 2*3*5*7*11*13*17*19;
<syntaxhighlight lang="asymptote">int temp = 2*3*5*7*11*13*17*19;
int smalmul = temp;
int smalmul = temp;
int lim = 1;
int lim = 1;
Line 93: Line 93:
}
}
}
}
write(smalmul);</lang>
write(smalmul);</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>primes := 1
<syntaxhighlight lang="autohotkey">primes := 1
loop 20
loop 20
if prime_numbers(A_Index).Count() = 1
if prime_numbers(A_Index).Count() = 1
Line 145: Line 145:
ans.push(n)
ans.push(n)
return ans
return ans
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>232792560</pre>
<pre>232792560</pre>
Line 152: Line 152:
=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang freebasic>temp = 2*3*5*7*11*13*17*19
<syntaxhighlight lang="freebasic">temp = 2*3*5*7*11*13*17*19
smalmul = temp
smalmul = temp
lim = 1
lim = 1
Line 159: Line 159:
if (smalmul mod lim) then lim = 1 : smalmul += temp
if (smalmul mod lim) then lim = 1 : smalmul += temp
until lim = 20
until lim = 20
print smalmul</lang>
print smalmul</syntaxhighlight>
{{out}}
{{out}}
<pre>232792560</pre>
<pre>232792560</pre>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>OpenConsole()
<syntaxhighlight lang="purebasic">OpenConsole()
temp.i = 2*3*5*7*11*13*17*19
temp.i = 2*3*5*7*11*13*17*19
smalmul.i = temp
smalmul.i = temp
Line 177: Line 177:
PrintN(Str(smalmul))
PrintN(Str(smalmul))
Input()
Input()
CloseConsole()</lang>
CloseConsole()</syntaxhighlight>
{{out}}
{{out}}
<pre>232792560</pre>
<pre>232792560</pre>


==={{header|True BASIC}}===
==={{header|True BASIC}}===
<lang QBasic>LET temp = 2*3*5*7*11*13*17*19
<syntaxhighlight lang="qbasic">LET temp = 2*3*5*7*11*13*17*19
LET smalmul = temp
LET smalmul = temp
LET lim = 1
LET lim = 1
Line 193: Line 193:
LOOP UNTIL lim = 20
LOOP UNTIL lim = 20
PRINT smalmul
PRINT smalmul
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>232792560</pre>
<pre>232792560</pre>
Line 200: Line 200:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Least Multiple. Nigel Galloway: October 22nd., 2021
// Least Multiple. Nigel Galloway: October 22nd., 2021
let fG n g=let rec fN i=match i*g with g when n>g->fN g |_->i in fN g
let fG n g=let rec fN i=match i*g with g when n>g->fN g |_->i in fN g
let leastMult n=let fG=fG n in primes32()|>Seq.takeWhile((>=)n)|>Seq.map fG|>Seq.reduce((*))
let leastMult n=let fG=fG n in primes32()|>Seq.takeWhile((>=)n)|>Seq.map fG|>Seq.reduce((*))
printfn $"%d{leastMult 20}"
printfn $"%d{leastMult 20}"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 212: Line 212:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.98}}
{{works with|Factor|0.98}}
<lang factor>USING: math.functions math.ranges prettyprint sequences ;
<syntaxhighlight lang="factor">USING: math.functions math.ranges prettyprint sequences ;


20 [1,b] 1 [ lcm ] reduce .</lang>
20 [1,b] 1 [ lcm ] reduce .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 221: Line 221:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>Func Ilog( n, b ) =
<syntaxhighlight lang="fermat">Func Ilog( n, b ) =
i:=0; {integer logarithm of n to base b, positive only}
i:=0; {integer logarithm of n to base b, positive only}
while b^i<=n do
while b^i<=n do
Line 236: Line 236:


!Smalmul(20);
!Smalmul(20);
</syntaxhighlight>
</lang>
{{out}}<pre>232792560</pre>
{{out}}<pre>232792560</pre>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
Use the code from the [[Least common multiple]] example as an include.
Use the code from the [[Least common multiple]] example as an include.
<lang freebasic>#include"lcm.bas"
<syntaxhighlight lang="freebasic">#include"lcm.bas"


redim shared as ulongint smalls(0 to 1) 'calculate and store as we go
redim shared as ulongint smalls(0 to 1) 'calculate and store as we go
Line 260: Line 260:
for i as uinteger = 0 to 20
for i as uinteger = 0 to 20
print i, smalmul(i)
print i, smalmul(i)
next i</lang>
next i</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 291: Line 291:
fmt.Printf("%4d: %s\n", i, lcm(i))
fmt.Printf("%4d: %s\n", i, lcm(i))
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 303: Line 303:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Text.Printf (printf)
<syntaxhighlight lang="haskell">import Text.Printf (printf)


--- SMALLEST INTEGER EVENLY DIVISIBLE BY EACH OF [1..N] --
--- SMALLEST INTEGER EVENLY DIVISIBLE BY EACH OF [1..N] --
Line 322: Line 322:
showSmallest =
showSmallest =
((<>) . (<> " -> ") . printf "%4d")
((<>) . (<> " -> ") . printf "%4d")
<*> (printf "%d" . smallest)</lang>
<*> (printf "%d" . smallest)</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 10 -> 2520
<pre> 10 -> 2520
Line 337: Line 337:
(*) The C implementation of jq has sufficient accuracy for N == 20 but not N == 200,
(*) The C implementation of jq has sufficient accuracy for N == 20 but not N == 200,
so the output shown below is based on a run of gojq.
so the output shown below is based on a run of gojq.
<lang jq># Output: a stream of primes less than $n in increasing order
<syntaxhighlight lang="jq"># Output: a stream of primes less than $n in increasing order
def primes($n):
def primes($n):
2, (range(3; $n; 2) | select(is_prime));
2, (range(3; $n; 2) | select(is_prime));
Line 349: Line 349:
"N: LCM of the numbers 1 to N inclusive",
"N: LCM of the numbers 1 to N inclusive",
( 10, 20, 200, 2000
( 10, 20, 200, 2000
| "\(.): \(smallest_multiple)" )</lang>
| "\(.): \(smallest_multiple)" )</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 360: Line 360:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>julia> foreach(x -> @show(lcm(x)), [1:10, 1:20, big"1":200, big"1":2000])
<syntaxhighlight lang="julia">julia> foreach(x -> @show(lcm(x)), [1:10, 1:20, big"1":200, big"1":2000])
lcm(x) = 2520
lcm(x) = 2520
lcm(x) = 232792560
lcm(x) = 232792560
lcm(x) = 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
lcm(x) = 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
lcm(x) = 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
lcm(x) = 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
</syntaxhighlight>
</lang>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>LCM @@ Range[20]</lang>
<syntaxhighlight lang="mathematica">LCM @@ Range[20]</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 376: Line 376:
Here the simplest way, like Raku, check the highest exponent of every prime in range<BR>
Here the simplest way, like Raku, check the highest exponent of every prime in range<BR>
Using harded coded primes.
Using harded coded primes.
<lang pascal>{$IFDEF FPC}
<syntaxhighlight lang="pascal">{$IFDEF FPC}
{$MODE DELPHI}
{$MODE DELPHI}
{$ELSE}
{$ELSE}
Line 412: Line 412:
{$ENDIF}
{$ENDIF}
END.
END.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 420: Line 420:
fascinating find, that the count of digits is nearly a constant x upper rangelimit.<br> The number of factors is the count of primes til limit.See GetFactorList.<br>No need for calculating lcm(lcm(lcm(1,2),3),4..) or prime decomposition<BR>
fascinating find, that the count of digits is nearly a constant x upper rangelimit.<br> The number of factors is the count of primes til limit.See GetFactorList.<br>No need for calculating lcm(lcm(lcm(1,2),3),4..) or prime decomposition<BR>
Using prime sieve.
Using prime sieve.
<lang pascal>{$IFDEF FPC}
<syntaxhighlight lang="pascal">{$IFDEF FPC}
{$MODE DELPHI} {$Optimization On}
{$MODE DELPHI} {$Optimization On}
{$ELSE}
{$ELSE}
Line 669: Line 669:
{$ENDIF}
{$ENDIF}
END.
END.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre style="height:300px">
<pre style="height:300px">
Line 705: Line 705:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Smallest_multiple#Raku
use strict; # https://rosettacode.org/wiki/Smallest_multiple#Raku
Line 711: Line 711:
use ntheory qw( lcm );
use ntheory qw( lcm );


print "for $_, it's @{[ lcm(1 .. $_) ]}\n" for 10, 20;</lang>
print "for $_, it's @{[ lcm(1 .. $_) ]}\n" for 10, 20;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 721: Line 721:
=={{header|Phix}}==
=={{header|Phix}}==
Using the builtin, limited to 2<small><sup>53</sup></small> aka N=36 on 32-bit, 2<small><sup>64</sup></small> aka N=46 on 64-bit.
Using the builtin, limited to 2<small><sup>53</sup></small> aka N=36 on 32-bit, 2<small><sup>64</sup></small> aka N=46 on 64-bit.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">lcm</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">lcm</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 731: Line 731:
Using gmp
Using gmp
{{trans|Wren}}
{{trans|Wren}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 747: Line 747:
<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;">"The LCMs of the numbers 1 to N inclusive is:\n"</span><span style="color: #0000FF;">)</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;">"The LCMs of the numbers 1 to N inclusive is:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">200</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2000</span><span style="color: #0000FF;">},</span><span style="color: #000000;">plcmz</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">200</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2000</span><span style="color: #0000FF;">},</span><span style="color: #000000;">plcmz</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 760: Line 760:
===lcm/2===
===lcm/2===
<code>lcm/2</code> is defined as:
<code>lcm/2</code> is defined as:
<lang Picat>lcm(X,Y) = X*Y//gcd(X,Y).</lang>
<syntaxhighlight lang="picat">lcm(X,Y) = X*Y//gcd(X,Y).</syntaxhighlight>


===Iteration===
===Iteration===
<lang Picat>smallest_multiple_range1(N) = A =>
<syntaxhighlight lang="picat">smallest_multiple_range1(N) = A =>
A = 1,
A = 1,
foreach(E in 2..N)
foreach(E in 2..N)
A := lcm(A,E)
A := lcm(A,E)
end.</lang>
end.</syntaxhighlight>


===fold/3===
===fold/3===
<lang Picat>smallest_multiple_range2(N) = fold(lcm, 1, 2..N).</lang>
<syntaxhighlight lang="picat">smallest_multiple_range2(N) = fold(lcm, 1, 2..N).</syntaxhighlight>


===reduce/2===
===reduce/2===
<lang Picat>smallest_multiple_range3(N) = reduce(lcm, 2..N).</lang>
<syntaxhighlight lang="picat">smallest_multiple_range3(N) = reduce(lcm, 2..N).</syntaxhighlight>




===Testing===
===Testing===
Of the three implementations the <code>fold/3</code> approach is slightly faster than the other two.
Of the three implementations the <code>fold/3</code> approach is slightly faster than the other two.
<lang Picat>main =>
<syntaxhighlight lang="picat">main =>
foreach(N in [10,20,200,2000])
foreach(N in [10,20,200,2000])
println(N=smallest_multiple_range2(N))
println(N=smallest_multiple_range2(N))
end.</lang>
end.</syntaxhighlight>


{{out}}
{{out}}
Line 790: Line 790:


=={{header|Python}}==
=={{header|Python}}==
<lang python>""" Rosetta code task: Smallest_multiple """
<syntaxhighlight lang="python">""" Rosetta code task: Smallest_multiple """


from math import gcd
from math import gcd
Line 804: Line 804:


for i in [10, 20, 200, 2000]:
for i in [10, 20, 200, 2000]:
print(str(i) + ':', reduce(lcm, range(1, i + 1)))</lang>
print(str(i) + ':', reduce(lcm, range(1, i + 1)))</syntaxhighlight>
{{out}}
{{out}}
<pre>10: 2520
<pre>10: 2520
Line 814: Line 814:
Exercise with some larger values as well.
Exercise with some larger values as well.


<lang perl6>say "$_: ", [lcm] 2..$_ for <10 20 200 2000></lang>
<syntaxhighlight lang="raku" line>say "$_: ", [lcm] 2..$_ for <10 20 200 2000></syntaxhighlight>


{{out}}
{{out}}
Line 823: Line 823:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
see "working..." + nl
see "working..." + nl
see "Smallest multiple is:" + nl
see "Smallest multiple is:" + nl
Line 843: Line 843:


see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 855: Line 855:
=={{header|Verilog}}==
=={{header|Verilog}}==
{{trans|Yabasic}}
{{trans|Yabasic}}
<lang Verilog>module main;
<syntaxhighlight lang="verilog">module main;
integer temp, smalmul, lim;
integer temp, smalmul, lim;
Line 874: Line 874:
$finish ;
$finish ;
end
end
endmodule</lang>
endmodule</syntaxhighlight>
{{out}}
{{out}}
<pre>232792560</pre>
<pre>232792560</pre>
Line 887: Line 887:


More formally and quite quick by Wren standards at 0.017 seconds:
More formally and quite quick by Wren standards at 0.017 seconds:
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./big" for BigInt
import "./big" for BigInt
import "./fmt" for Fmt
import "./fmt" for Fmt
Line 903: Line 903:


System.print("The LCMs of the numbers 1 to N inclusive is:")
System.print("The LCMs of the numbers 1 to N inclusive is:")
for (i in [10, 20, 200, 2000]) Fmt.print("$,5d: $,i", i, lcm.call(i))</lang>
for (i in [10, 20, 200, 2000]) Fmt.print("$,5d: $,i", i, lcm.call(i))</syntaxhighlight>


{{out}}
{{out}}
Line 915: Line 915:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>int N, D;
<syntaxhighlight lang="xpl0">int N, D;
[N:= 2*3*5*7*11*13*17*19;
[N:= 2*3*5*7*11*13*17*19;
D:= 1;
D:= 1;
Line 923: Line 923:
until D = 20;
until D = 20;
IntOut(0, N);
IntOut(0, N);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 932: Line 932:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|XPL0}}
{{trans|XPL0}}
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Smallest_multiple
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Smallest_multiple
// by Galileo, 05/2022
// by Galileo, 05/2022


Line 942: Line 942:
if mod(N, D) D = 1 : N = N + M
if mod(N, D) D = 1 : N = N + M
until D = 20
until D = 20
print N</lang>
print N</syntaxhighlight>
{{out}}
{{out}}
<pre>232792560
<pre>232792560