diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-07-04 21:10:19 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-07-04 21:10:19 +0100 |
| commit | 251b2bd8b0ff8d3b1262173c33a4e999cf37846a (patch) | |
| tree | e6d58a6a75f6a895cb07762b0183860f48096b9d /challenge-168 | |
| parent | 9a0e9aa937cd71b048889a57f68896e5187466ac (diff) | |
| download | perlweeklychallenge-club-251b2bd8b0ff8d3b1262173c33a4e999cf37846a.tar.gz perlweeklychallenge-club-251b2bd8b0ff8d3b1262173c33a4e999cf37846a.tar.bz2 perlweeklychallenge-club-251b2bd8b0ff8d3b1262173c33a4e999cf37846a.zip | |
new stuff
Diffstat (limited to 'challenge-168')
| -rw-r--r-- | challenge-168/james-smith/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-168/james-smith/perl/ch-2.pl | 9 |
2 files changed, 30 insertions, 1 deletions
diff --git a/challenge-168/james-smith/perl/ch-1.pl b/challenge-168/james-smith/perl/ch-1.pl index 2f25fc5369..66112af0ed 100644 --- a/challenge-168/james-smith/perl/ch-1.pl +++ b/challenge-168/james-smith/perl/ch-1.pl @@ -5,6 +5,7 @@ use warnings; use feature qw(say); use Math::Prime::Util qw(is_prime); use Math::BigInt; +$|=1; ## This is straight forward. ## In each interation of the loop, we remove the lowest number of the triple, @@ -23,6 +24,25 @@ my ($r,$s,$t)=(3,0,2); say "\n\nWith big ints (first 25)\n\n"; ($r,$s,$t)=(3,0,Math::BigInt->new(2)); -($r,$s,$t)=($s,$t,$r+$s), is_prime($t) && $t!=$s ? (say $t) : (redo) for 1..25; +($r,$s,$t)=($s,$t,$r+$s), is_prime($t) && $t!=$s ? (fmt($_,$t)) : (redo) for 1..40; say "\n"; + +sub fmt { + my $idx = shift; + $_ = reverse $_[0]; s{(\d{3})}{$1,}g; $_ = reverse $_; s{^,}{}; + my @t = split /,/; + printf " %3d : ",$idx; + if( @t == 1) { + say sprintf "%3d\n", $t[0]; + return; + } + my $pref = sprintf "%3d,",shift @t; + + while(@t) { + print $pref, join ',',splice @t,0,18; + say @t ? ',' : ''; + $pref = ' '; + } + say ''; +} diff --git a/challenge-168/james-smith/perl/ch-2.pl b/challenge-168/james-smith/perl/ch-2.pl index a81a68d340..e41f8436bd 100644 --- a/challenge-168/james-smith/perl/ch-2.pl +++ b/challenge-168/james-smith/perl/ch-2.pl @@ -66,8 +66,17 @@ is( home_prime($_), $TESTS[$_] ) for 2..48,50..76,78..100; done_testing(); +say sprintf '%4d: %64s',$_, $_==49 || $_ == 77 ? "***": th(home_prime($_)) for 2..100; + sub home_prime { return if (my$t=shift)<2; ## Skip in t is 0 or 1 which would loop infinitely... is_prime($t)?(return$t):($t=join'',factor$t)while 1; ## If prime we return o/w we compute sum of factors } +sub th { + my $t = reverse $_[0]; + $t =~ s{(\d{3})}{$1,}g; + $t = reverse $t; + $t =~ s{^,}{}; + $t; +} |
