diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-05 06:58:51 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-05 06:58:51 +0000 |
| commit | f760cbd9ef69aa1f075404aa5f6dc3e151a8bec0 (patch) | |
| tree | ecb08f1cf2f4a0d7c731a8633a0cf09bd2d80541 | |
| parent | 350f08ac8f823e5e37c387a2a02921137b8d3119 (diff) | |
| parent | 438d9bb8cb3e62ac744de5569944ead8ac098ba3 (diff) | |
| download | perlweeklychallenge-club-f760cbd9ef69aa1f075404aa5f6dc3e151a8bec0.tar.gz perlweeklychallenge-club-f760cbd9ef69aa1f075404aa5f6dc3e151a8bec0.tar.bz2 perlweeklychallenge-club-f760cbd9ef69aa1f075404aa5f6dc3e151a8bec0.zip | |
Merge pull request #3659 from wlmb/challenges
Challenges
| -rwxr-xr-x | challenge-102/wlmb/perl/ch-1b.pl | 3 | ||||
| -rwxr-xr-x | challenge-102/wlmb/perl/ch-1c.pl | 38 |
2 files changed, 40 insertions, 1 deletions
diff --git a/challenge-102/wlmb/perl/ch-1b.pl b/challenge-102/wlmb/perl/ch-1b.pl index bf4950d7cd..269db9954f 100755 --- a/challenge-102/wlmb/perl/ch-1b.pl +++ b/challenge-102/wlmb/perl/ch-1b.pl @@ -23,7 +23,8 @@ for my $N(@ARGV){ next A if $x>$max; my $y=join '', reverse split '', $x; next B unless $y==($a**2-$b**2)/2; - say "$x $y $a $b"; + my ($s, $d)=($x+$y, $x-$y); + say "N=$N\tx=$x\ty=$y\tx+y=$s=", sqrt($s),"**2\tx-y=$d=",sqrt($d),"**2"; } } } diff --git a/challenge-102/wlmb/perl/ch-1c.pl b/challenge-102/wlmb/perl/ch-1c.pl new file mode 100755 index 0000000000..9773a7fdee --- /dev/null +++ b/challenge-102/wlmb/perl/ch-1c.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +# Perl weekly challenge 102 +# Task 1: Rare numbers +# Faster by breaking the number at the middle. +use strict; +use warnings; +use v5.12; +use POSIX qw(floor); + +foreach my $N(@ARGV){ + my $min=10**($N-1); + my $N2=floor($N/2); + my $M=10**$N2; + my %seen; # disctint squares mod $M + foreach my $a(0..10**$N2){ + $seen{($a**2)%$M}=1; + } + my @squares=sort {$a<=>$b} keys %seen; + foreach my $a2(@squares){ + foreach my $b2(@squares){ + my $xr=($a2+$b2); + next unless $xr%2==0; + $xr=sprintf("%0${N2}d",($xr/2)%$M); + my $xl1=sprintf("%0${N2}d",(($a2-$b2)/2)%$M); + foreach my $mid($N%2==0?(''):(0..9)){ + my $x=join '', reverse(split '', $xl1), $mid, $xr; + next unless $x>=$min; + my $y=join '', reverse(split '', $x); + next unless $x>$y; + my $s=$x+$y; + my $d=$x-$y; + next unless floor(sqrt($s))**2==$s; + next unless floor(sqrt($d))**2==$d; + say "N=$N\tx=$x\ty=$y\tx+y=$s=", sqrt($s),"**2\tx-y=$d=",sqrt($d),"**2"; + } + } + } +} |
