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