How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the PL/I programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the PL/I 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 PL/I programming language
Source code in the pl/i programming language
ffr: procedure (n) returns (fixed binary(31));
declare n fixed binary (31);
declare v(2*n+1) bit(1);
declare (i, j) fixed binary (31);
declare (r, s) fixed binary (31);
v = '0'b;
v(1) = '1'b;
if n = 1 then return (1);
r = 1;
do i = 2 to n;
do j = 2 to 2*n;
if v(j) = '0'b then leave;
end;
v(j) = '1'b;
s = j;
r = r + s;
if r <= 2*n then v(r) = '1'b;
end;
return (r);
end ffr;
ffs: procedure (n) returns (fixed binary (31));
declare n fixed binary (31);
declare v(2*n+1) bit(1);
declare (i, j) fixed binary (31);
declare (r, s) fixed binary (31);
v = '0'b;
v(1) = '1'b;
if n = 1 then return (2);
r = 1;
do i = 1 to n;
do j = 2 to 2*n;
if v(j) = '0'b then leave;
end;
v(j) = '1'b;
s = j;
r = r + s;
if r <= 2*n then v(r) = '1'b;
end;
return (s);
end ffs;
Dcl t(1000) Bit(1) Init((1000)(1)'0'b);
put skip list ('Verification that the first 40 FFR numbers and the first');
put skip list ('960 FFS numbers result in the integers 1 to 1000 only.');
do i = 1 to 40;
j = ffr(i);
if t(j) then put skip list ('error, duplicate value at ' || i);
else t(j) = '1'b;
end;
do i = 1 to 960;
j = ffs(i);
if t(j) then put skip list ('error, duplicate value at ' || i);
else t(j) = '1'b;
end;
if all(t = '1'b) then put skip list ('passed test');
You may also check:How to resolve the algorithm Permutations step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Jsish programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Arrays step by step in the Vim Script programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the AutoHotkey programming language