Math-NumSeq
view release on metacpan or search on metacpan
devel/haferman-carpet.pl view on Meta::CPAN
#!/usr/bin/perl -w
# Copyright 2013, 2014, 2019, 2020 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 List::Util 'min', 'max';
use Math::PlanePath::Base::Digits
'round_down_pow',
'bit_split_lowtohigh',
'digit_split_lowtohigh',
'digit_join_lowtohigh';
use Math::NumSeq::HafermanCarpet;
# uncomment this to run the ### lines
# use Smart::Comments;
{
foreach my $n (0 .. 5) {
my $Seq_1s_init0 = Seq_1s_init0($n);
my $Seq_1s_init0_by_formula = Seq_1s_init0_by_formula($n);
my $Seq_1s_init1 = Seq_1s_init1($n);
my $Seq_1s_init1_by_formula = Seq_1s_init1_by_formula($n);
printf("%d %6d %6d%s %6d %6d%s %6d %6d\n",
$n,
$Seq_1s_init0,
$Seq_1s_init0_by_formula,
$Seq_1s_init0 == $Seq_1s_init0_by_formula ? '' : '******',
$Seq_1s_init1,
$Seq_1s_init1_by_formula,
$Seq_1s_init1 == $Seq_1s_init1_by_formula ? '' : '******',
Array_1s_init0($n),
Array_1s_init1($n),
);
}
exit 0;
# num black cells after n iterations (9^(k+1) - (-5)^(k+1))/14 = 1,4,61,424
# Array1s(k+1) = 9^(k+1) - 5*Array1s(k)
# Array1s(0) = 1
# Array1s(1) = 9^1 - 5*1 = 4
# Array1s(2) = 9^2 - 5*(9^1 - 5*1)
# = 5^0*9^2 - 5^1*9^1 + 5^2*9^0
# Array1s(3) = 5^0*9^3 - 5^1*9^2 + 5^2*9^1 - 5^3*9^0
# 5^0*9^0 = 1
# 5^1*9^0 - 5^2*9^1 = 20
sub Array_1s_init1 {
my ($n) = @_;
return (9**($n+1) - (-5)**($n+1)) / 14;
}
sub Hbox {
my ($n) = @_;
return 5**$n;
}
# Array_1s_init0 = (9^(k+1) - (-5)^(k+1)) / 14 - (-1)^k * 5^k
# = (9^(k+1) - (-5)^(k+1) - 14*(-5)^k) / 14
# = (9^(k+1) - (-5)^(k+1) - 14*(-5)^k) / 14
# = (9^(k+1) - -5*(-5)^k - 14*(-5)^k) / 14
# = (9^(k+1) + 5*(-5)^k - 14*(-5)^k) / 14
# = (9^(k+1) - 9*(-5)^k) / 14
sub Array_1s_init0 {
my ($n) = @_;
return (9**($n+1) - 9*(-5)**$n) / 14;
# +1 if n odd, -1 if n even
return Array_1s_init1($n) + (-1)**($n+1) * Hbox($n);
}
# Sones = (9^(n+1) - (-5)^(n+1)) / 14 - (1 + (-1)**$n)/2 * 5^n
# = (9^(n+1) - (-5)^(n+1) - 7*(1 + (-1)^n) * 5^n) / 14
# = (9^(n+1) - (-1)^(n+1)*5*5^n - 7*(1 + (-1)^n) * 5^n) / 14
# = (9^(n+1) + (- (-1)^(n+1)*5 - 7*(1 + (-1)^n)) * 5^n) / 14
# = (9^(n+1) + (- (-1)^(n+1)*5 - 7 - 7*(-1)^n) * 5^n) / 14
# = (9^(n+1) + (5*(-1)^n - 7 + 7*(-1)^n) * 5^n) / 14
# = (9^(n+1) + (-2*(-1)^n - 7) * 5^n) / 14
# = (9^(n+1) - (2*(-1)^n + 7) * 5^n) / 14 # 7+2=9 or 7-2=5
sub Seq_1s_init0_by_formula {
my ($n) = @_;
return (9**($n+1) - (2*(-1)**$n + 7) * 5**$n) / 14;
return Array_1s_init1($n) - (1 + (-1)**$n)/2 * Hbox($n);
}
# S1ones = (9^(n+1) - (2*(-1)^n + 7) * 5^n) / 14 + 5^n
# = (9^(n+1) - (2*(-1)^n + 7) * 5^n + 14 * 5^n) / 14
# = (9^(n+1) - (2*(-1)^n + 7 - 14) * 5^n) / 14
# = (9^(n+1) - (2*(-1)^n - 7) * 5^n) / 14
sub Seq_1s_init1_by_formula {
my ($n) = @_;
return (9**($n+1) - (2*(-1)**$n - 7) * 5**$n) / 14;
return Array_1s_init1($n) - (1 + (-1)**$n)/2 * Hbox($n);
}
( run in 0.755 second using v1.01-cache-2.11-cpan-71847e10f99 )