# HG changeset patch # User Heiko Schlittermann (JUMPER) # Date 1319918356 -7200 # Node ID 6a11b95c49040ff2b1feb0c9ea2edfa17166a4a3 # Parent 1519d83aa83baa526c6e8d26fa411500f0f0b2a6 put everthing into functions now diff -r 1519d83aa83b -r 6a11b95c4904 prime --- a/prime Sat Oct 29 21:49:21 2011 +0200 +++ b/prime Sat Oct 29 21:59:16 2011 +0200 @@ -7,38 +7,20 @@ use warnings; use File::Basename; -#use diagnostics; - my $ME = basename $0; -if (not @ARGV) { - - # FIXME: be more verbose - die "$ME: Sorry, need the number\n"; -} +die "$ME: Sorry, need the number\n" + if not @ARGV; my $number = $ARGV[0]; #1. find all divisors of $number my @divisors = findDivisors($number); +print "Die Teiler von $number sind: @divisors\n"; #2. delete all non primes -my @primes = (); - -#for each element in array divisors -foreach my $div (@divisors) { #same element in div - - #find all divisors of div and save in subprimes - my @subprimes = findDivisors($div); - - #determine length of subprimes and save in noOfDivisors - my $noOfDivisors = scalar(@subprimes); - - if ($noOfDivisors == 0) { @primes = (@primes, $div); } -} - -#print"Die Teile von $number sind: ".join(", ", @divisors)."\n"; -#print"Die Primzahlteile von $number sind: ".join(", ", @primes)."\n"; +my @primes = findPrimes(@divisors); +print "Die Primzahlteile von $number sind: @primes\n"; # not sure about the name of the function! // .hs @primes = factorize($number, @primes); @@ -47,9 +29,7 @@ print join("*", @primes), "\n"; } else { - - #print STDERR "Sorry, $number is prime itself!\n"; - warn "Sorry, $number is prime itself\n!"; + warn "Sorry, $number is prime itself!\n"; } exit; @@ -71,6 +51,19 @@ return @divisors; } +sub findPrimes { + my @primes; + + foreach my $div (@_) { + + #find all divisors of div and save in subprimes + my @subprimes = findDivisors($div); + + if (!@subprimes) { push @primes, $div } + } + return @primes; +} + sub factorize { my $current = shift @_; my @primes = @_;