# HG changeset patch # User Heiko Schlittermann (JUMPER) # Date 1319917761 -7200 # Node ID 1519d83aa83baa526c6e8d26fa411500f0f0b2a6 # Parent 22e93ecd595f4ac295b1082b58ca536526db3ba5 [perltidy] diff -r 22e93ecd595f -r 1519d83aa83b .perltidyrc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.perltidyrc Sat Oct 29 21:49:21 2011 +0200 @@ -0,0 +1,2 @@ +--paren-tightness=2 +--square-bracket-tightness=2 diff -r 22e93ecd595f -r 1519d83aa83b prime --- a/prime Sat Oct 29 21:48:44 2011 +0200 +++ b/prime Sat Oct 29 21:49:21 2011 +0200 @@ -12,6 +12,7 @@ my $ME = basename $0; if (not @ARGV) { + # FIXME: be more verbose die "$ME: Sorry, need the number\n"; } @@ -25,7 +26,7 @@ my @primes = (); #for each element in array divisors -foreach my $div (@divisors) { #same element in div +foreach my $div (@divisors) { #same element in div #find all divisors of div and save in subprimes my @subprimes = findDivisors($div); @@ -33,7 +34,7 @@ #determine length of subprimes and save in noOfDivisors my $noOfDivisors = scalar(@subprimes); - if ($noOfDivisors == 0) {@primes = (@primes, $div); } + if ($noOfDivisors == 0) { @primes = (@primes, $div); } } #print"Die Teile von $number sind: ".join(", ", @divisors)."\n"; @@ -43,13 +44,14 @@ @primes = factorize($number, @primes); if (@primes) { - print join("*", @primes), "\n"; + print join("*", @primes), "\n"; } else { + #print STDERR "Sorry, $number is prime itself!\n"; warn "Sorry, $number is prime itself\n!"; } - + exit; #### SUBROUTINES ##### @@ -57,30 +59,31 @@ sub findDivisors { my $i = 2; my $m = $_[0]; - + my @divisors = (); - + while ($i < $m) { - if($m % $i ==0){ - @divisors = (@divisors, $i); - } - $i++; + if ($m % $i == 0) { + @divisors = (@divisors, $i); + } + $i++; } return @divisors; } sub factorize { my $current = shift @_; - my @primes = @_; + my @primes = @_; my @result; foreach my $prime (@primes) { - while (0 == ($current % $prime)) { - push @result, $prime; - $current = $current / $prime; - # better: - # $current /= $prime; - } + while (0 == ($current % $prime)) { + push @result, $prime; + $current = $current / $prime; + + # better: + # $current /= $prime; + } } return @result;