From 2f661f978631af53d9cb473cdef818b8833ca0e9 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Tue, 7 Jun 2022 20:56:19 +0800 Subject: Week 168 First commit --- challenge-168/cheok-yin-fung/perl/ch-1.pl | 33 +++++++++++++++++ challenge-168/cheok-yin-fung/perl/ch-2.pl | 59 +++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 challenge-168/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-168/cheok-yin-fung/perl/ch-2.pl (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-1.pl b/challenge-168/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..61ba32d69e --- /dev/null +++ b/challenge-168/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +# The Weekly Challenge 168 +# Task 1 Perrin Prime +use v5.24.0; +use warnings; +use List::Util qw/reduce none/; +use Math::BigInt::GMP; # [remark] +use Math::BigInt::Pari; # [remark] +use Math::Prime::Util::GMP qw/is_prime next_prime/; +use bigint try => 'GMP,Pari'; # [remark] +# remark: follow suggestions on POD of Math::Prime::Util + +my @perrin_primes = (2,3); +my ($ppnm3,$ppnm2,$ppnm1, $ppn) = (3,0,2,3); + +while (scalar @perrin_primes < 13) { + if (is_prime($ppn) == 2) { + push @perrin_primes, $ppn if none {$_ == $ppn} @perrin_primes; + say $ppn; + } + $ppnm3 = $ppnm2; + $ppnm2 = $ppnm1; + $ppnm1 = $ppn; + $ppn = $ppnm2 + $ppnm3; +} + +say join ", ", @perrin_primes; + +# time: +# real 0m0.078s +# user 0m0.056s +# sys 0m0.008s + diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..0518d1dbcd --- /dev/null +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# The Weekly Challenge 168 +# Task 2 Home Prime +# Usage: ch-2.pl [integer > 1] +use v5.24.0; +use warnings; +use List::Util qw/reduce/; +use Math::BigInt::GMP; # [remark] +use Math::BigInt::Pari; # [remark] +use Math::Prime::Util::GMP qw/is_prime next_prime/; +use bigint try => 'GMP,Pari'; # [remark] +# remark: follow suggestions on POD of Math::Prime::Util + + +say hp($ARGV[0]) if defined($ARGV[0]); + + + +sub hp { + my_hp($_[0],0); +} + + + +sub my_hp { + my $recur_depth = $_[1]; + + die "Walk so far but still cannot get result. :(\n" if $recur_depth > 10; + my $num = $_[0]; + if (is_prime($num) == 2) { + return $num; + } + + my @factors = (); + my $p = 2; + do { + if (!($num % $p)) { + push @factors, $p; + $num /= $p; + } + else { + $p = next_prime($p); + } + } while ($num != 1); + my $nxt = (reduce { $a . $b } @factors); + say " $nxt"; + return my_hp($nxt, ++$recur_depth); +} + +use Test::More tests => 5; +# test data from OEIS +ok hp(10) == 773; +ok hp(24) == 331_319; +ok hp(32) == 241_271; +ok hp(45) == 3_411_949; + +ok hp(44) == 22815088913; # this test spends approx 0.5 min (on CY's laptop). + +# ok hp(40) == 3314192745739; # this test spends approx. 2 min. -- cgit From 3cc61075c2564e35c4826d6431312e2b0dcf2179 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Tue, 7 Jun 2022 21:17:33 +0800 Subject: to be improved within this week --- challenge-168/cheok-yin-fung/perl/ch-2.pl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl index 0518d1dbcd..3289846cfe 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -1,3 +1,5 @@ +# CAN BE IMPROVED + #!/usr/bin/perl # The Weekly Challenge 168 # Task 2 Home Prime @@ -39,7 +41,7 @@ sub my_hp { $num /= $p; } else { - $p = next_prime($p); + $p = next_prime($p); # CAN BE IMPROVED } } while ($num != 1); my $nxt = (reduce { $a . $b } @factors); -- cgit From bf717b19d8279f0a9b908d1f465a821d43f3eb55 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sat, 11 Jun 2022 20:41:39 +0800 Subject: improved raku ch-2 --- challenge-168/steve-g-lynn/raku/ch-2.p6 | 115 ++++++-------------------------- 1 file changed, 20 insertions(+), 95 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/raku/ch-2.p6 b/challenge-168/steve-g-lynn/raku/ch-2.p6 index bfe46ec923..f3a1259691 100755 --- a/challenge-168/steve-g-lynn/raku/ch-2.p6 +++ b/challenge-168/steve-g-lynn/raku/ch-2.p6 @@ -1,126 +1,51 @@ #!/usr/bin/raku #time (bash command): -#real 0m0.490s -#user 0m0.729s -#sys 0m0.056s - - -# acknowledgement: - -# I improved the previous grossly inefficient version -# (> 1 min script run time) after looking -# at the raku and python submissions from Roger Bell-West +#real 0m0.511s +#user 0m0.622s +#sys 0m0.046s say homeprime(10); #773 say homeprime(16); #31636373 +say homeprime(20); +#3318308475676071413 #-- sub for home prime -sub homeprime(Int $n) returns Int { +sub homeprime(Int $n) returns Int { $n.Int.is-prime && return $n; - my $ncopy=$n; - while (1) { - my $last=$ncopy; - $ncopy=factor($ncopy); - ($ncopy==$last) && last; - } - return $ncopy; + return homeprime(factor($n)); } #--sub for factorizing -multi sub factor (1) returns Int {1} - -multi sub factor (Int $n where $n > 1) returns Int { +sub factor (Int $n where $n > 1) returns Int { #-- returns the concatenated prime factors as an int $n.Int.is-prime && (return $n.Int ); - my $sqrt_n = sqrt($n).Int+1; - my @primes=find_primes($sqrt_n); + my Int $sqrt_n = sqrt($n).Int; + my @factors=(); - my $retstring=""; - my $ncopy=$n; + my Str $retstring=""; + my Int $ncopy=$n.Int; - for @primes -> $prime { - while ( ($ncopy % $prime)==0) { - $ncopy /= $prime; - push @factors, $prime; + for (2 .. $sqrt_n) -> $prime { + #-- no need to generate primes first.. the routine will + #-- automatically only choose prime factors + while ($ncopy %% $prime) { + $ncopy div= $prime; #-- integer division + @factors.append($prime); } + $ncopy.is-prime && last; } - ($ncopy > 1) && push @factors, $ncopy; + ($ncopy > 1) && @factors.append($ncopy); #-- any factor bigger than sqrt(n) is a prime factor $retstring = @factors.list.join; return $retstring.Int; } -multi sub find_primes(Int $n where $n <= 100) { - my @primes= - (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); - my @retval=(); - for @primes -> $prime { - ($prime < $n) && (push @retval, $prime); - } - return (@retval.sort); -} - -multi sub find_primes (Int $n where $n > 100) { - my @primes=[7,11,13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]; #-- candidate primes - my %primes{Int}=(); #-- we will return keys of this - my $sqrt_n = sqrt($n); #-- ceiling for iterating thru' sieve - - #-- initialize primes hash - #-- store candidates of the form 6k-1 to 6k+1 - #-- eliminate multiples of known primes - loop (my $i=102; $i <= $n+1; $i += 6) { - my @temp=($i-1, $i+1); - TEMP: for @temp -> $temp { - ($temp % 3) || next; - ($temp % 5) || next; - for @primes -> $prime { - ($prime > $sqrt_n) && last; - ($temp % $prime) || next TEMP; - } - ($temp <= $n) && (%primes{$temp}=True); - } - } - - #-- for prime candidates k greater than @primes[*-1] - #-- use odd numbers not divisible by 3 (last value + 4 and +6 if - # we start counting from 97) - #-- loop through factors kk+jk < n and delete - #-- any %hash entries with key matching these factors. - - my $last_prime = @primes[*-1]; - if ($last_prime < $sqrt_n) { - @primes=[$last_prime+4, $last_prime+2]; - while (1) { - my $prime = shift @primes; - (@primes.elems==0) && - (@primes.append($prime+4, $prime+2)); - #-- avoid multiples of 3 - - last if $prime > $sqrt_n; - - #-- only loop if the candidate is in the primes hash - if ( %primes{$prime} ) { - loop (my $i=$prime*$prime; - $i <= $n; - $i += $prime) { - %primes{$i}:delete; - } - } - } - } - my @retval=(2,3,5,7,11,13,17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); - return (@retval.append(%primes.keys.sort)); - - return 1; -} - - -- cgit From 3d271507b2ad90a357fb128df8fcf47b66f91ffb Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sat, 11 Jun 2022 21:03:20 +0800 Subject: improved perl ch-2.pl --- challenge-168/steve-g-lynn/perl/ch-2.pl | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl index d1f6f0aced..4732c9b136 100755 --- a/challenge-168/steve-g-lynn/perl/ch-2.pl +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -1,13 +1,17 @@ #!/usr/bin/perl -use Math::Prime::XS qw(is_prime sieve_primes); - +use Math::Prime::XS qw(is_prime); +print &home_prime(10),"\n"; +#773 print &home_prime(16),"\n"; -#-- should be 31636373 [wikipedia example] +#31636373 +print &home_prime(20),"\n"; +#3318308475676071413 sub home_prime { - local ($n)=@_; + my ($n)=@_; + is_prime($n) && return $n; while (1){ $n=&factors($n); (is_prime($n)) && last; @@ -18,20 +22,25 @@ sub home_prime { sub factors { #--return concatenated prime factors of a number n - local ($n)=@_; - local @primes=sieve_primes($n); - local $retstring=""; + my ($n)=@_; + local $sqrt_n=int(sqrt($n)); + + my $retstring=""; if (is_prime($n)){ return $n; } else { - foreach $prime (sort{$a<=>$b} @primes){ + for $prime (2 .. $sqrt_n){ + #-- no need to get primes first + #-- routine automatically finds only prime factors while ( ($n % $prime) == 0){ $n /= $prime; $retstring .= $prime; } + (is_prime($n)) && last; } + ($n > 1) && ($retstring .= $n); } return $retstring; } -- cgit From 5182b192585848d1b91afce11d151906762d9bcd Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sat, 11 Jun 2022 21:07:19 +0800 Subject: improved ch-2.pl --- challenge-168/steve-g-lynn/perl/ch-2.pl | 1 + 1 file changed, 1 insertion(+) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl index 4732c9b136..4fcc0e19dd 100755 --- a/challenge-168/steve-g-lynn/perl/ch-2.pl +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -9,6 +9,7 @@ print &home_prime(16),"\n"; print &home_prime(20),"\n"; #3318308475676071413 + sub home_prime { my ($n)=@_; is_prime($n) && return $n; -- cgit From 337a373ae28cb51b8d74be6505b2f695e5ac2cb5 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sat, 11 Jun 2022 21:10:58 +0800 Subject: improved perl ch-2.pl --- challenge-168/steve-g-lynn/perl/ch-2.pl | 2 ++ 1 file changed, 2 insertions(+) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl index 4fcc0e19dd..67db0846ea 100755 --- a/challenge-168/steve-g-lynn/perl/ch-2.pl +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -10,6 +10,7 @@ print &home_prime(20),"\n"; #3318308475676071413 + sub home_prime { my ($n)=@_; is_prime($n) && return $n; @@ -21,6 +22,7 @@ sub home_prime { } + sub factors { #--return concatenated prime factors of a number n my ($n)=@_; -- cgit From ff980517b9e50ee68b469cddc0ca26e5f9982cee Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sat, 11 Jun 2022 21:20:08 +0800 Subject: improved ch-2.pl --- challenge-168/steve-g-lynn/perl/ch-2.pl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl index 67db0846ea..be079624bc 100755 --- a/challenge-168/steve-g-lynn/perl/ch-2.pl +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -9,8 +9,6 @@ print &home_prime(16),"\n"; print &home_prime(20),"\n"; #3318308475676071413 - - sub home_prime { my ($n)=@_; is_prime($n) && return $n; @@ -21,12 +19,10 @@ sub home_prime { return $n; } - - sub factors { #--return concatenated prime factors of a number n my ($n)=@_; - local $sqrt_n=int(sqrt($n)); + local $sqrt_n=floor(sqrt($n)); my $retstring=""; -- cgit From bfba759aa6812adf1536870315cc6648b97163c5 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sat, 11 Jun 2022 21:45:00 +0800 Subject: improved ch-2.pl --- challenge-168/steve-g-lynn/perl/ch-2.pl | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl index be079624bc..5fe95544ad 100755 --- a/challenge-168/steve-g-lynn/perl/ch-2.pl +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -4,8 +4,10 @@ use Math::Prime::XS qw(is_prime); print &home_prime(10),"\n"; #773 + print &home_prime(16),"\n"; #31636373 + print &home_prime(20),"\n"; #3318308475676071413 @@ -43,3 +45,6 @@ sub factors { } return $retstring; } + +1; + -- cgit From fbe67d4a7029265af7d2e0a7ff6bd642bc748105 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 11 Jun 2022 19:03:46 +0200 Subject: feat(challenge-168/lubos-kolouch/p[erl,ython]/ch-2.p[ly]): Challenge 168 Task 2 LK Perl Python --- challenge-168/lubos-kolouch/perl/ch-2.pl | 24 ++++++++++++++++++++++++ challenge-168/lubos-kolouch/python/ch-2.py | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 challenge-168/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-168/lubos-kolouch/python/ch-2.py (limited to 'challenge-168') diff --git a/challenge-168/lubos-kolouch/perl/ch-2.pl b/challenge-168/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1390fba896 --- /dev/null +++ b/challenge-168/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,24 @@ +use strict; +use warnings; +use Math::Prime::Util qw/is_prime factor/; + +sub home_prime { + my $n = shift; + + while ( !is_prime($n) ) { + my @factors = factor($n); + my $sum = ''; + foreach my $factor (@factors) { + $sum .= $factor; + } + $n = $sum; + } + + return $n; +} + +use Test::More; + +is( home_prime(2), 2 ); +is( home_prime(10), 773 ); +done_testing; diff --git a/challenge-168/lubos-kolouch/python/ch-2.py b/challenge-168/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..16094e836d --- /dev/null +++ b/challenge-168/lubos-kolouch/python/ch-2.py @@ -0,0 +1,19 @@ +from sympy import factorint, isprime + + +def home_prime(n): + + while not isprime(n): + factors = factorint(n) + + my_sum = "" + for (my_factor, repetition) in factors.items(): + my_sum += str(my_factor) * repetition + + n = int(my_sum) + + return n + + +assert home_prime(2) == 2 +assert home_prime(10) == 773 -- cgit From ca71189e5959ea925a05c5b4412a64e5d10efe96 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 12 Jun 2022 05:00:23 +0800 Subject: improve efficiency --- challenge-168/cheok-yin-fung/perl/ch-2.pl | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl index 3289846cfe..7466f52b01 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -27,7 +27,7 @@ sub hp { sub my_hp { my $recur_depth = $_[1]; - die "Walk so far but still cannot get result. :(\n" if $recur_depth > 10; + die "Walk so far but still cannot get result. :(\n" if $recur_depth > 20; my $num = $_[0]; if (is_prime($num) == 2) { return $num; @@ -35,27 +35,35 @@ sub my_hp { my @factors = (); my $p = 2; - do { + while ($num != 1) { if (!($num % $p)) { push @factors, $p; $num /= $p; } else { - $p = next_prime($p); # CAN BE IMPROVED + $p = next_prime($p); } - } while ($num != 1); + if ($p > sqrt $num) { + push @factors, $num; + last; + } + } my $nxt = (reduce { $a . $b } @factors); say " $nxt"; return my_hp($nxt, ++$recur_depth); } -use Test::More tests => 5; +use Test::More tests => 7; # test data from OEIS ok hp(10) == 773; ok hp(24) == 331_319; ok hp(32) == 241_271; +ok hp(40) == 3314192745739; +ok hp(44) == 22815088913; ok hp(45) == 3_411_949; +ok hp(8) == 3331113965338635107; -ok hp(44) == 22815088913; # this test spends approx 0.5 min (on CY's laptop). - -# ok hp(40) == 3314192745739; # this test spends approx. 2 min. +# time: +# real 0m1.963s +# user 0m1.946s +# sys 0m0.016s -- cgit From eb043eb2670bd851f4f237961dc12036927fc570 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 12 Jun 2022 05:42:04 +0800 Subject: failed attempt: is_prime(BigInt) does not work --- challenge-168/cheok-yin-fung/perl/ch-2.pl | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl index 7466f52b01..0f08ab3dc4 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -28,7 +28,7 @@ sub my_hp { my $recur_depth = $_[1]; die "Walk so far but still cannot get result. :(\n" if $recur_depth > 20; - my $num = $_[0]; + my $num = Math::BigInt->new($_[0]); if (is_prime($num) == 2) { return $num; } @@ -36,24 +36,28 @@ sub my_hp { my @factors = (); my $p = 2; while ($num != 1) { - if (!($num % $p)) { + my $temp_num = $num; + if ( $num->bmod($p) == 0 ) { + say $p; push @factors, $p; - $num /= $p; + $num = $num->bdiv($p); } else { $p = next_prime($p); } - if ($p > sqrt $num) { + if ($p > $temp_num->bsqrt()) { push @factors, $num; last; } } - my $nxt = (reduce { $a . $b } @factors); + my $nxt = join "", @factors; say " $nxt"; return my_hp($nxt, ++$recur_depth); } -use Test::More tests => 7; + +=pod +use Test::More tests => 8; # test data from OEIS ok hp(10) == 773; ok hp(24) == 331_319; @@ -61,9 +65,16 @@ ok hp(32) == 241_271; ok hp(40) == 3314192745739; ok hp(44) == 22815088913; ok hp(45) == 3_411_949; -ok hp(8) == 3331113965338635107; +ok hp( 8) == 3331113965338635107; +ok hp(48) == 6161791591356884791277; -# time: +# time without the last hp(48) test case: # real 0m1.963s # user 0m1.946s # sys 0m0.016s +# +# time without the last hp(48) test case and use simply Math::Prime::Util : +# real 0m0.133s +# user 0m0.115s +# sys 0m0.011s + -- cgit From f95f9de8bebf4967125de3a10999cbe524f9f1a3 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Sat, 11 Jun 2022 18:02:10 -0400 Subject: Forgot to finish --- challenge-168/dave-jacoby/blog.txt | 3 +++ challenge-168/dave-jacoby/perl/ch-1.pl | 28 +++++++++++++++++++ challenge-168/dave-jacoby/perl/ch-2.pl | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 challenge-168/dave-jacoby/blog.txt create mode 100644 challenge-168/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-168/dave-jacoby/perl/ch-2.pl (limited to 'challenge-168') diff --git a/challenge-168/dave-jacoby/blog.txt b/challenge-168/dave-jacoby/blog.txt new file mode 100644 index 0000000000..b28b04f643 --- /dev/null +++ b/challenge-168/dave-jacoby/blog.txt @@ -0,0 +1,3 @@ + + + diff --git a/challenge-168/dave-jacoby/perl/ch-1.pl b/challenge-168/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..39a1510b86 --- /dev/null +++ b/challenge-168/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @perrin = ( 3, 0, 2 ); +my %perrin_primes; +$perrin_primes{2} = 1; +$perrin_primes{3} = 1; + +while ( scalar keys %perrin_primes < 13 ) { + my $x = $perrin[-2] + $perrin[-3]; + push @perrin, $x; + $perrin_primes{$x} = 1 if is_prime($x); +} + +say join ' ', sort { $a <=> $b } keys %perrin_primes; +exit; + +sub is_prime ($n) { + die "Bad number $n" unless length $n; + return 0 if $n == 0; + return 0 if $n == 1; + for ( 2 .. sqrt $n ) { return 0 unless $n % $_ } + return 1; +} + diff --git a/challenge-168/dave-jacoby/perl/ch-2.pl b/challenge-168/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..1d101a36a4 --- /dev/null +++ b/challenge-168/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Carp; +use Try::Tiny; +use Math::Prime::XS qw{ is_prime }; + +$| = 1; + +my @n = @ARGV; +push @n, 10 unless scalar @ARGV; + +for my $i (@n) { + try { + my $p = get_home_prime($i); + say join "\t", '-', $i, $p; + } + catch { + say $_; + }; +} + +sub get_home_prime($n) { + my $p = $n; + while ( !is_prime($p) ) { + my @factors = get_factors($p); + $p = join '', @factors; + print qq{$p }; + # croak 'Too Big, Too Slow' if length $p > 10; + } + say ''; + return $p; +} + +sub get_factors( $n ) { + my @factors; + for my $i ( 2 .. $n ) { + next unless $n % $i == 0; + while ( $n % $i == 0 ) { + push @factors, $i; + $n = $n / $i; + } + } + return @factors; +} + -- cgit From 87a49e0dde51ea6a342eaecef8820bf0b3bc5ead Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 12 Jun 2022 06:10:22 +0800 Subject: finalize Task 1 --- challenge-168/cheok-yin-fung/perl/ch-2.pl | 46 +++++++++++++------------------ 1 file changed, 19 insertions(+), 27 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl index 0f08ab3dc4..3e16406fbd 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -1,5 +1,3 @@ -# CAN BE IMPROVED - #!/usr/bin/perl # The Weekly Challenge 168 # Task 2 Home Prime @@ -7,14 +5,10 @@ use v5.24.0; use warnings; use List::Util qw/reduce/; -use Math::BigInt::GMP; # [remark] -use Math::BigInt::Pari; # [remark] -use Math::Prime::Util::GMP qw/is_prime next_prime/; -use bigint try => 'GMP,Pari'; # [remark] -# remark: follow suggestions on POD of Math::Prime::Util - +use Math::Prime::Util qw/is_prime next_prime/; +use Math::BigInt; -say hp($ARGV[0]) if defined($ARGV[0]); +say hp($ARGV[0]) if defined($ARGV[0]) && $ARGV[0] != 1; @@ -27,8 +21,14 @@ sub hp { sub my_hp { my $recur_depth = $_[1]; + die "Number involed too large.\n" + if + Math::BigInt->new(18446744073709551616) # 2**64 + ->bcmp( Math::BigInt->new($_[0]) ) + == -1; + die "Walk so far but still cannot get result. :(\n" if $recur_depth > 20; - my $num = Math::BigInt->new($_[0]); + my $num = $_[0]; if (is_prime($num) == 2) { return $num; } @@ -36,27 +36,23 @@ sub my_hp { my @factors = (); my $p = 2; while ($num != 1) { - my $temp_num = $num; - if ( $num->bmod($p) == 0 ) { - say $p; + if (!($num % $p)) { push @factors, $p; - $num = $num->bdiv($p); + $num /= $p; } else { $p = next_prime($p); } - if ($p > $temp_num->bsqrt()) { + if ($p > sqrt $num) { push @factors, $num; last; } } - my $nxt = join "", @factors; + my $nxt = (reduce { $a . $b } @factors); say " $nxt"; return my_hp($nxt, ++$recur_depth); } - -=pod use Test::More tests => 8; # test data from OEIS ok hp(10) == 773; @@ -65,16 +61,12 @@ ok hp(32) == 241_271; ok hp(40) == 3314192745739; ok hp(44) == 22815088913; ok hp(45) == 3_411_949; + +# bigger cases; ok hp( 8) == 3331113965338635107; -ok hp(48) == 6161791591356884791277; +ok hp(20) == 3318308475676071413; -# time without the last hp(48) test case: -# real 0m1.963s -# user 0m1.946s +# real 0m5.553s +# user 0m5.535s # sys 0m0.016s -# -# time without the last hp(48) test case and use simply Math::Prime::Util : -# real 0m0.133s -# user 0m0.115s -# sys 0m0.011s -- cgit From a2d9f56f20f4773112b0656892fd4a0945fbe401 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 12 Jun 2022 06:10:58 +0800 Subject: finalize Task 2 --- challenge-168/cheok-yin-fung/perl/ch-2.pl | 1 - 1 file changed, 1 deletion(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl index 3e16406fbd..ea156c4976 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -4,7 +4,6 @@ # Usage: ch-2.pl [integer > 1] use v5.24.0; use warnings; -use List::Util qw/reduce/; use Math::Prime::Util qw/is_prime next_prime/; use Math::BigInt; -- cgit From 7860753cd854ea07a29511477a5e4a3dca5a885e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 11 Jun 2022 23:34:50 +0100 Subject: - Added solutions by Laurent Rosenfeld. --- challenge-168/laurent-rosenfeld/blog.txt | 1 + challenge-168/laurent-rosenfeld/perl/ch-1.pl | 33 ++++++++++++++++++++++++++ challenge-168/laurent-rosenfeld/raku/ch-1.raku | 1 + 3 files changed, 35 insertions(+) create mode 100644 challenge-168/laurent-rosenfeld/blog.txt create mode 100644 challenge-168/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-168/laurent-rosenfeld/raku/ch-1.raku (limited to 'challenge-168') diff --git a/challenge-168/laurent-rosenfeld/blog.txt b/challenge-168/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..388761ba75 --- /dev/null +++ b/challenge-168/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2022/06/perl-weekly-challenge-168-perrin-primes.html diff --git a/challenge-168/laurent-rosenfeld/perl/ch-1.pl b/challenge-168/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..906b341080 --- /dev/null +++ b/challenge-168/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,33 @@ +use strict; +use warnings; +use feature "say"; + +my @perrin = (3, 0, 2); +my @result = (2, 3); +my %seen = map { $_ => 1 } @result; +while (1) { + my $new_item = $perrin[-3] + $perrin[-2]; + push @perrin, $new_item; + if (is_prime($new_item)) { + push @result, $new_item unless $seen{$new_item}; + $seen{$new_item} = 1; + last if @result > 12; + } +} +say join " ", sort { $a <=> $b } @result; + +sub is_prime { + my $n = shift; + return 1 if $n == 2; + return 0 if $n % 2 == 0; + return 0 if $n == 1; + + my $p = 3; + my $sqrt = sqrt $n; + while ($p <= $sqrt) { + # return 1 if $p >= $n; + return 0 if $n % $p == 0; + $p += 2; + } + return 1; +} diff --git a/challenge-168/laurent-rosenfeld/raku/ch-1.raku b/challenge-168/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..f00ae2d3ef --- /dev/null +++ b/challenge-168/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1 @@ +say (unique grep {.is-prime}, (3, 0, 2, -> $a, $b, $c { $a + $b } ... ∞))[0..12].sort; -- cgit From 8a6b17c38547a2663b7a46e51dc8fc07c1201889 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sun, 12 Jun 2022 11:21:46 +0800 Subject: faster ch-2.pl & ch-2.p6 using external libs --- challenge-168/steve-g-lynn/perl/ch-2.pl | 37 ++++++------------------------ challenge-168/steve-g-lynn/raku/ch-2.p6 | 40 ++++++--------------------------- 2 files changed, 14 insertions(+), 63 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl index 5fe95544ad..379ffd9078 100755 --- a/challenge-168/steve-g-lynn/perl/ch-2.pl +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -1,6 +1,11 @@ #!/usr/bin/perl -use Math::Prime::XS qw(is_prime); +#time (bash) output: +#real 0m0.026s +#user 0m0.019s +#sys 0m0.007s + +use Math::Prime::Util qw(is_prime factor); print &home_prime(10),"\n"; #773 @@ -14,37 +19,9 @@ print &home_prime(20),"\n"; sub home_prime { my ($n)=@_; is_prime($n) && return $n; - while (1){ - $n=&factors($n); - (is_prime($n)) && last; - } - return $n; + return &home_prime(join('',sort{$a<=>$b}(factor($n)))); } -sub factors { - #--return concatenated prime factors of a number n - my ($n)=@_; - local $sqrt_n=floor(sqrt($n)); - - my $retstring=""; - - if (is_prime($n)){ - return $n; - } - else { - for $prime (2 .. $sqrt_n){ - #-- no need to get primes first - #-- routine automatically finds only prime factors - while ( ($n % $prime) == 0){ - $n /= $prime; - $retstring .= $prime; - } - (is_prime($n)) && last; - } - ($n > 1) && ($retstring .= $n); - } - return $retstring; -} 1; diff --git a/challenge-168/steve-g-lynn/raku/ch-2.p6 b/challenge-168/steve-g-lynn/raku/ch-2.p6 index f3a1259691..869bc8b3b5 100755 --- a/challenge-168/steve-g-lynn/raku/ch-2.p6 +++ b/challenge-168/steve-g-lynn/raku/ch-2.p6 @@ -1,11 +1,13 @@ #!/usr/bin/raku #time (bash command): -#real 0m0.511s -#user 0m0.622s -#sys 0m0.046s +#real 0m0.312s +#user 0m0.382s +#sys 0m0.054s +use Prime::Factor; + say homeprime(10); #773 say homeprime(16); @@ -15,37 +17,9 @@ say homeprime(20); #-- sub for home prime -sub homeprime(Int $n) returns Int { +sub homeprime(Int $n where ($n > 1)) returns Int { $n.Int.is-prime && return $n; - return homeprime(factor($n)); -} - -#--sub for factorizing - -sub factor (Int $n where $n > 1) returns Int { -#-- returns the concatenated prime factors as an int - $n.Int.is-prime && (return $n.Int ); - my Int $sqrt_n = sqrt($n).Int; - - my @factors=(); - - my Str $retstring=""; - my Int $ncopy=$n.Int; - - for (2 .. $sqrt_n) -> $prime { - #-- no need to generate primes first.. the routine will - #-- automatically only choose prime factors - while ($ncopy %% $prime) { - $ncopy div= $prime; #-- integer division - @factors.append($prime); - } - $ncopy.is-prime && last; - } - ($ncopy > 1) && @factors.append($ncopy); - #-- any factor bigger than sqrt(n) is a prime factor - $retstring = @factors.list.join; - - return $retstring.Int; + return homeprime(([~] prime-factors($n)).Int); } -- cgit From 625339f89da8024a9de690f5ed163839989fa40a Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 12 Jun 2022 11:26:01 +0800 Subject: Task 2 big num --- challenge-168/cheok-yin-fung/perl/ch-2.pl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl index ea156c4976..6986c4809a 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -18,20 +18,20 @@ sub hp { sub my_hp { + my $num = $_[0]; my $recur_depth = $_[1]; - - die "Number involed too large.\n" + die "Number involved too large.\n" if - Math::BigInt->new(18446744073709551616) # 2**64 - ->bcmp( Math::BigInt->new($_[0]) ) - == -1; + Math::BigInt->new("18446744073709551616") # 2**64 + ->ble( Math::BigInt->new($num) ); + - die "Walk so far but still cannot get result. :(\n" if $recur_depth > 20; - my $num = $_[0]; if (is_prime($num) == 2) { return $num; } + die "Walk so far but still cannot get result. :(\n" if $recur_depth > 20; + my @factors = (); my $p = 2; while ($num != 1) { @@ -47,7 +47,7 @@ sub my_hp { last; } } - my $nxt = (reduce { $a . $b } @factors); + my $nxt = join "", @factors; say " $nxt"; return my_hp($nxt, ++$recur_depth); } -- cgit From 0d0f645575dc2177e765b1e4b64806f591663199 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 12 Jun 2022 11:33:20 +0800 Subject: Task 1 final --- challenge-168/cheok-yin-fung/perl/ch-1.pl | 14 +++++--------- challenge-168/cheok-yin-fung/perl/ch-2.pl | 1 + 2 files changed, 6 insertions(+), 9 deletions(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-1.pl b/challenge-168/cheok-yin-fung/perl/ch-1.pl index 61ba32d69e..3f51cb4766 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-1.pl @@ -3,12 +3,8 @@ # Task 1 Perrin Prime use v5.24.0; use warnings; -use List::Util qw/reduce none/; -use Math::BigInt::GMP; # [remark] -use Math::BigInt::Pari; # [remark] -use Math::Prime::Util::GMP qw/is_prime next_prime/; -use bigint try => 'GMP,Pari'; # [remark] -# remark: follow suggestions on POD of Math::Prime::Util +use List::Util qw/none/; +use Math::Prime::Util qw/is_prime next_prime/; my @perrin_primes = (2,3); my ($ppnm3,$ppnm2,$ppnm1, $ppn) = (3,0,2,3); @@ -27,7 +23,7 @@ while (scalar @perrin_primes < 13) { say join ", ", @perrin_primes; # time: -# real 0m0.078s -# user 0m0.056s -# sys 0m0.008s +# real 0m0.025s +# user 0m0.017s +# sys 0m0.009s diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl index 6986c4809a..00406d25ef 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl @@ -43,6 +43,7 @@ sub my_hp { $p = next_prime($p); } if ($p > sqrt $num) { + say "useful"; push @factors, $num; last; } -- cgit From e66b82a635ca6306adf4b7d46e8b675159e47c8e Mon Sep 17 00:00:00 2001 From: CY Fung Date: Sun, 12 Jun 2022 11:34:11 +0800 Subject: Task 1 final --- challenge-168/cheok-yin-fung/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-168') diff --git a/challenge-168/cheok-yin-fung/perl/ch-1.pl b/challenge-168/cheok-yin-fung/perl/ch-1.pl index 3f51cb4766..faf90a9bb3 100644 --- a/challenge-168/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-168/cheok-yin-fung/perl/ch-1.pl @@ -4,7 +4,7 @@ use v5.24.0; use warnings; use List::Util qw/none/; -use Math::Prime::Util qw/is_prime next_prime/; +use Math::Prime::Util qw/is_prime/; my @perrin_primes = (2,3); my ($ppnm3,$ppnm2,$ppnm1, $ppn) = (3,0,2,3); -- cgit From 2eb42e251cedc5dfc84e8537f5c5a34a7f3f76bf Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Sun, 12 Jun 2022 03:28:29 -0400 Subject: new file: challenge-168/mattneleigh/perl/ch-1.pl new file: challenge-168/mattneleigh/perl/ch-2.pl --- challenge-168/mattneleigh/perl/ch-1.pl | 99 +++++++++++++++++++++++ challenge-168/mattneleigh/perl/ch-2.pl | 141 +++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100755 challenge-168/mattneleigh/perl/ch-1.pl create mode 100755 challenge-168/mattneleigh/perl/ch-2.pl (limited to 'challenge-168') diff --git a/challenge-168/mattneleigh/perl/ch-1.pl b/challenge-168/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..3def181d44 --- /dev/null +++ b/challenge-168/mattneleigh/perl/ch-1.pl @@ -0,0 +1,99 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @perrin = (3, 0, 2); +my %perrin_primes = ( + 3 => 1, + 2 => 1 +); +my $num = 13; + +while(scalar(keys(%perrin_primes)) < $num){ + next_perrin(\@perrin, 1); + $perrin_primes{$perrin[$#perrin]} = 1 + if(is_prime($perrin[$#perrin])); +} + +print("\n"); +printf( + "f(%d) = [%s]\n", + $num, + join(", ", sort({ $a <=> $b } keys(%perrin_primes))) +); +print("\n"); + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculae the next number in the Perrin sequence, adding it to an existing +# list of Perrin numbers; see https://en.wikipedia.org/wiki/Perrin_number for +# details about this type of number +# Takes one or two arguments: +# * A ref to a list of Perrin numbers; upon first call to this function, this +# list must be [ 3, 0, 2 ] +# * An agument that, if defined, will cause the first member of the list to be +# removed after the new one is added, preventing the list from growing +# Returns no meaningful value +################################################################################ +sub next_perrin{ + my $perrin = shift(); + + # Add the next number in the sequence + push( + @{$perrin}, + $perrin->[$#$perrin - 2] + $perrin->[$#$perrin - 1] + ); + + # If specified, toss out the oldest number + shift(@{$perrin}) + if(defined(shift())); + +} + + + +################################################################################ +# Determine whether a given integer N is prime +# Takes one argument: +# * The integer N +# Returns on success: +# * 1 if N is prime +# * 0 if N is not prime +# NOTE: If N is less than zero, it will always be considered nonprime +################################################################################ +sub is_prime{ + my $n = int(shift()); + + my $i; + + # Take care of a few easy cases + return(1) + if(($n == 2) || ($n == 3)); + return(0) + if(($n <= 1) || !($n % 2) || !($n % 3)); + + # See if certain factors divide evenly + for($i = 5; $i * $i <= $n; $i += 6){ + if(!($n % $i) || !($n % ($i + 2))){ + return(0); + } + } + + return(1); + +} + + + diff --git a/challenge-168/mattneleigh/perl/ch-2.pl b/challenge-168/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..73a5a0962f --- /dev/null +++ b/challenge-168/mattneleigh/perl/ch-2.pl @@ -0,0 +1,141 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my $n = 10; + +# If you have some time on your hands... +# $n = 8; + +print("\n"); +printf("HP(%d) = %d\n", $n, calculate_home_prime($n)); +print("\n"); + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculate the home prime of a given integer; see +# https://en.wikipedia.org/wiki/Home_prime for details about this type of +# number +# Takes one argument: +# * An integer N, which must be at least two (2) +# Returns on success: +# * The home prime of N +# Returns on error: +# * undef if N is less than two (2) +# NOTE: For certain values of N, this calculation can take wholly unreasonable +# amounts of time +################################################################################ +sub calculate_home_prime{ + my $n = int(shift()); + + return(undef) + if($n < 2); + + # Loop until $n is prime + until(is_prime($n)){ + my @factors; + + # Get the prime factors of $n, and concatenate + # them into a new $n + prime_factorize_number($n, \@factors); + $n = join("", @factors); + } + + return($n); + +} + + + +################################################################################ +# Find the prime factorization of a given integer +# Takes two arguments: +# * The integer N to examine and factor +# * A ref to a list that will be populated with prime factors in ascending +# order; any previous contents will be deleted +# Returns no meaningful value +################################################################################ +sub prime_factorize_number{ + use POSIX; + + my $n = int(shift()); + my $factors = shift(); + + # Clobber existing list contents if any + splice(@{$factors}, 0) + if(scalar(@{$factors})); + + # Loop until $n is prime + until(is_prime($n)){ + # $n is not prime; set an upper bound on + # our factor search + my $i; + my $max = ceil(sqrt($n)); + + # Loop until we find prime $i that + # divides evenly into $n + for($i=2; $i<=$max; $i++){ + next unless(is_prime($i)); + last unless($n % $i); + } + + # Store the new prime factor $i and + # divide $n by it + push(@{$factors}, $i); + $n /= $i; + + } + + # Store $n, which by now is the last prime + # factor of the originally-supplied argument + push(@{$factors}, $n); + +} + + + +################################################################################ +# Determine whether a given integer N is prime +# Takes one argument: +# * The integer N +# Returns on success: +# * 1 if N is prime +# * 0 if N is not prime +# NOTE: If N is less than zero, it will always be considered nonprime +################################################################################ +sub is_prime{ + my $n = int(shift()); + + my $i; + + # Take care of a few easy cases + return(1) + if(($n == 2) || ($n == 3)); + return(0) + if(($n <= 1) || !($n % 2) || !($n % 3)); + + # See if certain factors divide evenly + for($i = 5; $i * $i <= $n; $i += 6){ + if(!($n % $i) || !($n % ($i + 2))){ + return(0); + } + } + + return(1); + +} + + + -- cgit From d6423b19ceac1cd20b9fd057ce3a82ca16111740 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Sun, 12 Jun 2022 03:33:21 -0400 Subject: modified: challenge-168/mattneleigh/perl/ch-1.pl --- challenge-168/mattneleigh/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-168') diff --git a/challenge-168/mattneleigh/perl/ch-1.pl b/challenge-168/mattneleigh/perl/ch-1.pl index 3def181d44..9ea3014572 100755 --- a/challenge-168/mattneleigh/perl/ch-1.pl +++ b/challenge-168/mattneleigh/perl/ch-1.pl @@ -37,7 +37,7 @@ exit(0); ################################################################################ -# Calculae the next number in the Perrin sequence, adding it to an existing +# Calculate the next number in the Perrin sequence, adding it to an existing # list of Perrin numbers; see https://en.wikipedia.org/wiki/Perrin_number for # details about this type of number # Takes one or two arguments: -- cgit From 8b3d4098625fe8fc4c962796812993aedf893441 Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Sun, 12 Jun 2022 16:17:37 +0800 Subject: modified ch-2.jl; tried homeprime(48,65,96) --- challenge-168/steve-g-lynn/julia/ch-2.jl | 43 +++++++++++++++++++++++++++++++- challenge-168/steve-g-lynn/perl/ch-2.pl | 8 ++++++ challenge-168/steve-g-lynn/raku/ch-2.p6 | 7 ++++++ 3 files changed, 57 insertions(+), 1 deletion(-) (limited to 'challenge-168') diff --git a/challenge-168/steve-g-lynn/julia/ch-2.jl b/challenge-168/steve-g-lynn/julia/ch-2.jl index 19d4c7d498..0828857250 100755 --- a/challenge-168/steve-g-lynn/julia/ch-2.jl +++ b/challenge-168/steve-g-lynn/julia/ch-2.jl @@ -1,15 +1,56 @@ #! /usr/bin/julia +#time (bash) output: with homeprime.([10,16,20,48,65,96]) +#real 0m41.340s +#user 0m41.149s +#sys 0m0.493s + +#-- without homeprime(96): +#real 0m4.744s +#user 0m4.745s +#sys 0m0.467s + +#-- without .homeprime.([96,65]) +#real 0m1.072s +#user 0m1.138s +#sys 0m0.419s + +#-- without .homeprime.([96,65,48]) +#real 0m1.002s +#user 0m1.095s +#sys 0m0.384s + + + using Primes function homeprime(n::Int64) + return homeprime(BigInt(n)) +end + +function homeprime(n::BigInt) ncopy=n while (!isprime(ncopy)) - ncopy=parse(Int64,join(factor(Vector,ncopy))) + ncopy=parse(BigInt,join(factor(Vector,ncopy))) end return ncopy end +println(homeprime(10)) +#773 + println(homeprime(16)) #31636373 +println(homeprime(20)) +#3318308475676071413 + +println(homeprime(48)) +#6161791591356884791277 + +println(homeprime(65)) +#1381321118321175157763339900357651 + +println(homeprime(96)) +#172929671097972226356946608292031596899264419 + diff --git a/challenge-168/steve-g-lynn/perl/ch-2.pl b/challenge-168/steve-g-lynn/perl/ch-2.pl index 379ffd9078..0548ac1ebc 100755 --- a/challenge-168/steve-g-lynn/perl/ch-2.pl +++ b/challenge-168/steve-g-lynn/perl/ch-2.pl @@ -5,6 +5,14 @@ #user 0m0.019s #sys 0m0.007s +# but this fast time degrades sharply with larger problems +# +# real is 0m45.199s if home_prime(48) also printed +# real is 5m5.662s if home_prime of 48 and 65 also printed +# real is 8m50.059s if home_prime of 48, 65 and 96 also printed +# +# probably some slowdown in factor(.) for factors of very large primes + use Math::Prime::Util qw(is_prime factor); print &home_prime(10),"\n"; diff --git a/challenge-168/steve-g-lynn/raku/ch-2.p6 b/challenge-168/steve-g-lynn/raku/ch-2.p6 index 869bc8b3b5..09cbc4412b 100755 --- a/challenge-168/steve-g-lynn/raku/ch-2.p6 +++ b/challenge-168/steve-g-lynn/raku/ch-2.p6 @@ -5,6 +5,13 @@ #user 0m0.382s #sys 0m0.054s +#time if homeprime(48), homeprime(65) and homeprime(96) are also printed: +#real 2m23.931s +#user 2m23.527s +#sys 0m0.071s +# +# ( real 11.512s with homeprime (10,16,20,48,65) +# real 0.922s with homeprime (10,16,20,48) ) use Prime::Factor; -- cgit From f1dcdae45ca76173c53de4f4f284bfae3a2d9aa3 Mon Sep 17 00:00:00 2001 From: Flavio Poletti Date: Sun, 12 Jun 2022 10:18:43 +0200 Subject: Add polettix's solution to challenge-168 --- challenge-168/polettix/blog.txt | 1 + challenge-168/polettix/blog1.txt | 1 + challenge-168/polettix/perl/ch-1.pl | 21 +++++++++++++++ challenge-168/polettix/perl/ch-2.pl | 17 ++++++++++++ challenge-168/polettix/perl/cpanfile | 1 + challenge-168/polettix/perl/cpanfile.snapshot | 38 +++++++++++++++++++++++++++ challenge-168/polettix/raku/ch-1.raku | 28 ++++++++++++++++++++ challenge-168/polettix/raku/ch-2.raku | 31 ++++++++++++++++++++++ 8 files changed, 138 insertions(+) create mode 100644 challenge-168/polettix/blog.txt create mode 100644 challenge-168/polettix/blog1.txt create mode 100644 challenge-168/polettix/perl/ch-1.pl create mode 100644 challenge-168/polettix/perl/ch-2.pl create mode 100644 challenge-168/polettix/perl/cpanfile create mode 100644 challenge-168/polettix/perl/cpanfile.snapshot create mode 100644 challenge-168/polettix/raku/ch-1.raku create mode 100644 challenge-168/polettix/raku/ch-2.raku (limited to 'challenge-168') diff --git a/challenge-168/polettix/blog.txt b/challenge-168/polettix/blog.txt new file mode 100644 index 0000000000..f6337130ae --- /dev/null +++ b/challenge-168/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2022/06/08/pwc168-perrin-prime/ diff --git a/challenge-168/polettix/blog1.txt b/challenge-168/polettix/blog1.txt new file mode 100644 index 0000000000..ad226cc866 --- /dev/null +++ b/challenge-168/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2022/06/09/pwc168-home-prime/ diff --git a/challenge-168/polettix/perl/ch-1.pl b/challenge-168/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..e4952fb0ed --- /dev/null +++ b/challenge-168/polettix/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +use ntheory 'is_prime'; + +my $n = shift // 13; +say join ', ', perrin_primes($n); + +sub perrin_primes ($n) { + my @pps = (2, 3, 5); + my @state = (2, 5, 5); + while (@pps < $n) { + push @state, my $candidate = $state[0] + $state[1]; + shift @state; + push @pps, $candidate if is_prime($candidate); + } + return @pps; +} diff --git a/challenge-168/polettix/perl/ch-2.pl b/challenge-168/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..24a1dd1255 --- /dev/null +++ b/challenge-168/polettix/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +use ntheory qw< factor >; + +my $n = shift // 10; +say home_prime($n); + +sub home_prime { + my $next = join '', factor($_[0]); + return $next if $next eq $_[0]; + $_[0] = $next; + goto &home_prime; +} diff --git a/challenge-168/polettix/perl/cpanfile b/challenge-168/polettix/perl/cpanfile new file mode 100644 index 0000000000..94ac365d6f --- /dev/null +++ b/challenge-168/polettix/perl/cpanfile @@ -0,0 +1 @@ +requires 'ntheory'; diff --git a/challenge-168/polettix/perl/cpanfile.snapshot b/challenge-168/polettix/perl/cpanfile.snapshot new file mode 100644 index 0000000000..2fd600a3af --- /dev/null +++ b/challenge-168/polettix/perl/cpanfile.snapshot @@ -0,0 +1,38 @@ +# carton snapshot format: version 1.0 +DISTRIBUTIONS + Math-Prime-Util-0.73 + pathname: D/DA/DANAJ/Math-Prime-Util-0.73.tar.gz + provides: + Math::Prime::Util 0.73 + Math::Prime::Util::ChaCha 0.73 + Math::Prime::Util::Entropy 0.73 + Math::Prime::Util::MemFree 0.73 + Math::Prime::Util::PP 0.73 + Math::Prime::Util::PrimeArray 0.73 + Math::Prime::Util::PrimeIterator 0.73 + ntheory 0.73 + requirements: + Carp 0 + Config 0 + Exporter 5.57 + ExtUtils::MakeMaker 0 + Math::BigFloat 1.59 + Math::BigInt 1.88 + Math::Prime::Util::GMP 0.50 + Tie::Array 0 + XSLoader 0.01 + base 0 + constant 0 + perl 5.006002 + Math-Prime-Util-GMP-0.52 + pathname: D/DA/DANAJ/Math-Prime-Util-GMP-0.52.tar.gz + provides: + Math::Prime::Util::GMP 0.52 + requirements: + Carp 0 + Exporter 5.57 + ExtUtils::MakeMaker 0 + Fcntl 0 + XSLoader 0.01 + base 0 + perl 5.006002 diff --git a/challenge-168/polettix/raku/ch-1.raku b/challenge-168/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..28f3b1fb86 --- /dev/null +++ b/challenge-168/polettix/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +class PerrinSequence { + has @!state = [3, 0, 2]; + method get () { + @!state.push(@!state[0] + @!state[1]); + return @!state.shift; + } +} + +multi sub MAIN (1) { put 2 } + +multi sub MAIN (2) { put '2, 3' } + +multi sub MAIN (3) { put '2, 3, 5' } + +multi sub MAIN (Int:D $n is copy where * > 3 = 13) { + my $ps = PerrinSequence.new; + $ps.get for 1..7; + my @n-primes = gather while $n > 3 { + my $candidate = $ps.get; + next unless $candidate.is-prime; + take $candidate; + --$n; + } + [2, 3, 5, |@n-primes].join(', ').put; +} diff --git a/challenge-168/polettix/raku/ch-2.raku b/challenge-168/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..5de2d6efd4 --- /dev/null +++ b/challenge-168/polettix/raku/ch-2.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (Int:D $n where * > 1 = 10) { put home-prime($n) } + +sub home-prime ($n is copy) { + loop { + my $m = factors($n).join('').Int; + return $n if $n == $m; + $n = $m; + } +} + +sub factors (Int $remainder is copy) { + return 1 if $remainder <= 1; + state @primes = 2, 3, 5, -> $n is copy { + repeat { $n += 2 } until $n %% none @primes ... { $_ * $_ >= $n } + $n; + } ... *; + gather for @primes -> $factor { + if $factor * $factor > $remainder { + take $remainder if $remainder > 1; + last; + } + + # How many times can we divide by this prime? + while $remainder %% $factor { + take $factor; + last if ($remainder div= $factor) === 1; + } + } +} -- cgit