Jump to content

Steady squares: Difference between revisions

Line 1,458:
 
=={{header|PROMAL}}==
As pointed out bywith the Tiny BASIC sample, 16-bit languages must avoid overflow when solving this task. Uses long multiplication modulo the appropriate power of ten and the fact that the final digit must be 1, 5 or 6 (see the Discussion page).
<syntaxhighlight lang="promal">
PROGRAM steadySquares
INCLUDE LIBRARY
 
WORD p ; the number to square, with the final digit replaced by 0
WORD n ; the number to square
WORD d10 ; 10^the number of digits in p, n
WORD s ; the square of n modulo d10
WORD f ; loop counter to choose 1, 5 or 6 as the final digit of n
WORD kfront ; the ;first ntwo modulodigits 1000of n
WORD back ; the last two digits of n
 
BEGIN
d10 = 10
Line 1,485 ⟶ 1,487:
IF n <= 255
s = ( n * n )
ELSE IF d10 = 1000
; three digits that would overflow
s = ( n * ( ( n ) % 10 ) % 1000 )
s = s + ( n * ( ( n / 10 ) % 10 ) % 100 ) * 10
s = s + ( n * ( ( n / 100 ) % 10 ) % 10 ) * 100
ELSE
;front four= digitsn / 100
kback = n % 1000100
; contribution to the first digit of the four digits of n^2 % 10000
s = ( back * back ) + ( n200 * ( ( n front * back ) % 10100 ) % 1000 )
; from the first digit times the last
s = ( ( ( n / 1000 ) * ( n % 10 ) ) % 10 ) * 1000
; remaining digits
k = n % 1000
s = s + ( k * ( ( n ) % 10 ) % 10000 )
s = s + ( k * ( ( n / 10 ) % 10 ) % 1000 ) * 10
s = s + ( k * ( ( n / 100 ) % 10 ) % 100 ) * 100
s = s + ( k * ( ( n / 1000 ) % 10 ) % 10 ) * 1000
s = s % d10
IF s = n
3,037

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.