Task1
Solving this using Euclid's formula:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
use ntheory qw(gcd);
sub pythagoras_multiplied{
my ($n) = @_;
my $count = 0;
for(my $m = 2; $m * $m + 1 <= $n; $m++){
for(my $k = 1; $k < $m; $k++){
next if (($m - $k) % 2 == 0) || gcd($m,$k) != 1;
my $c = $m * $m + $k * $k;
last if $c > $n && $k == 1;
next if $c > $n;
$count += 2 * int($n / $c)
}
}
$count
}
is pythagoras_multiplied(20),12,'Example 1';
is pythagoras_multiplied(7),2,'Example 2';
is pythagoras_multiplied(1),0,'Example 3';
is pythagoras_multiplied(15),8,'Example 4';
is pythagoras_multiplied(30),22,'Example 5';
done_testing();
Task2
We find the sum of all ascii values in the string and check for primality:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);
use ntheory qw(is_prime);
use Test::More tests => 5;
sub prime_step{
my $sum = sum0 map{ord} split '',$_[0];
my $d = 0;
while(1){
return $d if is_prime($sum - $d) || is_prime($sum + $d);
$d++
}
}
is prime_step('hello'),9,'Example 1';
is prime_step('football'),2,'Example 2';
is prime_step('a'),0,'Example 3';
is prime_step('challenge'),2,'Example 4';
is prime_step('perl'),2,'Example 5';
done_testing();
No comments:
Post a Comment