Jump to content

Nimber arithmetic: Difference between revisions

m
→‎{{header|REXX}}: simplified some code.
(Added translation in python)
m (→‎{{header|REXX}}: simplified some code.)
Line 1,531:
<br>two test numbers.
<lang rexx>/*REXX program performs nimber arithmetic (addition and multiplication); shows a table.*/
numeric digits 40; d= digits() % 8 /*use a big enough number of decimals. */
parse arg sz aa bb . /*obtain optional argument from the CL.*/
if sz=='' | sz=="," then sz= 15 /*Not specified? Then use the default.*/
Line 1,539:
sz1= sz + 1; w1= w-1 /*define the "dash" character for table*/
do am=0 for 2 /*perform sums, then perform multiplies*/
do j=0 for sz1 /*calculate & format a row of the table*/
if j==0 then call top '║'center("("@.am')', w1) /*show title of table. */
$= '║'center(j, w1)"║" /*index for a table row.*/
do k=0 for sz1 /*build a row of table. */
if am then $= $ || right( nprod(j, k), w) /*append to a table row.*/
else $= $ || right( nsum(j, k), w) /* " " " " " */
end /*k*/
say $ '║' /*show a row of a table.*/
end /*j*/
call bot
end /*am*/
 
say 'nimber sum of ' comma(aa) " and " comma(bb) ' ───► ' comma( nsum(aa, bb))
Line 1,563:
hpo2: procedure; parse arg z; return 2 ** (length( d2b(z) + 0) - 1)
lhpo2: procedure; arg z; m=hpo2(z); q=0; do while m//2==0; m= m%2; q= q+1; end; return q
nsum: procedure expose d; parse arg x,y; d= digits() % 8; return c2d( bitxor( d2c(x,d), d2c(y,d) ) )
shl: procedure; parse arg z,h; return z * (2**h)
shr: procedure; parse arg z,h; return z % (2**h)
/*──────────────────────────────────────────────────────────────────────────────────────*/
nprod: procedure expose d; parse arg x,y; if x<2 | y<2 then return x * y; h= hpo2(x)
if x>h then return nsum( nprod(h, y), nprod( nsum(x, h), y) )
if hpo2(y)<y then return nprod(y, x)
d= digits()%8; ands= c2d( bitand( d2c( lhpo2(x), d), d2c(lhpo2(y), d))); )if ands==0 ) then return x*y
ifh= hpo2(ands==0); then return xnprod( *nprod( shr(x,h), shr(y,h) ), shl(3, h-1) )</lang>
h= hpo2(ands); return nprod( nprod( shr(x,h), shr(y,h) ), shl(3, h-1) )</lang>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 25 </tt>}}
<pre>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.