Jump to content

Smallest multiple: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 13:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F f(n)
V ans = BigInt(1)
L(i) 1..n
Line 20:
 
L(n) [10, 20, 200, 2000]
print(n‘: ’f(n))</langsyntaxhighlight>
 
{{out}}
Line 35:
Uses Algol 68G's LONG LONG INT which has specifiable precision.
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find the smallest number that is divisible by each of the numbers 1..n #
# translation of the Wren sample #
PR precision 1000 PR # set the precision of LONG LONG INT #
Line 73:
print( ( whole( tests[ i ], -5 ), ": ", commatise( lcm( tests[ i ] ) ), newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 83:
 
=={{header|Asymptote}}==
<langsyntaxhighlight Asymptotelang="asymptote">int temp = 2*3*5*7*11*13*17*19;
int smalmul = temp;
int lim = 1;
Line 93:
}
}
write(smalmul);</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">primes := 1
loop 20
if prime_numbers(A_Index).Count() = 1
Line 145:
ans.push(n)
return ans
}</langsyntaxhighlight>
{{out}}
<pre>232792560</pre>
Line 152:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight lang="freebasic">temp = 2*3*5*7*11*13*17*19
smalmul = temp
lim = 1
Line 159:
if (smalmul mod lim) then lim = 1 : smalmul += temp
until lim = 20
print smalmul</langsyntaxhighlight>
{{out}}
<pre>232792560</pre>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">OpenConsole()
temp.i = 2*3*5*7*11*13*17*19
smalmul.i = temp
Line 177:
PrintN(Str(smalmul))
Input()
CloseConsole()</langsyntaxhighlight>
{{out}}
<pre>232792560</pre>
 
==={{header|True BASIC}}===
<langsyntaxhighlight QBasiclang="qbasic">LET temp = 2*3*5*7*11*13*17*19
LET smalmul = temp
LET lim = 1
Line 193:
LOOP UNTIL lim = 20
PRINT smalmul
END</langsyntaxhighlight>
{{out}}
<pre>232792560</pre>
Line 200:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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 leastMult n=let fG=fG n in primes32()|>Seq.takeWhile((>=)n)|>Seq.map fG|>Seq.reduce((*))
printfn $"%d{leastMult 20}"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 212:
=={{header|Factor}}==
{{works with|Factor|0.98}}
<langsyntaxhighlight lang="factor">USING: math.functions math.ranges prettyprint sequences ;
 
20 [1,b] 1 [ lcm ] reduce .</langsyntaxhighlight>
{{out}}
<pre>
Line 221:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Ilog( n, b ) =
i:=0; {integer logarithm of n to base b, positive only}
while b^i<=n do
Line 236:
 
!Smalmul(20);
</syntaxhighlight>
</lang>
{{out}}<pre>232792560</pre>
 
=={{header|FreeBASIC}}==
Use the code from the [[Least common multiple]] example as an include.
<langsyntaxhighlight lang="freebasic">#include"lcm.bas"
 
redim shared as ulongint smalls(0 to 1) 'calculate and store as we go
Line 260:
for i as uinteger = 0 to 20
print i, smalmul(i)
next i</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 291:
fmt.Printf("%4d: %s\n", i, lcm(i))
}
}</langsyntaxhighlight>
 
{{out}}
Line 303:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
 
--- SMALLEST INTEGER EVENLY DIVISIBLE BY EACH OF [1..N] --
Line 322:
showSmallest =
((<>) . (<> " -> ") . printf "%4d")
<*> (printf "%d" . smallest)</langsyntaxhighlight>
{{Out}}
<pre> 10 -> 2520
Line 337:
(*) 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.
<langsyntaxhighlight lang="jq"># Output: a stream of primes less than $n in increasing order
def primes($n):
2, (range(3; $n; 2) | select(is_prime));
Line 349:
"N: LCM of the numbers 1 to N inclusive",
( 10, 20, 200, 2000
| "\(.): \(smallest_multiple)" )</langsyntaxhighlight>
{{out}}
<pre>
Line 360:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">julia> foreach(x -> @show(lcm(x)), [1:10, 1:20, big"1":200, big"1":2000])
lcm(x) = 2520
lcm(x) = 232792560
lcm(x) = 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
lcm(x) = 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
</syntaxhighlight>
</lang>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">LCM @@ Range[20]</langsyntaxhighlight>
 
{{out}}<pre>
Line 376:
Here the simplest way, like Raku, check the highest exponent of every prime in range<BR>
Using harded coded primes.
<langsyntaxhighlight lang="pascal">{$IFDEF FPC}
{$MODE DELPHI}
{$ELSE}
Line 412:
{$ENDIF}
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
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>
Using prime sieve.
<langsyntaxhighlight lang="pascal">{$IFDEF FPC}
{$MODE DELPHI} {$Optimization On}
{$ELSE}
Line 669:
{$ENDIF}
END.
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:300px">
Line 705:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Smallest_multiple#Raku
Line 711:
use ntheory qw( lcm );
 
print "for $_, it's @{[ lcm(1 .. $_) ]}\n" for 10, 20;</langsyntaxhighlight>
{{out}}
<pre>
Line 721:
=={{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 731:
Using gmp
{{trans|Wren}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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;">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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 760:
===lcm/2===
<code>lcm/2</code> is defined as:
<langsyntaxhighlight Picatlang="picat">lcm(X,Y) = X*Y//gcd(X,Y).</langsyntaxhighlight>
 
===Iteration===
<langsyntaxhighlight Picatlang="picat">smallest_multiple_range1(N) = A =>
A = 1,
foreach(E in 2..N)
A := lcm(A,E)
end.</langsyntaxhighlight>
 
===fold/3===
<langsyntaxhighlight Picatlang="picat">smallest_multiple_range2(N) = fold(lcm, 1, 2..N).</langsyntaxhighlight>
 
===reduce/2===
<langsyntaxhighlight Picatlang="picat">smallest_multiple_range3(N) = reduce(lcm, 2..N).</langsyntaxhighlight>
 
 
===Testing===
Of the three implementations the <code>fold/3</code> approach is slightly faster than the other two.
<langsyntaxhighlight Picatlang="picat">main =>
foreach(N in [10,20,200,2000])
println(N=smallest_multiple_range2(N))
end.</langsyntaxhighlight>
 
{{out}}
Line 790:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">""" Rosetta code task: Smallest_multiple """
 
from math import gcd
Line 804:
 
for i in [10, 20, 200, 2000]:
print(str(i) + ':', reduce(lcm, range(1, i + 1)))</langsyntaxhighlight>
{{out}}
<pre>10: 2520
Line 814:
Exercise with some larger values as well.
 
<syntaxhighlight lang="raku" perl6line>say "$_: ", [lcm] 2..$_ for <10 20 200 2000></langsyntaxhighlight>
 
{{out}}
Line 823:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Smallest multiple is:" + nl
Line 843:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 855:
=={{header|Verilog}}==
{{trans|Yabasic}}
<langsyntaxhighlight Veriloglang="verilog">module main;
integer temp, smalmul, lim;
Line 874:
$finish ;
end
endmodule</langsyntaxhighlight>
{{out}}
<pre>232792560</pre>
Line 887:
 
More formally and quite quick by Wren standards at 0.017 seconds:
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
import "./big" for BigInt
import "./fmt" for Fmt
Line 903:
 
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))</langsyntaxhighlight>
 
{{out}}
Line 915:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int N, D;
[N:= 2*3*5*7*11*13*17*19;
D:= 1;
Line 923:
until D = 20;
IntOut(0, N);
]</langsyntaxhighlight>
 
{{out}}
Line 932:
=={{header|Yabasic}}==
{{trans|XPL0}}
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Smallest_multiple
// by Galileo, 05/2022
 
Line 942:
if mod(N, D) D = 1 : N = N + M
until D = 20
print N</langsyntaxhighlight>
{{out}}
<pre>232792560
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.