RosettaCodeData/Task/Babbage-problem/PL-I/babbage-problem.pli

25 lines
1.2 KiB
Plaintext

/* Babbage might have used a difference engine to compute squares. */
/* The algorithm used here uses only additions to form successive squares. */
/* Since there is no guarantee that the final square will not exceed a */
/* 32-bit integer word, modulus is formed to limit the magnitude of the */
/* squares, since we are really interested only in the last six digits of */
/* the square. */
Babbage_problem: procedure options (main); /* R. Vowels, 19 Dec. 2021 */
declare n fixed decimal (5);
declare (odd, sq) fixed binary (31);
odd = 3; sq = 4; /* the initial square is 4 */
do n = 3 to 99736;
odd = odd + 2;
sq = sq + odd; /* form the next square */
if sq >= 1000000 then sq = sq - 1000000; /* keep the remainder */
if sq = 269696 then leave;
end;
put ('The smallest number whose square ends in 269696 is ' || trim(n) );
put skip list ('The corresponding square is ' || trim (n*n) );
/* Even if the number had been 99736, n*n would not have overflowed */
/* because decimal arithmetic allows up to 15 decimal digits. */
end Babbage_problem;