prime
changeset 4 6a11b95c4904
parent 3 1519d83aa83b
child 5 51950b0c040d
equal deleted inserted replaced
3:1519d83aa83b 4:6a11b95c4904
     5 
     5 
     6 use strict;
     6 use strict;
     7 use warnings;
     7 use warnings;
     8 use File::Basename;
     8 use File::Basename;
     9 
     9 
    10 #use diagnostics;
       
    11 
       
    12 my $ME = basename $0;
    10 my $ME = basename $0;
    13 
    11 
    14 if (not @ARGV) {
    12 die "$ME: Sorry, need the number\n"
    15 
    13   if not @ARGV;
    16     # FIXME: be more verbose
       
    17     die "$ME: Sorry, need the number\n";
       
    18 }
       
    19 
    14 
    20 my $number = $ARGV[0];
    15 my $number = $ARGV[0];
    21 
    16 
    22 #1. find all divisors of $number
    17 #1. find all divisors of $number
    23 my @divisors = findDivisors($number);
    18 my @divisors = findDivisors($number);
       
    19 print "Die Teiler von $number sind: @divisors\n";
    24 
    20 
    25 #2. delete all non primes
    21 #2. delete all non primes
    26 my @primes = ();
    22 my @primes = findPrimes(@divisors);
    27 
    23 print "Die Primzahlteile von $number sind: @primes\n";
    28 #for each element in array divisors
       
    29 foreach my $div (@divisors) {    #same element in div
       
    30 
       
    31     #find all divisors of div and save in subprimes
       
    32     my @subprimes = findDivisors($div);
       
    33 
       
    34     #determine length of subprimes and save in noOfDivisors
       
    35     my $noOfDivisors = scalar(@subprimes);
       
    36 
       
    37     if ($noOfDivisors == 0) { @primes = (@primes, $div); }
       
    38 }
       
    39 
       
    40 #print"Die Teile von $number sind: ".join(", ", @divisors)."\n";
       
    41 #print"Die Primzahlteile von $number sind: ".join(", ", @primes)."\n";
       
    42 
    24 
    43 # not sure about the name of the function! // .hs
    25 # not sure about the name of the function! // .hs
    44 @primes = factorize($number, @primes);
    26 @primes = factorize($number, @primes);
    45 
    27 
    46 if (@primes) {
    28 if (@primes) {
    47     print join("*", @primes), "\n";
    29     print join("*", @primes), "\n";
    48 }
    30 }
    49 else {
    31 else {
    50 
    32     warn "Sorry, $number is prime itself!\n";
    51     #print STDERR "Sorry, $number is prime itself!\n";
       
    52     warn "Sorry, $number is prime itself\n!";
       
    53 }
    33 }
    54 
    34 
    55 exit;
    35 exit;
    56 
    36 
    57 #### SUBROUTINES #####
    37 #### SUBROUTINES #####
    69         $i++;
    49         $i++;
    70     }
    50     }
    71     return @divisors;
    51     return @divisors;
    72 }
    52 }
    73 
    53 
       
    54 sub findPrimes {
       
    55     my @primes;
       
    56 
       
    57     foreach my $div (@_) {
       
    58 
       
    59         #find all divisors of div and save in subprimes
       
    60         my @subprimes = findDivisors($div);
       
    61 
       
    62         if (!@subprimes) { push @primes, $div }
       
    63     }
       
    64     return @primes;
       
    65 }
       
    66 
    74 sub factorize {
    67 sub factorize {
    75     my $current = shift @_;
    68     my $current = shift @_;
    76     my @primes  = @_;
    69     my @primes  = @_;
    77 
    70 
    78     my @result;
    71     my @result;