| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 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::Triangular; | ||||||||||
| 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 | # uncomment this to run the ### lines | ||||||||||
| 33 | # use Smart::Comments; | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | # use constant name => Math::NumSeq::__('Triangular Numbers'); | ||||||||||
| 37 | 2 | ▶ | use constant description => Math::NumSeq::__('The triangular numbers 0, 1, 3, 6, 10, 15, 21, 28, etc, i*(i+1)/2.'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 2 | ▶ | use constant characteristic_increasing => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 2 | ▶ | use constant characteristic_integer => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | ▶ | use constant default_i_start => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | ▶ | use constant values_min => 0; # at i=0 | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | |||||||||||
| 43 | #------------------------------------------------------------------------------ | ||||||||||
| 44 | # cf A062828 gcd(2n,triangular(n)) | ||||||||||
| 45 | # | ||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | use constant oeis_anum => 'A000217'; # starting from i=0 value=0 | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | #------------------------------------------------------------------------------ | ||||||||||
| 50 | |||||||||||
| 51 | 126 | ▶ | sub ith { | ||||||||
|
Subroutine
ith: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | 126 | my ($self, $i) = @_; | |||||||||
| 53 | 126 | return $i*($i+1)/2; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | # [1,2,3,4,5],[1,3,6,10,15] | ||||||||||
| 57 | # N = ((1/2*$d + 1/2)*$d) | ||||||||||
| 58 | # d = -1/2 + sqrt(2 * $n + 1/4) | ||||||||||
| 59 | # = (-1 + 2*sqrt(2 * $n + 1/4))/2 | ||||||||||
| 60 | # = (sqrt(4*2*$n + 1) - 1)/2 | ||||||||||
| 61 | # = (sqrt(8*$n + 1) - 1)/2 | ||||||||||
| 62 | 50 | ▶ | sub pred { | ||||||||
|
Subroutine
pred: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 63 | 50 | my ($self, $value) = @_; | |||||||||
| 64 | ### Triangular pred(): $value | ||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | if ($value < 0) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 50 | my $int = int($value); | |||||||||
| 68 | 50 | ▶ | if ($value != $int) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | |||||||||||
| 70 | 29 | $int *= 2; | |||||||||
| 71 | 29 | my $i = int((sqrt(4*$int + 1) - 1)/2); | |||||||||
| 72 | |||||||||||
| 73 | ### $int | ||||||||||
| 74 | ### $i | ||||||||||
| 75 | ### triangular: ($i+1)*$i/2 | ||||||||||
| 76 | |||||||||||
| 77 | 29 | return ($int == ($i+1)*$i); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 35 | ▶ | sub value_to_i { | ||||||||
|
Subroutine
value_to_i: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 81 | 35 | my ($self, $value) = @_; | |||||||||
| 82 | 35 | ▶ | if ($value >= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 31 | my $int = int($value); | |||||||||
| 84 | 31 | ▶ | if ($value == $int) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 19 | my $i = int((sqrt(8*$int + 1) - 1)/2); | |||||||||
| 86 | 19 | ▶ | if ($int == $self->ith($i)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 14 | return $i; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | 21 | return undef; | |||||||||
| 92 | } | ||||||||||
| 93 | 110 | ▶ | sub value_to_i_floor { | ||||||||
|
Subroutine
value_to_i_floor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 94 | 110 | my ($self, $value) = @_; | |||||||||
| 95 | 110 | ▶ | if ($value < 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 14 | return 0; | |||||||||
| 97 | } | ||||||||||
| 98 | 96 | return int((sqrt(8*int($value) + 1) - 1)/2); | |||||||||
| 99 | } | ||||||||||
| 100 | 2 | *value_to_i_estimate = \&value_to_i_floor; | |||||||||
| 101 | |||||||||||
| 102 | 48 | ▶ | sub value_to_i_ceil { | ||||||||
|
Subroutine
value_to_i_ceil: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 103 | 48 | my ($self, $value) = @_; | |||||||||
| 104 | ### value_to_i_ceil(): $value | ||||||||||
| 105 | 48 | ▶ | if ($value <= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 7 | return 0; | |||||||||
| 107 | } | ||||||||||
| 108 | 41 | my $i = $self->value_to_i_floor($value); | |||||||||
| 109 | 41 | ▶ | if ($self->ith($i) < $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 16 | return $i+1; | |||||||||
| 111 | } else { | ||||||||||
| 112 | 25 | return $i; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 2 | 1; | |||||||||
| 117 | __END__ | ||||||||||