Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program Specifications You project will do the following: l. Prompt for two inte

ID: 3799124 • Letter: P

Question

Program Specifications You project will do the following: l. Prompt for two integers. T the integers that will be checked. 2. After the program runs, you will report the following statistics for the numbers examined in the given range: a. The number of "natural" palindromes (numbers in the range that are already palindromes, such a 11, 121, 3553) b. The number of non-Lychrel numbers (numbers which eventually yield a t palindrome using the 196 algorithm) c. The number of Lychrel numbers encountered. Assume a maximum of 60 iterations to indicate a Lychrel number d. Because numbers are rare, report each Lychrel number as it occurs

Explanation / Answer

/* program for palindrome and integers etc*/

INT max additions = 500;
[ 512 ]CHAR number;
FOR c TO UPB number DO number[ c ] := "0" OD;
INT digits := 0;
PROC set number = ( INT value )VOID:
BEGIN
digits := 0;
INT v := ABS value;
WHILE digits +:= 1;
number[ digits ] := REPR ( ABS "0" + v MOD 10 );
v OVERAB 10;
v > 0
DO SKIP OD
END
PROC add reverse = VOID:
BEGIN
[ digits + 1 ]CHAR result;
INT carry := 0;
INT r pos := digits;
FOR d pos TO digits DO
INT sum = ( ( ABS number[ d pos ] + ABS number[ r pos ] + carry )
- ( 2 * ABS "0" )
);
IF sum < 10 THEN
result[ d pos ] := REPR( sum + ABS "0" );
carry := 0
ELSE
result[ d pos ] := REPR ( ( sum - 10 ) + ABS "0" );
carry := 1
FI;
r pos -:= 1
OD;
IF carry /= 0 THE
digits +:= 1;
result[ digits ] := REPR ( ABS "0" + carry )
FI;
number[ : digits ] := result[ : digits ]
END
PROC is palindromic = BOOL:
BEGIN
BOOL result := TRUE;
INT d pos := 1;
INT r pos := digits;
WHILE IF d pos >= r pos
THEN FALSE
ELSE result := ( number[ d pos ] = number[ r pos ] )
FI
DO
d pos +:= 1;
r pos -:= 1
OD;
result
END ;
REF AARRAY related := INIT HEAP AARRAY;
PROC add related numbers = ( REF AARRAY path )VOID:
BEGIN
REF AAELEMENT r := FIRST path;
WHILE r ISNT nil element DO
related // key OF r := "Y";
r := NEXT path
OD
END # add related numbers # ;
INT not lychrel = 0;
INT lychrel seed = 1;
INT lychrel related = 2;
INT untested = 3;
INT max number = 10 000;
[ max number ]INT lychrel;
FOR n TO UPB lychrel DO lychrel[ n ] := untested OD;
[ UPB lychrel ]BOOL palindromic;
FOR n TO UPB palindromic DO palindromic[ n ] := FALSE OD;
INT seed count := 0;
INT related count := 0;
INT palindrome count := 0;
set number( n );
palindromic[ n ] := is palindromic;
add reverse;
REF AARRAY path := INIT HEAP AARRAY;
BOOL continue searching := TRUE;
TO max additions WHILE continue searching :=
IF related CONTAINSKEY number[ : digits ] THE
lychrel[ n ] := lychrel related;
add related numbers( path );
related count +:= 1;
FALSE
ELIF is palindromic THEN
lychrel[ n ] := not lychrel;
FALSE
ELS
path // number[ : digits ] := "Y";
add reverse;
TRUE
FI
DO SKIP OD;
IF continue searching THE
add related numbers( path );
lychrel[ n ] := lychrel seed;
seed count +:= 1
FI;
IF palindromic[ n ] AND ( lychrel[ n ] = lychrel seed OR lychrel[ n ] = lychrel related ) THEN
palindrome count +:= 1
FI
OD;
print( ( "There are "
, whole( seed count, 0 ), " seed Lychrel numbers and "
, whole( related count, 0 ), " related Lychrel numbers up to "
, whole( UPB lychrel, 0 )
, newline
)
);
print( ( "Seed Lychrel numbers up to ", whole( UPB lychrel, 0 ), ":" ) );
FOR n TO UPB lychrel DO IF lychrel[ n ] = lychrel seed THEN print( ( " ", whole( n, 0 ) ) ) FI OD;
print( ( newline ) );
print( ( "There are "
, whole( palindrome count, 0 )
, " palindromic Lychrel numbers up to "
, whole( UPB lychrel, 0 )
, ":"
)
);
FOR n TO UPB lychrel DO
IF ( lychrel[ n ] = lychrel seed OR lychrel[ n ] = lychrel related ) AND palindromic[ n ] THEN print( ( " ", whole( n, 0 ) ) ) FI
OD;
print( ( newline ) )

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote