diff -r 000000000000 -r 1f4d150b3e3b prime --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prime Sat Oct 29 21:40:31 2011 +0200 @@ -0,0 +1,91 @@ +#!/usr/bin/perl +# Natalie Schlittermann BB11 Set 1 +# Primzahlzerlegung +# Eine Zahl die in sämtlichen Primzahlen zerlegt wird + +use strict; +use warnings; +use File::Basename; +#use Smart::Comments; +#use diagnostics; + +my $ME = basename $0; + +#n: ist die Zahl aus der die sämtlichen Primzahlen gefiltert wird +#i: Primzahl: diese wird durch n geteilt + +my $number = $ARGV[0]; + +if (not @ARGV) { + # FIXME: be more verbose + die "$ME: Sorry, need the number\n"; +} + +#1. find all divisors of $number +my @divisors = findDivisors($number); + +#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 @xxx = xxx($number, @primes); + +if (@xxx) { + print join("*", @xxx), "\n"; +} +else { + #print STDERR "Sorry, $number is prime itself!\n"; + warn "Sorry, $number is prime itself\n!"; +} + +exit; + +#### SUBROUTINES ##### + +sub findDivisors { + my $i = 2; + my $m = $_[0]; + + my @divisors = (); + + while ($i < $m) { + if($m % $i ==0){ + @divisors = (@divisors, $i); + } + $i++; + } + return @divisors; +} + +sub xxx { + my $current = shift @_; + my @primes = @_; + + my @result; + foreach my $prime (@primes) { + while (0 == ($current % $prime)) { + push @result, $prime; + $current = $current / $prime; + # better: + # $current /= $prime; + } + } + + return @result; +}