Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Last active October 20, 2019 22:52
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/a499b866ccc39b489626977e5765ca14 to your computer and use it in GitHub Desktop.
Save adamcrussell/a499b866ccc39b489626977e5765ca14 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 030
use strict;
use warnings;
##
# Write a script to list dates for Sunday Christmas between 2019 and 2100.
##
sub day_of_week{
my($year, $month, $day) = @_;
my @month_value = (0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4);
$year-- if($month == 1 || $month == 2);
return ($year + int($year/4) - int($year/100) + int($year/400) + $month_value[$month-1] + $day) % 7
}
MAIN:{
for my $year (2019 .. 2100){
my $dow = day_of_week($year, 12, 25);
print "$year\n" if $dow == 0;
}
}
use strict;
use warnings;
##
# Write a script to print all possible series
# of 3 positive numbers, where in each series
# at least one of the number is even and sum
# of the three numbers is always 12.
##
use boolean;
use Math::Combinatorics;
sub has_even{
my @numbers = @_;
my @evens = grep { $_ % 2 == 0 } @numbers;
if(@evens){
return true;
}
return false;
}
sub sums_12{
my @numbers = @_;
my $sum = unpack("%32I*", pack("I*", @numbers));
if($sum == 12){
return true;
}
return false;
}
MAIN:{
my $combinations = new Math::Combinatorics(count => 3,
data => [-10 .. 10]
);
my @combination = $combinations->next_combination();
do{
if(has_even(@combination) and sums_12(@combination)){
print "@combination\n";
}
@combination = $combinations->next_combination();
} while(@combination);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment