Smallest multiple: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Add Factor)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(42 intermediate revisions by 22 users not shown)
Line 11:
* [[Least common multiple]]
 
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F f(n)
V ans = BigInt(1)
L(i) 1..n
ans *= BigInt(i) I/ gcd(BigInt(i), BigInt(ans))
R ans
 
L(n) [10, 20, 200, 2000]
print(n‘: ’f(n))</syntaxhighlight>
 
{{out}}
<pre>
10: 2520
20: 232792560
200: 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000: 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
</pre>
 
=={{header|ALGOL 68}}==
Line 17 ⟶ 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 55 ⟶ 73:
print( ( whole( tests[ i ], -5 ), ": ", commatise( lcm( tests[ i ] ) ), newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 62 ⟶ 80:
200: 337,293,588,832,926,264,639,465,766,794,841,407,432,394,382,785,157,234,228,847,021,917,234,018,060,677,390,066,992,000
2000: 151,117,794,877,444,315,307,536,308,337,572,822,173,736,308,853,579,339,903,227,904,473,000,476,322,347,234,655,122,160,866,668,946,941,993,951,014,270,933,512,030,194,957,221,371,956,828,843,521,568,082,173,786,251,242,333,157,830,450,435,623,211,664,308,500,316,844,478,617,809,101,158,220,672,108,895,053,508,829,266,120,497,031,742,749,376,045,929,890,296,052,805,527,212,315,382,805,219,353,316,270,742,572,401,962,035,464,878,235,703,759,464,796,806,075,131,056,520,079,836,955,770,415,021,318,508,272,982,103,736,658,633,390,411,347,759,000,563,271,226,062,182,345,964,184,167,346,918,225,243,856,348,794,013,355,418,404,695,826,256,911,622,054,015,423,611,375,261,945,905,974,225,257,659,010,379,414,787,547,681,984,112,941,581,325,198,396,634,685,659,217,861,208,771,400,322,507,388,161,967,513,719,166,366,839,894,214,040,787,733,471,287,845,629,833,993,885,413,462,225,294,548,785,581,641,804,620,417,256,563,685,280,586,511,301,918,399,010,451,347,815,776,570,842,790,738,545,306,707,750,937,624,267,501,103,840,324,470,083,425,714,138,183,905,657,667,736,579,430,274,197,734,179,172,691,637,931,540,695,631,396,056,193,786,415,805,463,680,000
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">print first select.first range.step:20 20 ∞ 'x ->
every? 11..19 'z -> zero? x % z</syntaxhighlight>
 
{{out}}
 
<pre>232792560</pre>
 
=={{header|Asymptote}}==
<syntaxhighlight lang="asymptote">int temp = 2*3*5*7*11*13*17*19;
int smalmul = temp;
int lim = 1;
while (lim <= 20) {
lim = lim + 1;
while (smalmul % lim != 0) {
lim = 1;
smalmul = smalmul + temp;
}
}
write(smalmul);</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">primes := 1
loop 20
if prime_numbers(A_Index).Count() = 1
primes *= A_Index
 
loop
{
Result := A_Index*primes
loop 20
if Mod(Result, A_Index)
continue, 2
break
}
MsgBox % Result
return
 
prime_numbers(n) { ; http://www.rosettacode.org/wiki/Prime_decomposition#Optimized_Version
if (n <= 3)
return [n]
ans := [], done := false
while !done
{
if !Mod(n,2){
ans.push(2), n /= 2
continue
}
if !Mod(n,3) {
ans.push(3), n /= 3
continue
}
if (n = 1)
return ans
sr := sqrt(n), done := true
; try to divide the checked number by all numbers till its square root.
i := 6
while (i <= sr+6){
if !Mod(n, i-1) { ; is n divisible by i-1?
ans.push(i-1), n /= i-1, done := false
break
}
if !Mod(n, i+1) { ; is n divisible by i+1?
ans.push(i+1), n /= i+1, done := false
break
}
i += 6
}
}
ans.push(n)
return ans
}</syntaxhighlight>
{{out}}
<pre>232792560</pre>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">temp = 2*3*5*7*11*13*17*19
smalmul = temp
lim = 1
do
lim += 1
if (smalmul mod lim) then lim = 1 : smalmul += temp
until lim = 20
print smalmul</syntaxhighlight>
{{out}}
<pre>232792560</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
temp.i = 2*3*5*7*11*13*17*19
smalmul.i = temp
lim.i = 1
Repeat
lim + 1
If (smalmul % lim)
lim = 1
smalmul = smalmul + temp
EndIf
Until lim = 20
PrintN(Str(smalmul))
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>232792560</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LET temp = 2*3*5*7*11*13*17*19
LET smalmul = temp
LET lim = 1
DO
LET lim = lim+1
IF (REMAINDER(ROUND(smalmul),ROUND(lim)) <> 0) THEN
LET lim = 1
LET smalmul = smalmul+temp
END IF
LOOP UNTIL lim = 20
PRINT smalmul
END</syntaxhighlight>
{{out}}
<pre>232792560</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsDivisible120(N: integer): boolean;
{Is N evenly divisible by numbers 1..20}
var I: integer;
begin
Result:=False;
{For speed - larger numbers less likely divisor}
for I:=20 downto 2 do
if (N mod I)<>0 then exit;
Result:=True;
end;
 
 
procedure SmallestDivide120(Memo: TMemo);
var I: integer;
begin
{Only look at even numbers for speed}
for I:=1 to High(Integer) do
if IsDivisible120(I*2) then
begin
Memo.Lines.Add(FloatToStrF(I*2,ffNumber,18,0));
break;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
232,792,560
Elapsed Time: 920.406 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight 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>
{{out}}
<pre>
232792560
</pre>
 
=={{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>
232792560
</pre>
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">Func Ilog( n, b ) =
i:=0; {integer logarithm of n to base b, positive only}
while b^i<=n do
i:+;
od;
i-1.;
Func Smalmul( n ) =
s:=1;
for a = 1 to n do
if Isprime(a) then s:=s*a^Ilog(n, a) fi;
od;
s.;
 
!Smalmul(20);
</syntaxhighlight>
{{out}}<pre>232792560</pre>
 
=={{header|FreeBASIC}}==
Use the code from the [[Least common multiple]] example as an include.
<syntaxhighlight lang="freebasic">#include"lcm.bas"
 
redim shared as ulongint smalls(0 to 1) 'calculate and store as we go
smalls(0) = 0: smalls(1) = 1
 
function smalmul(n as longint) as ulongint
if n<0 then return smalmul(-n) 'deal with negative input
dim as uinteger m = ubound(smalls)
if n<=m then return smalls(n) 'have we calculated this already
'if not, make room for the next bunch of terms
redim preserve as ulongint smalls(0 to n)
for i as uinteger = m+1 to n
smalls(i) = lcm(smalls(i-1), i)
next i
return smalls(n)
end function
 
for i as uinteger = 0 to 20
print i, smalmul(i)
next i</syntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 103 ⟶ 339:
fmt.Printf("%4d: %s\n", i, lcm(i))
}
}</langsyntaxhighlight>
 
{{out}}
Line 113 ⟶ 349:
2000: 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Text.Printf (printf)
 
--- SMALLEST INTEGER EVENLY DIVISIBLE BY EACH OF [1..N] --
 
smallest :: Integer -> Integer
smallest =
foldr lcm 1 . enumFromTo 1
 
 
--------------------------- TEST -------------------------
main :: IO ()
main =
(putStrLn . unlines) $
showSmallest <$> [10, 20, 200, 2000]
 
------------------------- DISPLAY ------------------------
showSmallest :: Integer -> String
showSmallest =
((<>) . (<> " -> ") . printf "%4d")
<*> (printf "%d" . smallest)</syntaxhighlight>
{{Out}}
<pre> 10 -> 2520
20 -> 232792560
200 -> 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000 -> 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> *./ >: i. 20
232792560</syntaxhighlight>
 
=={{header|jq}}==
'''Works with jq''' (*)<br>
'''Works with gojq, the Go implementation of jq'''
 
The following uses `is_prime` as defined at [[Erd%C5%91s-primes#jq]].
 
(*) 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.
<syntaxhighlight lang="jq"># Output: a stream of primes less than $n in increasing order
def primes($n):
2, (range(3; $n; 2) | select(is_prime));
 
# lcm of 1 to $n inclusive
def lcm:
. as $n
| reduce primes($n) as $p (1;
. * ($p | until(. * $p > $n; . * $p)) ) ;
 
"N: LCM of the numbers 1 to N inclusive",
( 10, 20, 200, 2000
| "\(.): \(smallest_multiple)" )</syntaxhighlight>
{{out}}
<pre>
N: LCM of the numbers 1 to N inclusive
10: 2520
20: 232792560
200: 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000: 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
</pre>
 
=={{header|Julia}}==
<syntaxhighlight 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>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">LCM @@ Range[20]</syntaxhighlight>
 
{{out}}<pre>
232792560
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec gcd a = function
| 0 -> a
| b -> gcd b (a mod b)
 
let lcm a b =
a * b / gcd a b
 
let smallest_multiple n =
Seq.(ints 1 |> take n |> fold_left lcm 1)
 
let () =
Printf.printf "%u\n" (smallest_multiple 20)</syntaxhighlight>
{{out}}
<pre>232792560</pre>
 
=={{header|Pascal}}==
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 153 ⟶ 481:
{$ENDIF}
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 159 ⟶ 487:
</pre>
===extended===
Findfascinating 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 and other contortions.<BR>
Using prime sieve.
<langsyntaxhighlight lang="pascal">{$IFDEF FPC}
{$MODE DELPHI} {$Optimization On}
{$ELSE}
{$APPTAYPE CONSOLE}
Line 172 ⟶ 500:
{$ENDIF}
sysutils; //format
 
const
UpperLimitMAX_LIMIT = 2*1000*1000;
UpperLimit = MAX_LIMIT+1000;// so to find a prime beyond MAX_LIMIT
MAX_UINT64 = 46;
MAX_UINT64 = 46;// unused.Limit to get an Uint64 output
type
tFactors = array of Uint32;
tprimelist = array of byte;
var
primelistprimeDeltalist : tPrimelist;
factors,
 
saveFactors:tFactors;
saveFactorsIdx,
maxFactorsIdx : Uint32;
procedure Init_Primes;
var
pPrime : pByte;
p ,i,delta,cnt: NativeUInt;
begin
setlength(primelistprimeDeltalist,UpperLimit+3*8+1);
pPrime := @primelistprimeDeltalist[0];
//delete multiples of 2,3
i := 0;
Line 198 ⟶ 529:
inc(i,24);
until i>UpperLimit;
cnt := 2;// 2,3
p := 5;
delta := 1;//5-3
repeat
if pPrime[p] <> 0 then
Line 205 ⟶ 538:
if i > UpperLimit then
break;
inc(cnt);
pPrime[p-2*delta] := delta;
delta := 0;
repeat
pPrime[i] := 0;
Line 211 ⟶ 547:
end;
inc(p,2);
inc(delta);
until p*p>UpperLimit;
setlength(saveFactors,cnt);
pPrime[1] := 0;
//convert to delta
pPrime[2] := 1;
repeat
pPrime[3] := 1;
if pPrime[p]<> 0 then
begin
pPrime[p-2*delta] := delta;
inc(cnt);
delta := 0;
end;
inc(p,2);
inc(delta);
until p > UpperLimit;
setlength(factors,cnt);
factors[0] := 2;
factors[1] := 3;
i := 2;
p := 5;
repeat
factors[i] := p;
p += 2*pPrime[p];
i += 1;
until i >= cnt;
setlength(primeDeltalist,0);
// writeln(length(savefactors)); writeln(length(factors));
end;
 
{$IFDEF USE_GMP}
procedure ConvertToMPZ(const factors:tFactors;dgtCnt:UInt32);
const
c19Digits = QWord(10*1000000)*1000000*1000000;
var
mp,mpdiv : mpz_t;
s : AnsiString;
irest,last : integerUint64;
f : Uint32;
i :int32;
begin
//Init and allocate space
mpz_init(mp);
mpz_init_set_ui(mp,0);
mpz_init(mpdiv);
mpz_ui_pow_ui(mpdiv,10,dgtCnt);
mpz_add(mp,mp,mpdiv);
mpz_add_ui(mp,mp,1);
mpz_set_ui(mp,1);
 
for i := 0 to high(factors) do
i := maxFactorsIdx;
mpz_mul_ui(mp,mp,factors[i]);
irest := mpz_sizeinbase(mp,10)1;
repeat
setlength(s,i+10);
last := rest;
mpz_get_str(@s[1],10,mp);
i f := factors[i+10];
rest *= f;
while not(s[i] in['0'..'9']) do
if rest div f <> last then
begin
mpz_mul_ui(mp,mp,last);
rest := f;
end;
dec(i);
until i < 0;
setlength(s,i+1);
mpz_mul_ui(mp,mp,rest);
if length(s)> 22 then
 
If dgtcnt>40 then
begin
rest := mpz_fdiv_ui(mp,c19Digits);
move(s[i-9],s[13],10);
s[11] := '..';s[12]:= +Format('%.19u',[rest]);
mpz_fdiv_q_ui (mpdiv,mpdiv,c19Digits);
setlength(s,22);
mpz_fdiv_q(mp,mp,mpdiv);
rest := mpz_get_ui(mp);
writeln(rest:19,s);
mpz_clear(mpdiv);
end
else
Begin
setlength(s,dgtCnt+1000);
mpz_get_str(@s[1],10,mp);
writeln(s);
i := length(s);
while not(s[i] in['0'..'9']) do
dec(i);
setlength(s,i+1);
writeln(s);
end;
writeln(s);
mpz_clear(mp);
end;
Line 248 ⟶ 636:
procedure CheckDigits(const factors:tFactors);
var
digCntdgtcnt : extended;
i : integer;
begin
digcntdgtcnt := 0;
for i := 0 to high(factors) do;
repeat
digcnt += ln(factors[i]);
i : dgtcnt += trunc(digcnt/ln(10)+1factors[i]);
inc(i);
writeln(' has ',length(factors):10,' factors and ',i:10,' digits');
until i > maxFactorsIdx;
dgtcnt := trunc(dgtcnt/ln(10))+1;
writeln(' has ',maxFactorsIdx+1:10,' factors and ',dgtcnt:10:0,' digits');
{$IFDEF USE_GMP}
If i <i 10000:= thentrunc(dgtcnt);
if i < 1000*1000 then
ConvertToMPZ(factors);
ConvertToMPZ(factors,i);
{$ENDIF}
end;
Line 266 ⟶ 658:
i : integer;
begin
if length(factors)maxFactorsIdx >15 then
Exit(0);
result := 1;
for i := 0 to high(factors)maxFactorsIdx do
result *= factors[i];
end;
Line 279 ⟶ 671:
begin
result := '';
for i := 0 to high(factors)maxFactorsIdx-1 do
begin
str(factors[i],s);
result += s+'*';
end;
str(factors[High(factors)maxFactorsIdx],s);
result += s;
end;
Line 290 ⟶ 682:
procedure GetFactorList(var factors:tFactors;max:Uint32);
var
pPrimep,f,lf : pByteUint32;
n,f,lf : Uint32;
BEGIN
pPrimep := @primeList[0]2;
n := 2;
lf := 0;
saveFactors[lf] := p;
setlength(factors,lf);
while p*p <= max do
 
while n*n <= max do
Begin
if pPrimesaveFactors[nlf]<>0 then:= p;
beginf := p*p;
while f*p <= max do
setlength(factors,lf+1);
f :*= n*np;
factors[lf] := f;
while f*n <= max do
f*= ninc(lf);
p := factors[lf] := f;
if p= inc(lf)0 then HALT;
end;
inc(n);
end;
if lf>0 then
//the rest are all the primes up to max
saveFactorsIdx := lf-1;
For n := n to max do
repeat
if pPrime[n]<>0 then
Begininc(lf)
until setlength(factors,[lf+1)]>Max;
factors[lf]maxFactorsIdx := nlf-1;
inc(lf);
end;
end;
 
procedure Check(var factors:tFactors;max:Uint32);
var
i: Uint32;
begin
GetFactorList(factors,max);
write(max:10,': ');
if length(factors)maxFactorsIdx>15 then
CheckDigits(factors)
else
writeln(ConvertToUint64(factors):21,' = ',ConvertToStr(factors));
for i := 0 to saveFactorsIdx do
factors[i] := savefactors[i];
end;
 
var
factors:tFactors;
max: Uint32;
BEGIN
Init_Primes;
 
max := 2002;
repeat
check(factors,max);
max *=10;
until max > UpperLimitMAX_LIMIT;
 
For max := MAX_UINT64 downto 2 do
writeln;
For max := 10 to 20 do // < MAX_UINT64
check(factors,max);
{$IFDEF WINDOWS}
READLN;
{$ENDIF}
END.</lang>
</syntaxhighlight>
{{out}}
<pre style="height:300px">
TIO.RUN Real time: 01.203161 s User time: 01.147106 s Sys. time: 0.054049 s CPU share: 9899.8849 %
2002: has 46 factors and 2 90= digits2
20: 232792560 = 16*9*5*7*11*13*17*19
3372935888..0066992000
200: has 46 factors and 90 digits
3372935888329262646..8060677390066992000
2000: has 303 factors and 867 digits
1511177948774443153..3786415805463680000
1511177948..5463680000
20000: has 2262 factors and 8676 digits
4879325627288270518..7411295098112000000
4879325627..8112000000
200000: has 17984 factors and 86871 digits
3942319728529926377..9513860925440000000
2000000: has 148933 factors and 868639 digits
8467191629995920178..6480233472000000000
46: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
{ at home
45: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
20000000: has 1270607 factors and 8686151 digits
44: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
1681437413936981958..6706037760000000000
43: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
200000000: has 11078937 factors and 86857606 digits
42: 219060189739591200 = 32*27*25*7*11*13*17*19*23*29*31*37*41
2000000000: has 98222287 factors and 868583388 digits
41: 219060189739591200 = 32*27*25*7*11*13*17*19*23*29*31*37*41
}
40: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
39: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
38: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
37: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
36: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
35: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
34: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
33: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
32: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
31: 72201776446800 = 16*27*25*7*11*13*17*19*23*29*31
30: 2329089562800 = 16*27*25*7*11*13*17*19*23*29
29: 2329089562800 = 16*27*25*7*11*13*17*19*23*29
28: 80313433200 = 16*27*25*7*11*13*17*19*23
27: 80313433200 = 16*27*25*7*11*13*17*19*23
26: 26771144400 = 16*9*25*7*11*13*17*19*23
25: 26771144400 = 16*9*25*7*11*13*17*19*23
24: 5354228880 = 16*9*5*7*11*13*17*19*23
23: 5354228880 = 16*9*5*7*11*13*17*19*23
22: 232792560 = 16*9*5*7*11*13*17*19
21: 232792560 = 16*9*5*7*11*13*17*19
20: 232792560 = 16*9*5*7*11*13*17*19
19: 232792560 = 16*9*5*7*11*13*17*19
18: 12252240 = 16*9*5*7*11*13*17
17: 12252240 = 16*9*5*7*11*13*17
16: 720720 = 16*9*5*7*11*13
15: 360360 = 8*9*5*7*11*13
14: 360360 = 8*9*5*7*11*13
13: 360360 = 8*9*5*7*11*13
12: 27720 = 8*9*5*7*11
11: 27720 = 8*9*5*7*11
10: 2520 = 8*9*5*7
911: 252027720 = 8*9*5*7*11
812: 84027720 = 8*39*5*7*11
713: 420360360 = 48*39*5*7*11*13
614: 60360360 = 48*39*5*7*11*13
515: 60360360 = 48*39*5*7*11*13
416: 12720720 = 416*9*5*7*11*313
317: 612252240 = 216*9*5*7*11*13*317
218: 212252240 = 216*9*5*7*11*13*17
19: 232792560 = 16*9*5*7*11*13*17*19
20: 232792560 = 16*9*5*7*11*13*17*19
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Smallest_multiple#Raku
use warnings;
use ntheory qw( lcm );
 
print "for $_, it's @{[ lcm(1 .. $_) ]}\n" for 10, 20;</syntaxhighlight>
{{out}}
<pre>
for 10, it's 2520
for 20, it's 232792560
</pre>
 
=={{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.
<!--<syntaxhighlight lang="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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
232792560
</pre>
Using gmp
{{trans|Wren}}
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">plcmz</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: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">*</span><span style="color: #000000;">p</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">p</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"%,5d: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
The LCMs of the numbers 1 to N inclusive is:
10: 2,520
20: 232,792,560
200: 337,293,588,832,926,...,677,390,066,992,000 (90 digits)
2,000: 151,117,794,877,444,...,415,805,463,680,000 (867 digits)
</pre>
 
=={{header|Picat}}==
===lcm/2===
<code>lcm/2</code> is defined as:
<syntaxhighlight lang="picat">lcm(X,Y) = X*Y//gcd(X,Y).</syntaxhighlight>
 
===Iteration===
<syntaxhighlight lang="picat">smallest_multiple_range1(N) = A =>
A = 1,
foreach(E in 2..N)
A := lcm(A,E)
end.</syntaxhighlight>
 
===fold/3===
<syntaxhighlight lang="picat">smallest_multiple_range2(N) = fold(lcm, 1, 2..N).</syntaxhighlight>
 
===reduce/2===
<syntaxhighlight lang="picat">smallest_multiple_range3(N) = reduce(lcm, 2..N).</syntaxhighlight>
 
 
===Testing===
Of the three implementations the <code>fold/3</code> approach is slightly faster than the other two.
<syntaxhighlight lang="picat">main =>
foreach(N in [10,20,200,2000])
println(N=smallest_multiple_range2(N))
end.</syntaxhighlight>
 
{{out}}
<pre>10 = 2520
20 = 232792560
200 = 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000 = 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">""" Rosetta code task: Smallest_multiple """
 
from math import gcd
from functools import reduce
 
 
def lcm(a, b):
""" least common multiple """
return 0 if 0 == a or 0 == b else (
abs(a * b) // gcd(a, b)
)
 
 
for i in [10, 20, 200, 2000]:
print(str(i) + ':', reduce(lcm, range(1, i + 1)))</syntaxhighlight>
{{out}}
<pre>10: 2520
20: 232792560
200: 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000: 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000</pre>
 
=={{header|Quackery}}==
<code>lcm</code> is defined at [[Least common multiple#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 1 swap times [ i 1+ lcm ] ] is smalmul ( n --> n )
 
' [ 10 20 200 2000 ] witheach [ dup echo say ": " smalmul echo cr ]</syntaxhighlight>
 
{{out}}
 
<pre>10: 2520
20: 232792560
200: 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000: 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000</pre>
 
=={{header|Raku}}==
Exercise with some larger values as well.
 
<syntaxhighlight lang="raku" perl6line>say "$_: ", [lcm] 2..$_ for <10 20 200 2000></langsyntaxhighlight>
 
{{out}}
Line 418 ⟶ 905:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Smallest multiple is:" + nl
Line 438 ⟶ 925:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 446 ⟶ 933:
done...
</pre>
=={{header|RPL}}==
{{trans|BASIC}}
≪ 2 3 * 5 * 7 * 9 * 11 * 13 * 17 * 19 * → t
≪ t 2 20 '''FOR''' lim
'''IF''' DUP lim MOD '''THEN''' 1 'lim' STO t + '''END NEXT'''
≫ ≫ '<span style="color:blue">TASK</span>' STO
With <code>LCM</code> defined at [[Least common multiple#RPL|Least common multiple]]:
≪ 1 2 20 '''FOR''' n n <span style="color:blue">LCM</span> '''NEXT''' ≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: 232792560
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
[10, 20, 200, 2000].each {|n| puts "#{n}: #{(1..n).inject(&:lcm)}" }</syntaxhighlight>
{{out}}
<pre>10: 2520
20: 232792560
200: 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000: 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
</pre>
 
=={{header|Verilog}}==
{{trans|Yabasic}}
<syntaxhighlight lang="verilog">module main;
integer temp, smalmul, lim;
initial begin
temp = 2*3*5*7*11*13*17*19;
smalmul = temp;
lim = 1;
while (lim <= 20) begin
lim = lim + 1;
while (smalmul % lim != 0) begin
lim = 1;
smalmul = smalmul + temp;
end
end
 
$display(smalmul);
$finish ;
end
endmodule</syntaxhighlight>
{{out}}
<pre>232792560</pre>
 
 
=={{header|Wren}}==
Line 455 ⟶ 990:
 
More formally and quite quick by Wren standards at 0.017 seconds:
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./big" for BigInt
import "./fmt" for Fmt
Line 471 ⟶ 1,006:
 
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 481 ⟶ 1,016:
2,000: 151,117,794,877,444,315,307,536,308,337,572,822,173,736,308,853,579,339,903,227,904,473,000,476,322,347,234,655,122,160,866,668,946,941,993,951,014,270,933,512,030,194,957,221,371,956,828,843,521,568,082,173,786,251,242,333,157,830,450,435,623,211,664,308,500,316,844,478,617,809,101,158,220,672,108,895,053,508,829,266,120,497,031,742,749,376,045,929,890,296,052,805,527,212,315,382,805,219,353,316,270,742,572,401,962,035,464,878,235,703,759,464,796,806,075,131,056,520,079,836,955,770,415,021,318,508,272,982,103,736,658,633,390,411,347,759,000,563,271,226,062,182,345,964,184,167,346,918,225,243,856,348,794,013,355,418,404,695,826,256,911,622,054,015,423,611,375,261,945,905,974,225,257,659,010,379,414,787,547,681,984,112,941,581,325,198,396,634,685,659,217,861,208,771,400,322,507,388,161,967,513,719,166,366,839,894,214,040,787,733,471,287,845,629,833,993,885,413,462,225,294,548,785,581,641,804,620,417,256,563,685,280,586,511,301,918,399,010,451,347,815,776,570,842,790,738,545,306,707,750,937,624,267,501,103,840,324,470,083,425,714,138,183,905,657,667,736,579,430,274,197,734,179,172,691,637,931,540,695,631,396,056,193,786,415,805,463,680,000
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">int N, D;
[N:= 2*3*5*7*11*13*17*19;
D:= 1;
repeat D:= D+1;
if rem(N/D) then
[D:= 1; N:= N + 2*3*5*7*11*13*17*19];
until D = 20;
IntOut(0, N);
]</syntaxhighlight>
 
{{out}}
<pre>
232792560
</pre>
 
=={{header|Yabasic}}==
{{trans|XPL0}}
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Smallest_multiple
// by Galileo, 05/2022
 
M = 2*3*5*7*11*13*17*19
N = M
D = 1
repeat
D = D + 1
if mod(N, D) D = 1 : N = N + M
until D = 20
print N</syntaxhighlight>
{{out}}
<pre>232792560
---Program done, press RETURN---</pre>
9,476

edits