useful output if input is a prime itself
authorHeiko Schlittermann (JUMPER) <hs@schlittermann.de>
Sat, 29 Oct 2011 22:11:42 +0200
changeset 6 ad2ed8ef3e21
parent 5 51950b0c040d
child 7 6a025d656935
useful output if input is a prime itself
prime
--- a/prime	Sat Oct 29 22:07:12 2011 +0200
+++ b/prime	Sat Oct 29 22:11:42 2011 +0200
@@ -19,21 +19,18 @@
 
 #1. find all divisors of $number
 my @divisors = findDivisors($number);
-print "Die Teiler von $number sind: @divisors\n";
+
+#print "Die Teiler von $number sind: @divisors\n";
 
 #2. delete all non primes
 my @primes = findPrimes(@divisors);
-print "Die Primzahlteile von $number sind: @primes\n";
+
+#print "Die Primzahlteile von $number sind: @primes\n";
 
 # not sure about the name of the function! // .hs
 @primes = factorize($number, @primes);
 
-if (@primes) {
-    print join("*", @primes), "\n";
-}
-else {
-    warn "Sorry, $number is prime itself!\n";
-}
+print join("*", @primes), "\n";
 
 exit;
 
@@ -43,13 +40,11 @@
     my $i = 2;
     my $m = $_[0];
 
-    my @divisors = ();
-
-    while ($i < $m) {
+    my @divisors;
+    for (my $i = 2 ; $i <= $m ; $i++) {
         if ($m % $i == 0) {
-            @divisors = (@divisors, $i);
+            push @divisors, $i;
         }
-        $i++;
     }
     return @divisors;
 }
@@ -61,8 +56,7 @@
 
         #find all divisors of div and save in subprimes
         my @subprimes = findDivisors($div);
-
-        if (!@subprimes) { push @primes, $div }
+        if (@subprimes == 1) { push @primes, $div }
     }
     return @primes;
 }
@@ -75,10 +69,7 @@
     foreach my $prime (@primes) {
         while (0 == ($current % $prime)) {
             push @result, $prime;
-            $current = $current / $prime;
-
-            # better:
-            # $current /= $prime;
+            $current /= $prime;
         }
     }