Nimber arithmetic: Difference between revisions

Content added Content deleted
(Added translation in python)
m (→‎{{header|REXX}}: simplified some code.)
Line 1,531: Line 1,531:
<br>two test numbers.
<br>two test numbers.
<lang rexx>/*REXX program performs nimber arithmetic (addition and multiplication); shows a table.*/
<lang rexx>/*REXX program performs nimber arithmetic (addition and multiplication); shows a table.*/
numeric digits 40 /*use a big enough number of decimals. */
numeric digits 40; d= digits() % 8 /*use a big enough number of decimals. */
parse arg sz aa bb . /*obtain optional argument from the CL.*/
parse arg sz aa bb . /*obtain optional argument from the CL.*/
if sz=='' | sz=="," then sz= 15 /*Not specified? Then use the default.*/
if sz=='' | sz=="," then sz= 15 /*Not specified? Then use the default.*/
Line 1,539: Line 1,539:
sz1= sz + 1; w1= w-1 /*define the "dash" character for table*/
sz1= sz + 1; w1= w-1 /*define the "dash" character for table*/
do am=0 for 2 /*perform sums, then perform multiplies*/
do am=0 for 2 /*perform sums, then perform multiplies*/
do j=0 for sz1 /*calculate & format a row of the table*/
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. */
if j==0 then call top '║'center("("@.am')', w1) /*show title of table. */
$= '║'center(j, w1)"║" /*index for a table row.*/
$= '║'center(j, w1)"║" /*index for a table row.*/
do k=0 for sz1 /*build a row of table. */
do k=0 for sz1 /*build a row of table. */
if am then $= $ || right( nprod(j, k), w) /*append to a table row.*/
if am then $= $ || right( nprod(j, k), w) /*append to a table row.*/
else $= $ || right( nsum(j, k), w) /* " " " " " */
else $= $ || right( nsum(j, k), w) /* " " " " " */
end /*k*/
end /*k*/
say $ '║' /*show a row of a table.*/
say $ '║' /*show a row of a table.*/
end /*j*/
end /*j*/
call bot
call bot
end /*am*/
end /*am*/


say 'nimber sum of ' comma(aa) " and " comma(bb) ' ───► ' comma( nsum(aa, bb))
say 'nimber sum of ' comma(aa) " and " comma(bb) ' ───► ' comma( nsum(aa, bb))
Line 1,563: Line 1,563:
hpo2: procedure; parse arg z; return 2 ** (length( d2b(z) + 0) - 1)
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
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; parse arg x,y; d= digits() % 8; return c2d(bitxor(d2c(x,d), d2c(y,d)))
nsum: procedure expose d; parse arg x,y; return c2d( bitxor( d2c(x,d), d2c(y,d) ) )
shl: procedure; parse arg z,h; return z * (2**h)
shl: procedure; parse arg z,h; return z * (2**h)
shr: procedure; parse arg z,h; return z % (2**h)
shr: procedure; parse arg z,h; return z % (2**h)
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
nprod: procedure; parse arg x,y; if x<2 | y<2 then return x * y; h= hpo2(x)
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 x>h then return nsum( nprod(h, y), nprod( nsum(x, h), y) )
if hpo2(y)<y then return nprod(y, x)
if hpo2(y)<y then return nprod(y, x)
d= digits()%8; ands= c2d( bitand( d2c( lhpo2(x), d), d2c(lhpo2(y), d) ) )
ands= c2d(bitand(d2c(lhpo2(x), d), d2c(lhpo2(y), d))); if ands==0 then return x*y
if ands==0 then return x * y
h= hpo2(ands); return nprod( 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>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 25 </tt>}}
<pre>
<pre>