Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Created January 5, 2020 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrussell/0b4d6321607d341b717183c61ab37e1f to your computer and use it in GitHub Desktop.
Save adamcrussell/0b4d6321607d341b717183c61ab37e1f to your computer and use it in GitHub Desktop.
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