[libmath-prime-util-perl] 28/54: Add xt ispower test and some more PE examples
Partha P. Mukherjee
ppm-guest at moszumanska.debian.org
Thu May 21 18:52:09 UTC 2015
This is an automated email from the git hooks/post-receive script.
ppm-guest pushed a commit to annotated tag v0.38
in repository libmath-prime-util-perl.
commit 759e933fe14a70dfab6023ca68aa01a2392a780a
Author: Dana Jacobsen <dana at acm.org>
Date: Thu Feb 13 12:26:24 2014 -0800
Add xt ispower test and some more PE examples
---
MANIFEST | 3 +++
examples/project_euler_131.pl | 18 ++++++++++++++++++
examples/project_euler_142.pl | 42 ++++++++++++++++++++++++++++++++++++++++++
xt/test-ispower.pl | 34 ++++++++++++++++++++++++++++++++++
4 files changed, 97 insertions(+)
diff --git a/MANIFEST b/MANIFEST
index 399f926..5121185 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -77,6 +77,8 @@ examples/project_euler_069.pl
examples/project_euler_070.pl
examples/project_euler_072.pl
examples/project_euler_095.pl
+examples/project_euler_131.pl
+examples/project_euler_142.pl
examples/project_euler_211.pl
examples/project_euler_214.pl
examples/project_euler_342.pl
@@ -141,4 +143,5 @@ xt/test-primes-script.pl
xt/test-primes-script2.pl
xt/test-factor-yafu.pl
xt/test-nextprime-yafu.pl
+xt/test-ispower.pl
.travis.yml
diff --git a/examples/project_euler_131.pl b/examples/project_euler_131.pl
new file mode 100644
index 0000000..05b3d5b
--- /dev/null
+++ b/examples/project_euler_131.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use Math::Prime::Util qw/is_prime/;
+
+my $limit = shift || 1000000;
+
+# Any prime p where n^3 + n^2*p = m^3 must be the difference of (i+1)^3 - i^3.
+# So we'll just walk them looking for primes.
+
+my $sum = 0;
+foreach my $i (1 .. 2650070) {
+ my $j = $i+1;
+ my $p = $j*$j*$j - $i*$i*$i;
+ last if $p > $limit;
+ $sum++ if is_prime($p);
+}
+print "$sum\n";
diff --git a/examples/project_euler_142.pl b/examples/project_euler_142.pl
new file mode 100644
index 0000000..4d0ab12
--- /dev/null
+++ b/examples/project_euler_142.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use Math::Prime::Util qw/:all/;
+
+# x+y = a^2 x = a^2 - y
+# x-y = b^2 a^2-y-y = b^2 2y = b^2-a^2 y = (b^2-a^2)/2
+# x+z = c^2 z = c^2 - z
+# x-z = d^2 c^2-z-z=d^2 2z = c^2-d^2 z = (c^2-d^2)/2
+# y+z = e^2
+# y-z = f^2 x = (e^2-f^2)/2
+
+# x+y = a^2 x-y = b^2 ===> 2x = a^2+b^2 x=(a^2+b^2)/2
+# x+z = c^2 x-z = d^2 ===> 2z = c^2-d^2 z=(c^2-d^2)/2
+# y+z = e^2 y-z = f^2 ===> 2y = e^2+f^2 y=(e^2+f^2)/2
+
+# a^2 = x+y = x+y+z-z = x+z + y-z = c^2 + f^2
+# e^2 = y+z = y+z+x-x = y+x -(x-z) = a^2 - d^2
+# b^2 = x-y = x-y+z-z = x+z -(y+z) = c^2 - e^2
+
+foreach my $a (4 .. 1000000) {
+ my $a2 = $a*$a;
+ foreach my $c (3 .. $a-1) {
+ my $c2 = $c*$c;
+ my $f2 = $a2 - $c2;
+ next unless $f2 >= 0 && is_power($f2,2);
+ foreach my $d (1 .. $c-1) {
+ next if ($d ^ $c) & 1; # c and d must have same parity
+ my $d2 = $d*$d;
+ my $e2 = $a2 - $d2;
+ my $b2 = $c2 - $e2;
+ next if $e2 <= 0 || $b2 <= 0;
+ #next if (($a2+$b2) & 1) || (($e2+$f2) & 1) || (($c2-$d2) & 1);
+ next unless is_power($e2,2) && is_power($b2,2);
+ my $x = ($a2+$b2) >> 1;
+ my $y = ($e2+$f2) >> 1;
+ my $z = ($c2-$d2) >> 1;
+ my $result = $x+$y+$z;
+ die "$result [$x $y $z]\n";
+ }
+ }
+}
diff --git a/xt/test-ispower.pl b/xt/test-ispower.pl
new file mode 100755
index 0000000..234b099
--- /dev/null
+++ b/xt/test-ispower.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+$| = 1; # fast pipes
+
+use Math::Prime::Util qw/is_prime is_power/;
+use Math::GMPz;
+
+foreach my $e (5 .. 37) {
+ next unless is_prime($e);
+ print "$e ";
+ for (3..1000000) {
+ my $n = Math::GMPz->new($_) ** $e;
+ last if $n > ~0;
+ die "$n\n" unless is_power($n);
+ foreach my $o (-10..10) {
+ my $m = $n+$o;
+ next if $m==$n;
+ die "$m\n" if is_power($m) && int(sqrt($m))**2 != $m && $m!=2197;
+ }
+ }
+}
+print "\n";
+
+my $int = 100000;
+foreach my $i (1 .. 80*$int) {
+ print "." unless $i % $int;
+ my @iroots = (0,0,map { int($i ** (1.0/$_) + 0.00001) ** $_ } 2 .. 12);
+ foreach my $e (2 .. 12) {
+ if (is_power($i,$e)) { die "$i $e" unless $iroots[$e] == $i; }
+ else { die "$i $e" unless $iroots[$e] != $i; }
+ }
+}
+print "\n";
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git
More information about the Pkg-perl-cvs-commits
mailing list