1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#!/usr/bin/perl -w
# Copyright 2010, 2011 Kevin Ryde
# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
use 5.010;
use strict;
use warnings;
use POSIX 'fmod';
use Math::BigRat;
use Math::Prime::XS;
#use Smart::Comments;
use constant PHI => (1 + sqrt(5)) / 2;
# (3n-1)*n/2 pentagonal
# (3n+1)*n/2 second pentagonal
# http://www.research.att.com/~njas/sequences/A005449
# sum of n consecutive numbers >= n (n+1)+(n+2)+...+(n+n)
# triangular+square (n+1)*n/2 + n*n
# (3n+1)*n/2-2 = offset (3n+7)*n/2
# http://www.research.att.com/~njas/sequences/A140090
# sum n+1 to n+n-3 or some such
# (3n+1)*n/2
# (3n+1)*n/2 - 1
# (3n+1)*n/2 - 2
sub three {
my ($i) = @_;
return (3*$i+1)*$i/2 - 2;
}
sub is_perfect_square {
my ($n) = @_;
$n = sqrt($n);
return ($n == int($n));
}
{
my $prev_k = 0;
foreach my $k (0 .. 1000) {
my $sq = 24*$k+1;
if (is_perfect_square($sq)) {
printf "%4d %+4d %4d %4d\n", $k, $k-$prev_k, $k%24, $sq;
$prev_k = $k;
}
}
exit 0;
}
{
# i==0mod4 or 1mod4 always even
#
foreach my $k (0 .. 100) {
my $i = 4*$k + 2;
my $n = three($i);
my $factors = factorize($n);
printf "%4d %4d %s\n", $i,$n,$factors;
# unless ($factors =~ /\Q*/) {
# die;
# }
}
exit 0;
}
{
local $, = ',';
print map {three($_)} 0..20;
exit 0;
}
{
my $a = Math::BigRat->new('3/2');
my $b = Math::BigRat->new('1/2');
my $c = Math::BigRat->new('-2');
my $x = -$b;
my $sq = ($b*$b-4*$a*$c);
my $y = $sq;
$y->bsqrt;
print "$x $sq $y\n";
my $r1 = ($x + $y)/(2*$a);
my $r2 = ($x - $y)/(2*$a);
print "$r1 $r2\n";
exit 0;
}
{
foreach my $i (5 .. 500) {
my $n = three($i);
if (Math::Prime::XS::is_prime($n)) {
say "$i $n";
last;
}
}
exit 0;
}
sub factorize {
my ($n) = @_;
my @factors;
foreach my $f (2 .. int(sqrt($n)+1)) {
while (($n % $f) == 0) {
push @factors, $f;
### $n
$n /= $f;
}
}
if ($n != 1) {
push @factors, $n;
}
return join ('*',@factors);
}
exit 0;
|