Anonymous user
Nimber arithmetic: Difference between revisions
m
→{{header|REXX}}: simplified some code, optimized some functions, used narrower columns for the index and 1st column, added wording to the REXX section header.
(→{{header|REXX}}: added the computer programming language REXX.) |
m (→{{header|REXX}}: simplified some code, optimized some functions, used narrower columns for the index and 1st column, added wording to the REXX section header.) |
||
Line 1,338:
This REXX version optimizes the '''nimber product''' by using the '''nimber sum''' for some of its calculations.
The table size (for nimber sum and nimber products) may be specified on the <u>c</u>ommand <u>l</u>ine ('''CL''') as well as the
<br>two test numbers.
<lang rexx>/*REXX program performs nimber arithmetic (addition and multiplication); shows a table.*/
numeric digits 40 /*use a big enough number of decimals. */
Line 1,344 ⟶ 1,347:
if aa=='' | aa=="," then aa= 21508 /* " " " " " " */
if bb=='' | bb=="," then bb= 42689 /* " " " " " " */
w= max(4,
do j=0
if j==0
$= '║'center(j, w1)"║"
say $ '║'
call bot
end
say 'nimber sum of ' comma(aa) " and " comma(bb) ' ───► ' comma( nsum(aa, bb))
Line 1,364 ⟶ 1,366:
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
hdr: $= ?
comma: parse arg ?; do jc=length(?)-3 to 1 by -3; ?= insert(',', ?, jc); end; return ?
d2b: procedure; parse arg z; return right( x2b( d2x(z) ), digits(), 0)
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; parse arg x,y; d= digits() % 8; return c2d(bitxor(d2c(x,
shl: procedure; parse arg z,h;
shr: procedure; parse arg z,h;
/*──────────────────────────────────────────────────────────────────────────────────────*/
nprod: procedure; parse arg x,y;
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),
if ands==0 then return x * y
h= hpo2(ands); return nprod( nprod( shr(x,h), shr(y,h) ), shl(3, h-1) )</lang>
{{out|output|text= when using the input of: <tt> 25 </tt>}}
<pre>
╔═══╦═════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║(+)
╠═══╬═════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ 0
║ 1
║ 2
║ 3
║ 4
║ 5
║ 6
║ 7
║ 8
║ 9
╚═══╩═════════════════════════════════════════════════════════════════════════════════════════════════════════╝
╔═══╦═════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║(*)
╠═══╬═════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ 0
║ 1
║ 2
║ 3
║ 4
║ 5
║ 6
║ 7
║ 8
║ 9
╚═══╩═════════════════════════════════════════════════════════════════════════════════════════════════════════╝
|