Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ function run_python {
echo ""
}

function run_perl {
echo "*------------ Perl -------------*"
perl -l -e 'print $^V'

sleep 5 # cpu cool down
perl ./src/prime.pl
echo ""
}

function run_php {
echo "*------------ Php -------------*"
php --version
Expand Down Expand Up @@ -243,6 +252,7 @@ declare -A langs=(
["ruby"]="run_ruby"
["chap"]="run_chap"
["lua"]="run_lua"
["perl"]="run_perl"
)

# Define a function to display the help message
Expand Down
33 changes: 33 additions & 0 deletions src/prime.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/perl -l

use Time::HiRes;


sub is_prime {
$n = $_[0];

if( $n <= 1 ) {
return 0
}
$end = int(sqrt($n));

for( $a = 2; $a < $end + 1; $a = $a + 1 ) {
if ( $n % $a == 0 ) {
return 0
}
}
return 1
}

$start_time = Time::HiRes::gettimeofday();

$c = 0;

for( $i = 0; $i < 9000000; $i = $i + 1 ) {
if ( is_prime( $i ) ) {
$c = $c + 1;
}
}

print $c;
print Time::HiRes::gettimeofday() - $start_time;