blob: 9d6a3af8dce4c489d32ec1a607d63075438dc915 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#! /usr/bin/env perl
use strict;
use feature 'say';
use Algorithm::Combinatorics 'combinations';
use List::Util 'sum';
use Getopt::Long;
my $verbose = 0;
GetOptions("verbose" => \$verbose);
my $N = shift(@ARGV) // die "Please specify an integer > 0";
die "Please specify an integer > 0" unless $N =~ /^[1-9]\d*$/;
my $D = shift(@ARGV) // die "Please specify a digit";
die "Please specify a digit" unless $D =~ /^\d$/;
my @candidates = grep { /$D/ } (1 .. $N);
say ": Candidates " . join(', ', @candidates) if $verbose;
for my $size (1 .. @candidates)
{
for my $comb (combinations(\@candidates, $size))
{
say ": Considering " . join(' + ', @$comb) if $verbose;
if (sum(@$comb) == $N)
{
say 1;
exit;
}
}
}
say 0;
|