aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-07-24 11:55:15 +0100
committerGitHub <noreply@github.com>2022-07-24 11:55:15 +0100
commit7a2fb0ae4f165044ee7305f4d6e4a4b8a09a43c2 (patch)
tree6b69aaa5a7d90ff573fa36c9a23358d9fff74bf5
parent480d500ac1c1c6cd84abc83851a2c5a369c93994 (diff)
parentff6ca46da65cd4ae744aa986c6e155b774a577c5 (diff)
downloadperlweeklychallenge-club-7a2fb0ae4f165044ee7305f4d6e4a4b8a09a43c2.tar.gz
perlweeklychallenge-club-7a2fb0ae4f165044ee7305f4d6e4a4b8a09a43c2.tar.bz2
perlweeklychallenge-club-7a2fb0ae4f165044ee7305f4d6e4a4b8a09a43c2.zip
Merge pull request #6489 from steve-g-lynn/branch-for-challenge-174
fast PDL::PP solution
-rwxr-xr-xchallenge-174/steve-g-lynn/perl/ch-1-pp.pl49
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-174/steve-g-lynn/perl/ch-1-pp.pl b/challenge-174/steve-g-lynn/perl/ch-1-pp.pl
new file mode 100755
index 0000000000..5ada3df755
--- /dev/null
+++ b/challenge-174/steve-g-lynn/perl/ch-1-pp.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+#real 0m0.480s
+#user 0m1.139s
+#sys 0m0.055s
+
+#-- PDL solution using inline PP
+
+use PDL;
+use PDL::NiceSlice;
+use PDL::AutoLoader;
+
+use Inline 'Pdlpp';
+
+
+#-- now find the sequence by checking ints up to 3_000_000
+
+my $pdl=sequence(long, 3_000_000);
+my $out= $pdl->get_poly;
+print $pdl->where($pdl==$out),"\n";
+
+#[0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798]
+
+__END__
+
+__Pdlpp__
+
+pp_addhdr('
+#include "math.h"
+');
+
+
+#-- the below code used ideas from Laurent Rosenfeld's C solution
+#-- https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-174/laurent-rosenfeld/c/ch-1.c
+
+pp_def('get_poly',
+ Pars => 'a(); [o]b()',
+ Code => q{
+ int mya = $a();
+ int mya_length = ( (mya <= 9) ? 1 : (floor(log10($a()))+1) );
+ $b()=0;
+ while (mya > 0) {
+ $b()+= pow(mya % 10, mya_length);
+ mya /= 10;
+ mya_length--;
+ }
+ }
+);
+