-
-
Save vnaskos/cedbd07016ba6fe3f0d20b67e8266412 to your computer and use it in GitHub Desktop.
Learn Perl - Project Euler P3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my @primes; | |
die "(script waits for exactly one argument)\n" unless(scalar @ARGV == 1); | |
CANDIDATE: for my $candidate ( 2 .. 10000 ) { | |
for my $divisor ( 2 .. sqrt $candidate ) { | |
next CANDIDATE if $candidate % $divisor == 0; | |
} | |
push @primes, $candidate; | |
} | |
my $num = $ARGV[0]; | |
my @primeFactors = (); | |
my $q = $num; | |
while((scalar grep{$_ == $q} @primes) == 0) { | |
ITERATION: foreach(@primes) { | |
if($q % $_ == 0) { | |
push @primeFactors, $_; | |
$q = $q / $_; | |
last ITERATION; | |
} | |
} | |
} | |
print "$num: @primeFactors $q\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment