| 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::Odd; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | $VERSION = 75; | |||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Math::NumSeq; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 28 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # use constant name => Math::NumSeq::__('Odd Integers'); | ||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use constant description => Math::NumSeq::__('The odd integers 1, 3, 5, 7, 9, etc, 2i+1.'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use constant values_min => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use constant default_i_start => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use constant characteristic_increasing => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | use constant characteristic_integer => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | use constant oeis_anum => 'A005408'; # odd 1,3,5,... | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 7 | ▶ | sub rewind { | ||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 41 | 7 | $self->{'i'} = $self->i_start; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 22 | ▶ | sub seek_to_i { | ||||||||||||||||||||||||||
|
Subroutine
seek_to_i: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 22 | my ($self, $i) = @_; | |||||||||||||||||||||||||||
| 45 | 22 | $self->{'i'} = $i; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 15 | ▶ | sub seek_to_value { | ||||||||||||||||||||||||||
|
Subroutine
seek_to_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 15 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 49 | 15 | $self->seek_to_i($self->value_to_i_ceil($value)); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 57 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 57 | my ($self) = @_; | |||||||||||||||||||||||||||
| 53 | 57 | my $i = $self->{'i'}++; | |||||||||||||||||||||||||||
| 54 | 57 | return ($i, 2*$i+1); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 24 | ▶ | sub ith { | ||||||||||||||||||||||||||
|
Subroutine
ith: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 24 | my ($self, $i) = @_; | |||||||||||||||||||||||||||
| 58 | 24 | return 2*$i+1; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 32 | ▶ | sub pred { | ||||||||||||||||||||||||||
|
Subroutine
pred: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 32 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 62 | ### Odd pred(): $value | ||||||||||||||||||||||||||||
| 63 | 32 | ▶ | return ($value == int($value) | ||||||||||||||||||||||||||
|
Condition100%3/3
$value == int $value && $value % 2
MC/DC100%2/2
$value == int $value && $value % 2
$value == int $value
$value % 2
Truth table100%3/3
A && B
A= $value == int $valueB= $value % 2
| |||||||||||||||||||||||||||||
| 64 | && ($value % 2)); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 38 | ▶ | sub value_to_i { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 38 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 69 | 38 | my $int = int($value); | |||||||||||||||||||||||||||
| 70 | 38 | ▶ | if ($value == $int | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value == $int and $int % 2 == 1
MC/DC100%2/2
$value == $int and $int % 2 == 1
$value == $int
$int % 2 == 1
Truth table100%3/3
A and B
A= $value == $intB= $int % 2 == 1
| |||||||||||||||||||||||||||||
| 71 | && ($int % 2) == 1) { | ||||||||||||||||||||||||||||
| 72 | 15 | return ($int-1)/2; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 23 | return undef; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 66 | ▶ | sub value_to_i_floor { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i_floor: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 66 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 78 | 66 | ▶ | if (($value -= 1) < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 17 | my $i = int($value/2); | |||||||||||||||||||||||||||
| 80 | 17 | ▶ | if (2*$i > $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 14 | return $i-1; | |||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||
| 83 | 3 | return $i; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||
| 86 | 49 | return int($value/2); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 77 | ▶ | sub value_to_i_ceil { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i_ceil: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 77 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 91 | 77 | $value -= 1; | |||||||||||||||||||||||||||
| 92 | 77 | my $i = int($value/2); | |||||||||||||||||||||||||||
| 93 | 77 | ▶ | if (2*$i < $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 23 | return $i+1; | |||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 54 | return $i; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 16 | ▶ | sub value_to_i_estimate { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i_estimate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 16 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 101 | 16 | return int(($value-1)/2); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | 1; | |||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||