Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perl Weekly Challenge 041
use strict;
use warnings;
##
# Write a script to display attractive numbers between 1 and 50.
##
sub prime_factor{
my $x = shift(@_);
my @factors;
for (my $y = 2; $y <= $x; $y++){
next if $x % $y;
$x /= $y;
push @factors, $y;
redo;
}
return @factors;
}
MAIN:{
for my $n (1 .. 50){
my @factors = prime_factor($n);
print "$n (" . join(", " , @factors) . ")\n" if(prime_factor(scalar @factors) == 1);
}
}
use strict;
use warnings;
##
# Write a script to display the first 20 Leonardo Numbers.
##
use constant NUMBERS => 20;
my $numbers = NUMBERS;
my @leonardo = (1, 1);
while($numbers){
if((NUMBERS - $numbers) < 2){
print "L(" . (NUMBERS - $numbers) . ") = " . $leonardo[-1] . "\n";
}
else{
my $leonardo = $leonardo[-1] + $leonardo[-2] + 1;
print "L(" . (NUMBERS - $numbers) . ") = $leonardo\n";
push @leonardo, $leonardo;
}
$numbers--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.