Nimber arithmetic: Difference between revisions
Content deleted Content added
→{{header|REXX}}: added the computer programming language REXX. |
|||
Line 1,332:
2150821508215082150821508 ⊕ 4268942689426894268942689 = 2722732241575131661744101
2150821508215082150821508 ⊗ 4268942689426894268942689 = 221974472829844568827862736061997038065</pre>
=={{header|REXX}}==
{{trans|FreeBASIC}}
This REXX version optimizes the '''nimber product''' by using the '''nimber sum''' for some of its calculations.
<lang rexx>/*REXX program performs nimber arithmetic (addition and multiplication); shows a table.*/
numeric digits 40 /*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.*/
if aa=='' | aa=="," then aa= 21508 /* " " " " " " */
if bb=='' | bb=="," then bb= 42689 /* " " " " " " */
w= max(4, length(siz) ) /*calculate the width of the table cols*/
_= '═' /*define the "dash" character for table*/
do am=0 for 2 /*perform sums, then perform multiplies*/
do j=0 to sz /*calculate & format a row of the table*/
if j==0 then if am then call top '║'center('(*)', w) /*show title of table. */
else call top '║'center('(+)', w) /* " " " " */
$= '║'center(j, w)"║" /*index for a table row.*/
do k=0 to sz /*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; say; say
end /*am*/
say 'nimber sum of ' comma(aa) " and " comma(bb) ' ───► ' comma( nsum(aa, bb))
say 'nimber product of ' comma(aa) " and " comma(bb) ' ───► ' comma(nprod(aa, bb))
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
hdr: $= ?"║"; do i=0 to sz; $= $ || right(i,w); end; say $ '║'; call sep; return
sep: $= '╠'copies(_, w)"╬"copies(copies(_, w), sz+1)_; say $'╣'; return
bot: $= '╚'copies(_, w)"╩"copies(copies(_, w), sz+1)_; say $'╝'; return
top: $= '╔'copies(_, w)"╦"copies(copies(_, w), sz+1)_; say $'╗'; arg ?; call hdr; return
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; return c2d(bitxor(d2c(x,digits()%8), d2c(y,digits()%8)))
shl: procedure; parse arg z,h; if h==0 then return z; return z * (2**h)
shr: procedure; parse arg z,h; if h==0 then return z; return z % (2**h)
/*──────────────────────────────────────────────────────────────────────────────────────*/
nprod: procedure; 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) ) /*optimized*/
if hpo2(y)<y then return nprod(y, x)
ands= c2d( bitand( d2c( lhpo2(x), digits()%8), d2c(lhpo2(y), digits()%8) ) )
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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ║
╠════╬═════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ 0 ║ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ║
║ 1 ║ 1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24 ║
║ 2 ║ 2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13 18 19 16 17 22 23 20 21 26 27 ║
║ 3 ║ 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 19 18 17 16 23 22 21 20 27 26 ║
║ 4 ║ 4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11 20 21 22 23 16 17 18 19 28 29 ║
║ 5 ║ 5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10 21 20 23 22 17 16 19 18 29 28 ║
║ 6 ║ 6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9 22 23 20 21 18 19 16 17 30 31 ║
║ 7 ║ 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23 22 21 20 19 18 17 16 31 30 ║
║ 8 ║ 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31 16 17 ║
║ 9 ║ 9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6 25 24 27 26 29 28 31 30 17 16 ║
║ 10 ║ 10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5 26 27 24 25 30 31 28 29 18 19 ║
║ 11 ║ 11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4 27 26 25 24 31 30 29 28 19 18 ║
║ 12 ║ 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 28 29 30 31 24 25 26 27 20 21 ║
║ 13 ║ 13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2 29 28 31 30 25 24 27 26 21 20 ║
║ 14 ║ 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 30 31 28 29 26 27 24 25 22 23 ║
║ 15 ║ 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24 23 22 ║
║ 16 ║ 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 1 2 3 4 5 6 7 8 9 ║
║ 17 ║ 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 1 0 3 2 5 4 7 6 9 8 ║
║ 18 ║ 18 19 16 17 22 23 20 21 26 27 24 25 30 31 28 29 2 3 0 1 6 7 4 5 10 11 ║
║ 19 ║ 19 18 17 16 23 22 21 20 27 26 25 24 31 30 29 28 3 2 1 0 7 6 5 4 11 10 ║
║ 20 ║ 20 21 22 23 16 17 18 19 28 29 30 31 24 25 26 27 4 5 6 7 0 1 2 3 12 13 ║
║ 21 ║ 21 20 23 22 17 16 19 18 29 28 31 30 25 24 27 26 5 4 7 6 1 0 3 2 13 12 ║
║ 22 ║ 22 23 20 21 18 19 16 17 30 31 28 29 26 27 24 25 6 7 4 5 2 3 0 1 14 15 ║
║ 23 ║ 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24 7 6 5 4 3 2 1 0 15 14 ║
║ 24 ║ 24 25 26 27 28 29 30 31 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 ║
║ 25 ║ 25 24 27 26 29 28 31 30 17 16 19 18 21 20 23 22 9 8 11 10 13 12 15 14 1 0 ║
╚════╩═════════════════════════════════════════════════════════════════════════════════════════════════════════╝
╔════╦═════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║(*) ║ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ║
╠════╬═════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ 0 ║ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ║
║ 1 ║ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ║
║ 2 ║ 0 2 3 1 8 10 11 9 12 14 15 13 4 6 7 5 32 34 35 33 40 42 43 41 44 46 ║
║ 3 ║ 0 3 1 2 12 15 13 14 4 7 5 6 8 11 9 10 48 51 49 50 60 63 61 62 52 55 ║
║ 4 ║ 0 4 8 12 6 2 14 10 11 15 3 7 13 9 5 1 64 68 72 76 70 66 78 74 75 79 ║
║ 5 ║ 0 5 10 15 2 7 8 13 3 6 9 12 1 4 11 14 80 85 90 95 82 87 88 93 83 86 ║
║ 6 ║ 0 6 11 13 14 8 5 3 7 1 12 10 9 15 2 4 96 102 107 109 110 104 101 99 103 97 ║
║ 7 ║ 0 7 9 14 10 13 3 4 15 8 6 1 5 2 12 11 112 119 121 126 122 125 115 116 127 120 ║
║ 8 ║ 0 8 12 4 11 3 7 15 13 5 1 9 6 14 10 2 128 136 140 132 139 131 135 143 141 133 ║
║ 9 ║ 0 9 14 7 15 6 1 8 5 12 11 2 10 3 4 13 144 153 158 151 159 150 145 152 149 156 ║
║ 10 ║ 0 10 15 5 3 9 12 6 1 11 14 4 2 8 13 7 160 170 175 165 163 169 172 166 161 171 ║
║ 11 ║ 0 11 13 6 7 12 10 1 9 2 4 15 14 5 3 8 176 187 189 182 183 188 186 177 185 178 ║
║ 12 ║ 0 12 4 8 13 1 9 5 6 10 2 14 11 7 15 3 192 204 196 200 205 193 201 197 198 202 ║
║ 13 ║ 0 13 6 11 9 4 15 2 14 3 8 5 7 10 1 12 208 221 214 219 217 212 223 210 222 211 ║
║ 14 ║ 0 14 7 9 5 11 2 12 10 4 13 3 15 1 8 6 224 238 231 233 229 235 226 236 234 228 ║
║ 15 ║ 0 15 5 10 1 14 4 11 2 13 7 8 3 12 6 9 240 255 245 250 241 254 244 251 242 253 ║
║ 16 ║ 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 24 8 56 40 88 72 120 104 152 136 ║
║ 17 ║ 0 17 34 51 68 85 102 119 136 153 170 187 204 221 238 255 8 25 42 59 76 93 110 127 128 145 ║
║ 18 ║ 0 18 35 49 72 90 107 121 140 158 175 189 196 214 231 245 56 42 27 9 112 98 83 65 180 166 ║
║ 19 ║ 0 19 33 50 76 95 109 126 132 151 165 182 200 219 233 250 40 59 9 26 100 119 69 86 172 191 ║
║ 20 ║ 0 20 40 60 70 82 110 122 139 159 163 183 205 217 229 241 88 76 112 100 30 10 54 34 211 199 ║
║ 21 ║ 0 21 42 63 66 87 104 125 131 150 169 188 193 212 235 254 72 93 98 119 10 31 32 53 203 222 ║
║ 22 ║ 0 22 43 61 78 88 101 115 135 145 172 186 201 223 226 244 120 110 83 69 54 32 29 11 255 233 ║
║ 23 ║ 0 23 41 62 74 93 99 116 143 152 166 177 197 210 236 251 104 127 65 86 34 53 11 28 231 240 ║
║ 24 ║ 0 24 44 52 75 83 103 127 141 149 161 185 198 222 234 242 152 128 180 172 211 203 255 231 21 13 ║
║ 25 ║ 0 25 46 55 79 86 97 120 133 156 171 178 202 211 228 253 136 145 166 191 199 222 233 240 13 20 ║
╚════╩═════════════════════════════════════════════════════════════════════════════════════════════════════════╝
nimber sum of 21,508 and 42,689 ───► 62,149
nimber product of 21,508 and 42,689 ───► 35,202
</pre>
=={{header|Rust}}==
|