#!/usr/bin/perl use warnings; use strict; ######## my $doSqrt; if($ARGV[0] eq '--sqrt') {$doSqrt = shift @ARGV;} # added to toggle whether or not I do the sqrt after ever num sub prime_factors { my $num = shift; my @primes = (); my $max = sqrt($num); while ($num % 2 == 0) { push @primes, 2; $num /= 2; } foreach (my $i = 3; $i <= $max; $i += 2) { if ($num % $i == 0) { push @primes, $i; $num /= $i; $max = sqrt($num) if $doSqrt; # added by [pryrt] redo; # changed from [id://1194102], where it used to be: next; } } if ($num > 2) { push @primes, $num; } return @primes; } foreach my $n ( @ARGV ? @ARGV : (20, 48, 96, 7, 69, 33, 13195, 600851475143, 2147483647) ) { my @result = prime_factors($n); print "Number: $n , Prime_Factors: => @result", "\n"; }