From d5f8714d7a5a24f911ebfdb43b657bfa25c95cb2 Mon Sep 17 00:00:00 2001 From: Niels van Dijke <65567640+PerlBoy1967@users.noreply.github.com> Date: Wed, 17 Nov 2021 01:01:06 +0100 Subject: Delete ch-2.pl --- challenge-139/perlboy1967/perl/ch-2.pl | 76 ---------------------------------- 1 file changed, 76 deletions(-) delete mode 100755 challenge-139/perlboy1967/perl/ch-2.pl diff --git a/challenge-139/perlboy1967/perl/ch-2.pl b/challenge-139/perlboy1967/perl/ch-2.pl deleted file mode 100755 index d065fe75e3..0000000000 --- a/challenge-139/perlboy1967/perl/ch-2.pl +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 139 - - https://perlweeklychallenge.org/blog/perl-weekly-challenge-139/#TASK2 - -Author: Niels 'PerlBoy' van Dijke - -Write a script to generate first 5 Long Primes. - - || A prime number (p) is called Long Prime if (1/p) has an infinite decimal - || expansion repeating every (p-1) digits. - -=cut - -use v5.16; -use strict; -use warnings; - -use Math::Prime::XS qw(is_prime); - - -# Re-usage of code week 106, task #2 -sub repeatedDivDigits { - my ($d) = @_; - my ($n,$result) = (1,''); - - my %rSeen; - my $r = $n; - - # Let's do a 'long div' and track if the - # remainder becomes zero or if we've seen - # a numerator before - - do { - $n = $r; - - while ($n < $d) { - $n .= '0'; - $result .= '0' if ($n < $d); - } - - push(@{$rSeen{$n}}, length($result)); - - $result .= int($n/$d) - if (scalar(@{$rSeen{$n}}) < 2); - - $r = $n % $d; - - } while ($r != 0 and scalar(@{$rSeen{$n}} < 2)); - - # Non repetitative division? - return '' if ($r == 0); - - if ($result =~ m#([0]+)$#) { - $rSeen{$n}[0] -= length($1); - $rSeen{$n}[1] -= length($1); - } - - return substr($result, - $rSeen{$n}[0], - $rSeen{$n}[1] - $rSeen{$n}[0]); -} - -my @primes; - -my $i = 1; -while (scalar(@primes) < 5) { - push(@primes,$i) - if (is_prime($i) and length(repeatedDivDigits($i)) == $i-1); - $i++ -} - -printf "%s\n",join("\n",@primes); - -- cgit From 87aa8152be4cf2d9b0c65caa07a9070bd5ceceea Mon Sep 17 00:00:00 2001 From: Niels van Dijke <65567640+PerlBoy1967@users.noreply.github.com> Date: Wed, 17 Nov 2021 01:02:33 +0100 Subject: Task 2 --- challenge-139/perlboy1967/perl/ch-2.pl | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 challenge-139/perlboy1967/perl/ch-2.pl diff --git a/challenge-139/perlboy1967/perl/ch-2.pl b/challenge-139/perlboy1967/perl/ch-2.pl new file mode 100644 index 0000000000..9bfa980f52 --- /dev/null +++ b/challenge-139/perlboy1967/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 139 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-139/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +Write a script to generate first 5 Long Primes. + + || A prime number (p) is called Long Prime if (1/p) has an infinite decimal + || expansion repeating every (p-1) digits. + +=cut + +use v5.16; +use strict; +use warnings; + +use Math::Prime::XS qw(is_prime); + + +# Re-usage of code week 106, task #2 +sub repeatedDivDigits { + my ($d) = @_; + my ($n,$result) = (1,''); + + my %rSeen; + my $r = $n; + + # Let's do a 'long div' and track if the + # remainder becomes zero or if we've seen + # a numerator before + + do { + $n = $r; + + while ($n < $d) { + $n .= '0'; + $result .= '0' if ($n < $d); + } + + push(@{$rSeen{$n}}, length($result)); + + $result .= int($n/$d) + if (scalar(@{$rSeen{$n}}) < 2); + + $r = $n % $d; + + } while ($r != 0 and scalar(@{$rSeen{$n}} < 2)); + + # Non repetitative division? + return '' if ($r == 0); + + if ($result =~ m#([0]+)$#) { + $rSeen{$n}[0] -= length($1); + $rSeen{$n}[1] -= length($1); + } + + return substr($result, + $rSeen{$n}[0], + $rSeen{$n}[1] - $rSeen{$n}[0]); +} + +my @primes; + +my $i = 1; +while (scalar(@primes) < 5) { + push(@primes,$i) + if (is_prime($i) and length(repeatedDivDigits($i)) == $i-1); + $i++ +} + +printf "%s\n",join("\n",@primes); -- cgit From 4db9aacffd4ef48b60a7242f73b3f5da79133a7e Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Sun, 28 Nov 2021 22:30:22 +0000 Subject: Task 1 & 2 --- challenge-140/perlboy1967/perl/ch-1.pl | 88 ++++++++++++++++++++++++++++++++++ challenge-140/perlboy1967/perl/ch-2.pl | 25 ++++++++++ 2 files changed, 113 insertions(+) create mode 100755 challenge-140/perlboy1967/perl/ch-1.pl create mode 100755 challenge-140/perlboy1967/perl/ch-2.pl diff --git a/challenge-140/perlboy1967/perl/ch-1.pl b/challenge-140/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..b3e8358af1 --- /dev/null +++ b/challenge-140/perlboy1967/perl/ch-1.pl @@ -0,0 +1,88 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 139 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-139/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › Add Binary +Submitted by: Mohammad S Anwar + +You are given two decimal-coded binary numbers, $a and $b. + +Write a script to simulate the addition of the given binary numbers. + + || The script should simulate something like $a + $b. (operator overloading) + +=cut + +use v5.16; +use strict; +use warnings; + +my $b1 = new myBin(0b001); +my $b2 = new myBin(0b101); +my $b3 = new myBin(0b011); + +printf "b1: %s, b2: %s, b3: %s\n", $b1, $b2, $b3; + +my $A = new myBin($b3); +printf "A: %s\n", $A; + +my $B = new myBin($b1 + 7); +printf "B: %s (%s + 7)\n", $B, $b1; +$B += $b2; +printf "B: %s (%s + 7 + %s)\n", $B, $b1, $b2; +$B += $b3; +printf "B: %s (%s + %s + %s)\n", $B, $b1, $b2, $b3; +$B = $b1 + $b3; +printf "B: %s (%s + %s)\n", $B, $b1, $b3; +$B = 9 + $b1; +printf "B: %s (%s + %s)\n", $B, 9, $b1; + + +package myBin; + +use List::MoreUtils qw(pairwise); + +use overload + '""' => sub { '0b'.join '',reverse @{$_[0]} }, + '+' => \&_plus; + +sub new { + my ($self,$arg) = @_; + + # Accept a myBin object to initialize + # or an integer number + my @data = (); + if (defined $arg) { + if (ref $arg) { + @data = @$arg; + } else { + @data = reverse split //,sprintf '%b', $arg; + } + } + + bless \@data,$self; +} + +sub _plus { + my ($self, $other) = @_; + + $other = new myBin($other) if (!ref $other); + + my ($carry,$res) = (0); + my @result = pairwise { + no warnings 'once'; + $res = ($a//0) + ($b//0) + $carry; + $carry = ($res >> 1); + $res &= 1; + } @$self, @$other; + push(@result,1) if $carry; + + bless \@result; +} + +1; diff --git a/challenge-140/perlboy1967/perl/ch-2.pl b/challenge-140/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..8ce285cc84 --- /dev/null +++ b/challenge-140/perlboy1967/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 140 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Multiplication Table +Submitted by: Mohammad S Anwar + +You are given 3 positive integers, $i, $j and $k. + +Write a script to print the $kth element in the sorted multiplication table of $i and $j. + +=cut + +use v5.16; +use strict; +use warnings; + +use List::MoreUtils qw(arrayify); +my($i,$j,$k)=@ARGV; +printf"%s\n",(sort{$a<=>$b}arrayify map{my$n=$_;$_=[map{$n*$_}1..$j]}1..$i)[$k-1]; -- cgit