Math-NumSeq

 view release on metacpan or  search on metacpan

devel/lucky-numbers.pl  view on Meta::CPAN

#!/usr/bin/perl -w

# Copyright 2012, 2013 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.010;
use strict;
use warnings;
use POSIX;
use List::Util 'max','min';

# uncomment this to run the ### lines
# use Smart::Comments;





{
  # speed
  require Devel::TimeThis;
  my $iterations = 5000;
  # {
  #   my $self = { values    => [ 7 ],
  #                value     => 7,
  #                i         => 1,
  #                remaining => [ 4 ],
  #                inc       => 4,
  #              };
  #   my $t = Devel::TimeThis->new('subs');
  #   foreach (1 .. $iterations) {
  #     &next($self);
  #   }
  # }
  # {
  #   require Math::NumSeq::LuckyNumbersSlow;
  #   my $seq = Math::NumSeq::LuckyNumbersSlow->new;
  #   my $t = Devel::TimeThis->new('slow');
  #   foreach (1 .. $iterations) {
  #     $seq->next;
  #   }
  # }
  {
    require Math::NumSeq::LuckyNumbers;
    my $seq = Math::NumSeq::LuckyNumbers->new;
    if ($seq->can('ith')) {
      my $t = Devel::TimeThis->new('ith');
      foreach (1 .. $iterations) {
        $seq->ith($_);
      }
      ### $seq
    }
  }
  {
    require Math::NumSeq::LuckyNumbers;
    my $seq = Math::NumSeq::LuckyNumbers->new;
    my $t = Devel::TimeThis->new('seq');
    foreach (1 .. $iterations) {
      $seq->next;
    }
    ### $seq
  }
  {
    require Math::NumSeq::LuckyNumbersByStep;
    my $seq = Math::NumSeq::LuckyNumbersByStep->new;
    my $t = Devel::TimeThis->new('step');
    foreach (1 .. $iterations) {
      $seq->next;
    }
    ### $seq
  }
  {
    require '../backup/DanaLuckyNumbers.pm';
    my $seq = Math::NumSeq::DanaLuckyNumbers->new;
    my $t = Devel::TimeThis->new('array');
    foreach (1 .. $iterations) {
      $seq->next;
    }
    ### $seq
  }
  exit 0;
}

{
  require Math::NumSeq::LuckyNumbers;
  require Math::NumSeq::OEIS::File;
  my $seq = Math::NumSeq::LuckyNumbers->new;
  my $file = Math::NumSeq::OEIS::File->new (anum => 'A000959');
  for (;;) {
    my ($got_i, $got_value) = $seq->next;
    my ($file_i, $file_value) = $file->next
      or do {
        print "ok to $got_i\n";
        last;
      };
    if ($got_i != $file_i) {
      die; 
    }
    if ($got_value != $file_value) {
      die; 
    }
  }
  exit 0;
}


{
  my @want = (undef,1,3,7,9,13,15,21,25,31,33,37,43,49,51,63,67,69,73,75,
              79,87,93,99,105,111,115,127,129,133,135,141,151,159,163,
              169,171,189,193,195,201,205,211,219,223,231,235,237,241,
              259,261,267,273,283,285,289,297,303);

  # 1 2   3 4
  # 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,...
  # 1,3,  7,9,   13,15,   19,21,   25,27,   31,33,   37,39,...  3s
  # 1,3,  7,9,   13,15,      21,   25,27,   31,33,   37,   ...  7s exclude 19..
  # 1,3,  7,9,   13,15,      21,   25,      31,33,   37,   ...  9s exclude 27..

  require Math::NumSeq::LuckyNumbers;
  my $seq = Math::NumSeq::LuckyNumbers->new;
  # print $seq->ith(20),"\n";
  # my $seq = {
  #            };
  #foreach (1 .. 33) {
  foreach (1 .. $#want) {
    my ($i,$value) = $seq->next;
    my $bad = (defined $value && $value == $want[$i] ? '' : "  *** want=$want[$i]");
    print "i=$i value=$value$bad\n";

    # $value = $seq->ith($i);
    # $bad = (defined $value && $value == $want[$i] ? '' : '  ****');
    # print "  ith($i) value=$value$bad\n";
  }
  # use Smart::Comments;
  # no Smart::Comments;
}



( run in 0.580 second using v1.01-cache-2.11-cpan-71847e10f99 )