How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Perl programming language

Table of Contents

Problem Statement

These two sequences of positive integers are defined as:

The sequence

S ( n )

{\displaystyle S(n)}

is further defined as the sequence of positive integers not present in

R ( n )

{\displaystyle R(n)}

. Sequence

R

{\displaystyle R}

starts: Sequence

S

{\displaystyle S}

starts:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Perl programming language

Source code in the perl programming language

#!perl
use strict;
use warnings;

my @r = ( undef, 1 );
my @s = ( undef, 2 );
 
sub ffsr {
  my $n = shift;
  while( $#r < $n ) {
    push @r, $s[$#r]+$r[-1];
    push @s, grep { $s[-1]<$_ } $s[-1]+1..$r[-1]-1, $r[-1]+1;
  }
  return $n;
}
 
sub ffr { $r[ffsr shift] }
sub ffs { $s[ffsr shift] }
 
printf "  i: R(i) S(i)\n";
printf "==============\n";
printf "%3d:  %3d  %3d\n", $_, ffr($_), ffs($_) for 1..10;
printf "\nR(40)=%3d S(960)=%3d R(41)=%3d\n", ffr(40), ffs(960), ffr(41);

my %seen;
$seen{ffr($_)}++ for 1 .. 40;
$seen{ffs($_)}++ for 1 .. 960;
if( 1000 == keys %seen and grep $seen{$_}, 1 .. 1000 ) {
	print "All occured exactly once.\n";
} else {
	my @missed = grep !$seen{$_}, 1 .. 1000;
	my @dupped = sort { $a <=> $b} grep $seen{$_}>1, keys %seen;
	print "These were missed: @missed\n";
	print "These were duplicated: @dupped\n";
}


  

You may also check:How to resolve the algorithm Möbius function step by step in the Raku programming language
You may also check:How to resolve the algorithm Here document step by step in the AWK programming language
You may also check:How to resolve the algorithm First class environments step by step in the Perl programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the APL programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Nim programming language