#!/usr/bin/perl -w
# Copyright 2012 Kevin Ryde
# This file is part of Math-NumSeq.
#
# Math-NumSeq 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-NumSeq 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-NumSeq. If not, see <http://www.gnu.org/licenses/>.
use 5.004;
use strict;
use Test;
plan tests => 5;
use lib 't','xt';
use MyTestHelpers;
MyTestHelpers::nowarnings();
use MyOEIS;
use Math::NumSeq::GolombSequence;
# uncomment this to run the ### lines
#use Smart::Comments '###';
sub numeq_array {
my ($a1, $a2) = @_;
if (! ref $a1 || ! ref $a2) {
return 0;
}
my $i = 0;
while ($i < @$a1 && $i < @$a2) {
if ($a1->[$i] ne $a2->[$i]) {
return 0;
}
$i++;
}
return (@$a1 == @$a2);
}
#------------------------------------------------------------------------------
# A104236 n*golomb(n)
{
my $anum = 'A104236';
my ($bvalues, $lo, $filename) = MyOEIS::read_values($anum);
my @got;
if ($bvalues) {
MyTestHelpers::diag ("$anum has ",scalar(@$bvalues)," values");
my $seq = Math::NumSeq::GolombSequence->new;
while (@got < @$bvalues) {
my ($i, $value) = $seq->next;
push @got, $i * $value;
}
if (! numeq_array(\@got, $bvalues)) {
MyTestHelpers::diag ("bvalues: ",join(',',@{$bvalues}[0..10]));
MyTestHelpers::diag ("got: ",join(',',@got[0..10]));
}
} else {
MyTestHelpers::diag ("$anum not available");
}
skip (! $bvalues,
numeq_array(\@got, $bvalues),
1, "$anum -- n*golomb(n)");
}
#------------------------------------------------------------------------------
# A143125 n*golomb(n) cumulative
{
my $anum = 'A143125';
my ($bvalues, $lo, $filename) = MyOEIS::read_values($anum);
my @got;
if ($bvalues) {
MyTestHelpers::diag ("$anum has ",scalar(@$bvalues)," values");
my $seq = Math::NumSeq::GolombSequence->new;
my $cumulative = 0;
while (@got < @$bvalues) {
my ($i, $value) = $seq->next;
$cumulative += $i * $value;
push @got, $cumulative;
}
if (! numeq_array(\@got, $bvalues)) {
MyTestHelpers::diag ("bvalues: ",join(',',@{$bvalues}[0..10]));
MyTestHelpers::diag ("got: ",join(',',@got[0..10]));
}
} else {
MyTestHelpers::diag ("$anum not available");
}
skip (! $bvalues,
numeq_array(\@got, $bvalues),
1, "$anum -- n*golomb(n) cumulative");
}
#------------------------------------------------------------------------------
# A088517 first differences
{
my $anum = 'A088517';
my ($bvalues, $lo, $filename) = MyOEIS::read_values($anum);
my @got;
if ($bvalues) {
MyTestHelpers::diag ("$anum has ",scalar(@$bvalues)," values");
my $seq = Math::NumSeq::GolombSequence->new;
my (undef, $prev) = $seq->next;
while (@got < @$bvalues) {
my ($i, $value) = $seq->next;
push @got, $value - $prev;
$prev = $value;
}
if (! numeq_array(\@got, $bvalues)) {
MyTestHelpers::diag ("bvalues: ",join(',',@{$bvalues}[0..10]));
MyTestHelpers::diag ("got: ",join(',',@got[0..10]));
}
} else {
MyTestHelpers::diag ("$anum not available");
}
skip (! $bvalues,
numeq_array(\@got, $bvalues),
1, "$anum -- differences");
}
#------------------------------------------------------------------------------
# A001463 partial sums
{
my $anum = 'A001463';
my ($bvalues, $lo, $filename) = MyOEIS::read_values($anum);
my @got;
if ($bvalues) {
MyTestHelpers::diag ("$anum has ",scalar(@$bvalues)," values");
my $seq = Math::NumSeq::GolombSequence->new;
my $cumulative = 0;
while (@got < @$bvalues) {
my ($i, $value) = $seq->next;
$cumulative += $value;
push @got, $cumulative;
}
if (! numeq_array(\@got, $bvalues)) {
MyTestHelpers::diag ("bvalues: ",join(',',@{$bvalues}[0..10]));
MyTestHelpers::diag ("got: ",join(',',@got[0..10]));
}
} else {
MyTestHelpers::diag ("$anum not available");
}
skip (! $bvalues,
numeq_array(\@got, $bvalues),
1, "$anum -- cumulative");
}
#------------------------------------------------------------------------------
# A095114 partial sums + 1
{
my $anum = 'A095114';
my ($bvalues, $lo, $filename) = MyOEIS::read_values($anum);
my @got;
if ($bvalues) {
MyTestHelpers::diag ("$anum has ",scalar(@$bvalues)," values");
push @got, 1;
my $seq = Math::NumSeq::GolombSequence->new;
my $cumulative = 0;
while (@got < @$bvalues) {
my ($i, $value) = $seq->next;
$cumulative += $value;
push @got, $cumulative + 1;
}
if (! numeq_array(\@got, $bvalues)) {
MyTestHelpers::diag ("bvalues: ",join(',',@{$bvalues}[0..10]));
MyTestHelpers::diag ("got: ",join(',',@got[0..10]));
}
} else {
MyTestHelpers::diag ("$anum not available");
}
skip (! $bvalues,
numeq_array(\@got, $bvalues),
1, "$anum -- cumulative");
}
#------------------------------------------------------------------------------
exit 0;