lcm - Perl - least common multiple -
this code should lcm n numbers.
i tried put prints wherever can in order see mistake. , think in:
if($vec[0] == $vec[$n-1]){ $resultado = $vec[0]; last; }
but can not make work. please me?
i'm rookie perl. hope can solve problem.
tried change variables not work. mean
$u = 0 , $w = $n-1;
to lcm, can split task multiple subroutines:
- is_prime # returns true if value prime
- roots # returns roots of number (all prime numbers make value. ex: roots of 12 are: 2, 2, 3)
- lcm # takes list of values. extract roots while number not prime. store in hash , increment everytime see root
so we'll have hash like:
%sub_total = ( value => times_found, 2 => 2, 3 => 1, );
we have hash total. if sub_total hash has key used more in total hash, add total.
finally, loop through total hash , and find product using algorithm:
for (%total){ $prod *= $_ ** $total {$_}; }
note
i'll shortly attach code wrote getting lcm. it's not me here.
Comments
Post a Comment