Smallest multiple: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Raku}}: Add a Raku example)
(added pascal version)
Line 6: Line 6:
<br>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
<br>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
<br>What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
<br>What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
=={{header|Pascal}}==
Here the simplest way, like Raku, check the highest exponent of every prime in range<BR>
Using harded coded primes.
<lang pascal>{$IFDEF FPC}
{$MODE DELPHI}
{$ELSE}
{$APPTAYPE CONSOLE}
{$ENDIF}
const
smallprimes : array[0..10] of Uint32 = (2,3,5,7,11,13,17,19,23,29,31);
MAX = 20;

function getmaxfac(pr: Uint32): Uint32;
//get the pr^highest exponent of prime used in 2 .. MAX
var
i,fac : integer;
Begin
result := pr;
while pr*result <= MAX do
result *= pr;
end;

var
n,pr,prIdx : Uint32;
BEGIN
n := 1;
prIdx := 0;
pr := smallprimes[prIdx];
repeat
pr := smallprimes[prIdx];
n *= getmaxfac(pr);
inc(prIdx);
pr := smallprimes[prIdx];
until pr>MAX;
writeln(n);
{$IFDEF WINDOWS}
READLN;
{$ENDIF}
END.
</lang>
{{out}}
<pre>
232792560
</pre>
=={{header|Raku}}==
=={{header|Raku}}==
Exercise with some larger values as well.
Exercise with some larger values as well.

Revision as of 08:22, 21 October 2021

Smallest multiple is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Task desciption is taken from Project Euler
(https://projecteuler.net/problem=5)
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Pascal

Here the simplest way, like Raku, check the highest exponent of every prime in range
Using harded coded primes. <lang pascal>{$IFDEF FPC}

 {$MODE DELPHI}

{$ELSE}

 {$APPTAYPE CONSOLE}

{$ENDIF} const

smallprimes : array[0..10] of Uint32 = (2,3,5,7,11,13,17,19,23,29,31);
MAX = 20;

function getmaxfac(pr: Uint32): Uint32; //get the pr^highest exponent of prime used in 2 .. MAX var

 i,fac : integer;

Begin

 result := pr;
 while pr*result <= MAX do
   result *= pr;

end;

var

 n,pr,prIdx : Uint32;

BEGIN

 n := 1;
 prIdx := 0;
 pr := smallprimes[prIdx];
 repeat
   pr := smallprimes[prIdx];
   n *= getmaxfac(pr);
   inc(prIdx);
   pr := smallprimes[prIdx];
 until pr>MAX;
 writeln(n);

{$IFDEF WINDOWS}

 READLN;

{$ENDIF} END. </lang>

Output:
  232792560

Raku

Exercise with some larger values as well.

<lang perl6>use Prime::Factor;

sub minimum-multiple ($n where * > 1) {

   my %max-factor;
   for 2..$n { .&prime-factors.Bag.map: { %max-factor{.key} max= .value } }
   [*] flat %max-factor.map: { .key xx .value }

}

say "$_: ", .&minimum-multiple for <10 20 200 2000>; printf "%.3f seconds elapsed\n", now - INIT now;</lang>

Output:
10: 2520
20: 232792560
200: 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000
2000: 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000
0.315 seconds elapsed

Ring

<lang ring> see "working..." + nl see "Smallest multiple is:" + nl n = 0

while true

     n++
     flag = 0
     for m = 1 to 20
         if n % m = 0
            flag += 1
         ok
     next
     if flag = 20
        see "" + n + nl
        exit
     ok

end

see "done..." + nl </lang>

Output:
working...
Smallest multiple is:
232792560
done...