From 9eb0f47862f3ff47e7b63d01181309113f5c652f Mon Sep 17 00:00:00 2001 From: boblied Date: Sun, 12 Feb 2023 09:48:37 -0600 Subject: Update README week 176 --- challenge-176/bob-lied/README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-176/bob-lied/README b/challenge-176/bob-lied/README index c231e3a589..a6a80e8eea 100644 --- a/challenge-176/bob-lied/README +++ b/challenge-176/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 138 by Bob Lied +Solutions to weekly challenge 176 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-138/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-176/ -- cgit From 8f8efeb2bd1142342d75988e3fd69faa1102810a Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 20 Feb 2023 13:05:27 -0600 Subject: Start task 1 --- challenge-176/bob-lied/perl/ch-1.pl | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-176/bob-lied/perl/ch-1.pl diff --git a/challenge-176/bob-lied/perl/ch-1.pl b/challenge-176/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..1aaaeb52db --- /dev/null +++ b/challenge-176/bob-lied/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Week 176 Task 1 Permuted Multiples +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# Write a script to find the smallest positive integer x +# such that x, 2x, 3x, 4x, 5x and 6x are permuted multiples of each other. +# For example, the integers 125874 and 251748 are permutated multiples +# of each other as 251784 = 2 x 125874 +# and also both have the same digits but in different order. +# Output 142857 +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub runTest +{ + use Test2::V0; + + is(0, 1, "FAIL"); + + done_testing; +} + -- cgit From 39222b30c78b2727fa5786ed3205fdf50ee758b1 Mon Sep 17 00:00:00 2001 From: boblied Date: Tue, 7 Mar 2023 10:33:16 -0600 Subject: Week 176 Task 1 --- challenge-176/bob-lied/perl/ch-1.pl | 48 +++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/challenge-176/bob-lied/perl/ch-1.pl b/challenge-176/bob-lied/perl/ch-1.pl index 1aaaeb52db..2c663c5d77 100644 --- a/challenge-176/bob-lied/perl/ch-1.pl +++ b/challenge-176/bob-lied/perl/ch-1.pl @@ -15,18 +15,58 @@ use v5.36; +use builtin qw/true false/; +no warnings "experimental::builtin"; + +use List::Util qw/all/; + use Getopt::Long; -my $Verbose = 0; -my $DoTest = 0; +my $DoTest = false; +my $DoAll = false; +my $Size = 6; -GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +GetOptions("test" => \$DoTest, "all" => \$DoAll, "size:i" => \$Size); exit(!runTest()) if $DoTest; +sub isPermutation($digits, $m) +{ + my @m = sort split("", $m); + return false if $#m != $#{$digits}; + return all { $digits->[$_] == $m[$_] } 0 .. $#m; +} + +say join(" ", smallestPermuted($Size, $DoAll)->@*); + +sub smallestPermuted($size = 6, $doAll = false) +{ + my @result; + MAG: for ( my $magnitude = 100 ; $magnitude <= 10**$size ; $magnitude *= 10 ) + { + my $max = int( $magnitude / 6 ); + my $base = int ( $max / 10 ); + for ( my $i = $base + 1 ; $i <= $base + $max ; $i++ ) + { + my @digits = sort split("", $i); + + if ( all { isPermutation(\@digits, $i * $_) } 2 .. 6 ) + { + push @result, $i; + last MAG unless $doAll; + } + } + } + return \@result; +} + sub runTest { use Test2::V0; - is(0, 1, "FAIL"); + is( smallestPermuted(2), [], "2 digits"); + is( smallestPermuted(3), [], "3 digits"); + is( smallestPermuted(4), [], "4 digits"); + is( smallestPermuted(5), [], "5 digits"); + is( smallestPermuted(6), [ 142857 ], "6 digits"); done_testing; } -- cgit From 470aa2b0e5065127df081d457a017d3c992fb8b6 Mon Sep 17 00:00:00 2001 From: boblied Date: Wed, 8 Mar 2023 14:07:02 -0600 Subject: Week 176 task 2 --- challenge-176/bob-lied/perl/ch-2.pl | 55 +++++++++++++++++++++++++++++ challenge-176/bob-lied/perl/rev.c | 61 ++++++++++++++++++++++++++++++++ challenge-176/bob-lied/perl/rev.pl | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 challenge-176/bob-lied/perl/ch-2.pl create mode 100644 challenge-176/bob-lied/perl/rev.c create mode 100644 challenge-176/bob-lied/perl/rev.pl diff --git a/challenge-176/bob-lied/perl/ch-2.pl b/challenge-176/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..d59aabaeb0 --- /dev/null +++ b/challenge-176/bob-lied/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Task 2 Reversible Numbers +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# Write a script to find out all Reversible Numbers below 100. +# A number is said to be a reversible if sum of the number and its +# reverse had only odd digits. +# For example, +# 36 is reversible number as 36 + 63 = 99 i.e. all digits are odd. +# 17 is not reversible as 17 + 71 = 88, none of the digits are odd. +#============================================================================= + +use v5.36; + +use builtin qw/true false/; +no warnings "experimental::builtin"; + +# To be an odd number, it has to be the sum of an even and an odd number. +# We're considering only two-digit numbers, so that means if the first digit +# is odd, the second must be even, and vice versa. Generate the possibilities. +my @candidate = ( glob("{1,3,5,7,9}{0,2,4,6,8}"), glob("{2,4,6,8}{1,3,5,7,9}") ); + +say join ",", sort { $a <=> $b } + grep { $_ < 100 && allOdd($_ + revNum($_)) } @candidate; + +# There are two fairly obvious ways of reversing the number. We can treat +# it as a string and reverse the digits, or we can do the math. The string +# operations are actually faster. +sub revNum($n) +{ + (my $r = reverse("$n")) =~ s/^0+//; + return +($r); +} + +# Checking that all digits are odd. Again, two possibilities: do the math +# one digit at a time, or treat as a string and test each digit. +sub allOdd($n) +{ + my $isOdd = true; # Doesn't handle 0, but 0 is not happening here. + while ( $n && $isOdd ) + { + $isOdd &&= ( $n % 10 ) % 2; + $n = int($n / 10); + } + return $isOdd; +} + +sub allOdd_str($n) +{ + use List::Util qw/all/; + return all { $_ % 2 } split("", $n); +} diff --git a/challenge-176/bob-lied/perl/rev.c b/challenge-176/bob-lied/perl/rev.c new file mode 100644 index 0000000000..0dff0ea4fb --- /dev/null +++ b/challenge-176/bob-lied/perl/rev.c @@ -0,0 +1,61 @@ +#include +#include +#include + +#include + +void reverse(char s[]) +{ + int i, j; + char c; + + for (i = 0, j = strlen(s)-1; i 236). Which is faster: doing +# the math, or treating it as a string? +#============================================================================= + +use v5.36; + +use Benchmark qw/cmpthese/; + +use Getopt::Long; +my $DoBenchmark = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "benchmark" => \$DoBenchmark); + +if ( $DoBenchmark ) +{ + cmpthese(-5, { + numeric => sub { + rev1($_) for 123400 .. 124500; + }, + string => sub { + rev2($_) for 123400 .. 124500; + }, + } + ); +} +exit(!runTest()) if $DoTest; + +sub rev1($n) +{ + use integer; + my $r = $n % 10; + while ( $n = int($n/10) ) + { + $r = ($r * 10) + ($n % 10); + } + return $r; +} + +sub rev2($n) +{ + (my $r = reverse("$n")) =~ s/^0+//; + return $r; +} + +sub runTest +{ + use Test2::V0; + + is( rev1( 2), 2, "Test 1"); + is( rev1( 20), 2, "Test 2"); + is( rev1(632), 236, "Test 3"); + is( rev1(495632), 236594, "Test 6"); + + is( rev2( 2), 2, "Test 1"); + is( rev2( 20), 2, "Test 2"); + is( rev2(632), 236, "Test 3"); + is( rev2(495632), 236594, "Test 6"); + + done_testing; +} + -- cgit From 7c25bd7c2f37595d67c2e37723a6aca650bc216f Mon Sep 17 00:00:00 2001 From: boblied Date: Wed, 8 Mar 2023 18:39:55 -0600 Subject: blog link --- challenge-176/bob-lied/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-176/bob-lied/blog.txt diff --git a/challenge-176/bob-lied/blog.txt b/challenge-176/bob-lied/blog.txt new file mode 100644 index 0000000000..234f398ef6 --- /dev/null +++ b/challenge-176/bob-lied/blog.txt @@ -0,0 +1 @@ +https://dev.to/boblied/pwc-176-reversing-a-number-39gg -- cgit