aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2022-06-12 05:42:04 +0800
committerCY Fung <fungcheokyin@gmail.com>2022-06-12 05:42:04 +0800
commiteb043eb2670bd851f4f237961dc12036927fc570 (patch)
treee114aad858aa44122df4c4fdafce8793fbadcd0a
parentca71189e5959ea925a05c5b4412a64e5d10efe96 (diff)
downloadperlweeklychallenge-club-eb043eb2670bd851f4f237961dc12036927fc570.tar.gz
perlweeklychallenge-club-eb043eb2670bd851f4f237961dc12036927fc570.tar.bz2
perlweeklychallenge-club-eb043eb2670bd851f4f237961dc12036927fc570.zip
failed attempt: is_prime(BigInt) does not work
-rw-r--r--challenge-168/cheok-yin-fung/perl/ch-2.pl27
1 files changed, 19 insertions, 8 deletions
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
+