aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-02-01 13:41:34 +0000
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-02-01 13:41:34 +0000
commitcfcef44f1f3b1d4708c0345b0e403f2abc18f4ea (patch)
treea27d759384a93b7241dd61ce96d3ae5de0289e45
parentebcd59a50056e1da3a3f28c98e867c2344548ac6 (diff)
downloadperlweeklychallenge-club-cfcef44f1f3b1d4708c0345b0e403f2abc18f4ea.tar.gz
perlweeklychallenge-club-cfcef44f1f3b1d4708c0345b0e403f2abc18f4ea.tar.bz2
perlweeklychallenge-club-cfcef44f1f3b1d4708c0345b0e403f2abc18f4ea.zip
My attempts at week 150
-rw-r--r--challenge-150/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-150/peter-campbell-smith/perl/ch-1.pl38
-rwxr-xr-xchallenge-150/peter-campbell-smith/perl/ch-2.pl48
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-150/peter-campbell-smith/blog.txt b/challenge-150/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..a109b80842
--- /dev/null
+++ b/challenge-150/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2022/02/fibo-nacci-fibonacci-naccifibonacci.html
diff --git a/challenge-150/peter-campbell-smith/perl/ch-1.pl b/challenge-150/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..69b7f52e8c
--- /dev/null
+++ b/challenge-150/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-01-31
+# PWC 150 task 1
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+
+# You are given two strings having same number of digits, $a and $b.
+# Write a script to generate Fibonacci Words by concatenation of the previous two
+# strings. Finally print 51st digit of the first term having at least 51 digits.
+
+my ($a, $b, $index, $j, $test, @fib, @tests);
+
+# sets of inputs to test ($a, $b, character index of interest)
+@tests = (['1234', '5678', 51], ['12345678', '98765432', 159], ['1', '2', 1000]);
+
+# loop over tests
+for $test (@tests) {
+
+ # get parameters
+ ($fib[0], $fib[1], $index) = @$test;
+
+ # create successive ternms of series until one is long enough
+ $j = 1;
+ while (1) {
+ $j ++;
+ $fib[$j] = $fib[$j - 2] . $fib[$j - 1];
+ last if length($fib[$j]) >= $index;
+ }
+
+ # format the answer
+ say qq[\nInput: $fib[0], $fib[1]];
+ say qq[Term $j is $fib[$j] (] . length($fib[$j]) . ' characters long)';
+ say qq[Character $index is ] . substr($fib[$j], $index - 1, 1);
+}
diff --git a/challenge-150/peter-campbell-smith/perl/ch-2.pl b/challenge-150/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..bdb718579d
--- /dev/null
+++ b/challenge-150/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-01-31
+# PWC 150 task 2
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+
+# Write a script to generate all square-free integers <= 500.
+
+# In mathematics, a square-free integer (or squarefree integer) is an integer
+# which is divisible by no perfect square other than 1. That is, its prime
+# factorization has exactly one factor for each prime that appears in it. For
+# example, 10 = 2 x 5 is square-free, but 18 = 2 x 3 x 3 is not, because 18 is
+# divisible by 9 = 3**2.
+
+my ($j, $mult, $no_good, $square, $top, @sfi, $results, $count);
+
+$top = 500;
+
+# let's start by guessing that all integers are square-free
+for $j (1 .. $top) {
+ $sfi[$j] = 1;
+}
+
+# now let's knock out anything that is a multiple of a square
+for $j (2 .. int(sqrt($top))) {
+ next unless $sfi[$j]; # no need to bother if $j is a known non-square-free
+ $square = $j ** 2;
+ for ($mult = 1;; $mult ++) {
+ $no_good = $mult * $square;
+ last if $no_good > $top;
+ $sfi[$no_good] = 0;
+ }
+}
+
+# output what's left neatly
+say qq[Square-free integers <= $top: ];
+$count = 1;
+for $j (1 .. $top) {
+ next unless $sfi[$j];
+ $results .= $j . ', ';
+ $results .= qq[\n] unless ($count++ % 10);
+}
+$results =~ s|[, \n]+$||;
+say $results;