aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-05-10 12:17:25 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-05-12 20:53:51 +0200
commit028a12d2d851f05a95b879392c8dae6eef7655f9 (patch)
treef8ea92fecbbfd9afb2b70ca0e415c17fef31fdda
parent2c28471a3f7edab1f863f7ec22404b8f7ac2eb67 (diff)
downloadperlweeklychallenge-club-028a12d2d851f05a95b879392c8dae6eef7655f9.tar.gz
perlweeklychallenge-club-028a12d2d851f05a95b879392c8dae6eef7655f9.tar.bz2
perlweeklychallenge-club-028a12d2d851f05a95b879392c8dae6eef7655f9.zip
Solution to task 2
-rwxr-xr-xchallenge-112/jo-37/perl/ch-2.pl36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-112/jo-37/perl/ch-2.pl b/challenge-112/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..9eb1ffb432
--- /dev/null
+++ b/challenge-112/jo-37/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use warnings;
+use Math::Prime::Util 'lucasu';
+
+die <<EOS unless @ARGV;
+usage: $0 N
+
+N
+ Count the different ways to climb a stair of N steps taking either
+ one ore two steps at a time.
+
+EOS
+
+# Are Abigail and Abigail the same person? See perlsecret.
+<<m=~m>>
+Let S(n) be the number of different ways to climb a stair of n steps
+taking either one ore two steps at a time.
+
+Let n > 2. The last move is either one step with S(n-1) ways to reach
+the second last step or two steps with S(n-2) ways to reach the step
+before it.
+
+Thus S(n) = S(n-1) + S(n-2). Furthermore, the trivial cases S(1) = 1
+and S(2) = 2 lead to S(n) = Fₙ₊₁, the (n+1)th Fibonacci number.
+
+The Fibonacci sequence Fₙ is the particular Lucas sequence Uₙ(1, -1) as
+provided by Math::Prime::Util.
+
+See e.g. http://oeis.org/wiki/Lucas_sequences.
+
+m
+;
+
+say lucasu 1, -1, 1 + shift;