| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2017, 2019, 2020 Kevin Ryde | ||||||||||
| 2 | |||||||||||
| 3 | # This file is part of Math-NumSeq. | ||||||||||
| 4 | # | ||||||||||
| 5 | # Math-NumSeq is free software; you can redistribute it and/or modify | ||||||||||
| 6 | # it under the terms of the GNU General Public License as published by the | ||||||||||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||||||||||
| 8 | # version. | ||||||||||
| 9 | # | ||||||||||
| 10 | # Math-NumSeq is distributed in the hope that it will be useful, but | ||||||||||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||
| 13 | # for more details. | ||||||||||
| 14 | # | ||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||
| 16 | # with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>. | ||||||||||
| 17 | |||||||||||
| 18 | package Math::NumSeq::Pronic; | ||||||||||
| 19 | 2 | ▶ | use 5.004; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use POSIX 'ceil'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ▶ | use List::Util 'max'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | use vars '$VERSION','@ISA'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | $VERSION = 75; | |||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | use Math::NumSeq; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 2 | ▶ | use Math::NumSeq::Base::IterateIth; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 2 | @ISA = ('Math::NumSeq::Base::IterateIth', | |||||||||
| 30 | 'Math::NumSeq'); | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | # use constant name => Math::NumSeq::__('Pronic Numbers'); | ||||||||||
| 34 | 2 | ▶ | use constant description => Math::NumSeq::__('The pronic numbers 0, 2, 6, 12, 20, 30, etc, etc, i*(i+1). These are twice the triangular numbers, and half way between perfect squares.'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 2 | ▶ | use constant default_i_start => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 2 | ▶ | use constant characteristic_increasing => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 2 | ▶ | use constant characteristic_integer => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 2 | ▶ | use constant values_min => 0; # at i=0 | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 2 | ▶ | use constant oeis_anum => 'A002378'; # pronic, starting from 0 | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 8 | ▶ | sub rewind { | ||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 8 | my ($self) = @_; | |||||||||
| 43 | 8 | $self->{'i'} = $self->i_start; | |||||||||
| 44 | } | ||||||||||
| 45 | 23 | ▶ | sub seek_to_i { | ||||||||
|
Subroutine
seek_to_i: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 23 | my ($self, $i) = @_; | |||||||||
| 47 | 23 | $self->{'i'} = $i; | |||||||||
| 48 | } | ||||||||||
| 49 | 14 | ▶ | sub seek_to_value { | ||||||||
|
Subroutine
seek_to_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 14 | my ($self, $value) = @_; | |||||||||
| 51 | 14 | $self->seek_to_i($self->value_to_i_ceil($value)); | |||||||||
| 52 | } | ||||||||||
| 53 | 166 | ▶ | sub ith { | ||||||||
|
Subroutine
ith: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | 166 | my ($self, $i) = @_; | |||||||||
| 55 | 166 | return $i*($i+1); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | # [0,1,2,3,4],[0,2,6,12,20] | ||||||||||
| 59 | # N = (d^2 + d) | ||||||||||
| 60 | # = ($d**2 + $d) | ||||||||||
| 61 | # = (($d + 1)*$d) | ||||||||||
| 62 | # d = -1/2 + sqrt(1 * $n + 1/4) | ||||||||||
| 63 | # = (-1 + 2*sqrt(1 * $n + 1/4))/2 | ||||||||||
| 64 | # = (-1 + sqrt(4 * $n + 1))/2 | ||||||||||
| 65 | # = (sqrt(4*$n + 1) - 1)/2 | ||||||||||
| 66 | |||||||||||
| 67 | 92 | ▶ | sub pred { | ||||||||
|
Subroutine
pred: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 68 | 92 | my ($self, $value) = @_; | |||||||||
| 69 | 0 | ▶ | if ($value < 0) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 92 | my $i = $self->value_to_i_floor($value); | |||||||||
| 71 | 92 | return ($value == $i*($i+1)); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 38 | ▶ | sub value_to_i { | ||||||||
|
Subroutine
value_to_i: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 75 | 38 | my ($self, $value) = @_; | |||||||||
| 76 | 38 | ▶ | if ($value >= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 34 | my $int = int($value); | |||||||||
| 78 | 34 | ▶ | if ($value == $int) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 20 | my $i = int((sqrt(4*$int + 1) - 1)/2); | |||||||||
| 80 | 20 | ▶ | if ($int == $self->ith($i)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 14 | return $i; | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | 24 | return undef; | |||||||||
| 86 | } | ||||||||||
| 87 | 227 | ▶ | sub value_to_i_floor { | ||||||||
|
Subroutine
value_to_i_floor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 88 | 227 | my ($self, $value) = @_; | |||||||||
| 89 | 227 | ▶ | if ($value < 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 17 | return 0; | |||||||||
| 91 | } | ||||||||||
| 92 | 210 | return int((sqrt(4*int($value) + 1) - 1)/2); | |||||||||
| 93 | } | ||||||||||
| 94 | 64 | ▶ | sub value_to_i_ceil { | ||||||||
|
Subroutine
value_to_i_ceil: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 64 | my ($self, $value) = @_; | |||||||||
| 96 | 64 | my $i = $self->value_to_i_floor($value); | |||||||||
| 97 | 64 | ▶ | if ($self->ith($i) < $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 21 | return $i+1; | |||||||||
| 99 | } else { | ||||||||||
| 100 | 43 | return $i; | |||||||||
| 101 | } | ||||||||||
| 102 | } | ||||||||||
| 103 | 2 | *value_to_i_estimate = \&value_to_i_floor; | |||||||||
| 104 | |||||||||||
| 105 | 2 | 1; | |||||||||
| 106 | __END__ | ||||||||||