aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-06-21 18:52:33 +0800
committerGitHub <noreply@github.com>2020-06-21 18:52:33 +0800
commit9e9a14c455445f33b11c72d06b3f674b29b49576 (patch)
treefa66a10bc5548a0e7e26162b3ffc65ebfeb7a188
parent72c85bb371e7d3e0a31ffa6fb821d70b06859fb0 (diff)
downloadperlweeklychallenge-club-9e9a14c455445f33b11c72d06b3f674b29b49576.tar.gz
perlweeklychallenge-club-9e9a14c455445f33b11c72d06b3f674b29b49576.tar.bz2
perlweeklychallenge-club-9e9a14c455445f33b11c72d06b3f674b29b49576.zip
Add files via upload
-rw-r--r--ch-1.pl35
1 files changed, 35 insertions, 0 deletions
diff --git a/ch-1.pl b/ch-1.pl
new file mode 100644
index 0000000000..777de7f037
--- /dev/null
+++ b/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+use strict;
+use List::Util qw/sum/;
+
+my $N;
+my $S;
+
+if ($ARGV[0] and $ARGV[1]) {
+ $N = $ARGV[0];
+ $S = $ARGV[1];
+}
+else { #example
+ $N = 2;
+ $S = 4;
+}
+
+
+sub digitsum {
+ my $candidate = $_[0];
+ my @digits = split //, $candidate;
+ return sum @digits;
+}
+
+sub is {
+ my $candidate = $_[0];
+ return 1 if $S == digitsum $candidate;
+ return 0; #return false
+}
+
+my $start = 10**($N-1);
+my $end = 10**$N - 1;
+
+for ($start..$end) {
+ print $_,"\n" if is($_);
+}