Nimber arithmetic: Difference between revisions

m
(J: not convinced I understand the negative infinities in the math, but this seems to be the algorithm)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 27:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F hpo2(n)
R n [&] (-n)
 
Line 70:
V (a, b) = (21508, 42689)
print(a‘ + ’b‘ = ’nimsum(a, b))
print(a‘ * ’b‘ = ’nimprod(a, b))</langsyntaxhighlight>
 
{{out}}
Line 118:
=={{header|C}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdint.h>
 
Line 179:
printf("%d * %d = %d\n", a, b, nimprod(a, b));
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 227:
=={{header|C++}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <functional>
#include <iomanip>
Line 291:
std::cout << a << " * " << b << " = " << nimprod(a, b) << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 340:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Nimber_arithmetic;
 
Line 455:
readln;
 
end.</langsyntaxhighlight>
=={{header|Factor}}==
{{trans|FreeBASIC}}
{{works with|Factor|0.99 2020-07-03}}
<langsyntaxhighlight lang="factor">USING: combinators formatting io kernel locals math sequences ;
 
! highest power of 2 that divides a given number
Line 504:
33333 77777
[ 2dup nim-sum "%d + %d = %d\n" printf ]
[ 2dup nim-prod "%d * %d = %d\n" printf ] 2bi</langsyntaxhighlight>
{{out}}
<pre>
Line 550:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function hpo2( n as uinteger ) as uinteger
'highest power of 2 that divides a given number
return n and -n
Line 624:
 
print using "##### + ##### = ##########"; a; b; nimsum(a,b)
print using "##### * ##### = ##########"; a; b; nimprod(a,b)</langsyntaxhighlight>
{{out}}
<pre>
Line 671:
=={{header|Go}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 743:
fmt.Printf("%d + %d = %d\n", a, b, nimsum(a, b))
fmt.Printf("%d * %d = %d\n", a, b, nimprod(a, b))
}</langsyntaxhighlight>
 
{{out}}
Line 792:
{{trans|FreeBASIC}}
 
<langsyntaxhighlight Jlang="j">nadd=: 22 b. NB. bitwise exclusive or on integers
and=: 17 b. NB. bitwise exclusive or on integers
 
Line 812:
end.
end.
}}M."0</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> nadd table _4+i.20
┌────┬───────────────────────────────────────────────────────────────────────┐
│nadd│ _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15│
Line 869:
80139
12345 nmul 67890
809054384</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="java">import java.util.function.IntBinaryOperator;
 
public class Nimber {
Line 936:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 984:
=={{header|Julia}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="julia">""" highest power of 2 that divides a given number """
hpo2(n) = n & -n
 
Line 1,024:
println("nim-sum: $a ⊕ $b = $(nimsum(a, b))")
println("nim-product: $a ⊗ $b = $(nimprod(a, b))")
</langsyntaxhighlight>{{out}}
<pre>
⊕ | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Line 1,070:
=={{header|Nim}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight Nimlang="nim">import bitops, strutils
 
type Nimber = Natural
Line 1,121:
const B = 42689
echo "$1 ⊕ $2 = $3".format(A, B, ⊕(A, B))
echo "$1 ⊗ $2 = $3".format(A, B, ⊗(A, B))</langsyntaxhighlight>
 
{{out}}
Line 1,167:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,216:
say nim_prod(21508, 42689);
say nim_sum(2150821508215082150821508, 4268942689426894268942689);
say nim_prod(2150821508215082150821508, 4268942689426894268942689); # pretty slow</langsyntaxhighlight>
{{out}}
<pre> + │ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Line 1,263:
=={{header|Phix}}==
{{trans|FreeBASIC}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">hpo2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,320:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5d + %5d = %5d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nimsum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5d * %5d = %5d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nimprod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,388:
{{trans|FreeBASIC}}
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">% highest power of 2 that divides a given number
hpo2(N, P):-
P is N /\ -N.
Line 1,465:
nimprod(A, B, Product),
writef('%w + %w = %w\n', [A, B, Sum]),
writef('%w * %w = %w\n', [A, B, Product]).</langsyntaxhighlight>
 
{{out}}
Line 1,513:
=={{header|Python}}==
{{trans|Go}}
<langsyntaxhighlight lang="python"># Highest power of two that divides a given number.
def hpo2(n): return n & (-n)
 
Line 1,558:
a, b = 21508, 42689
print(f"{a} + {b} = {nimsum(a,b)}")
print(f"{a} * {b} = {nimprod(a,b)}")</langsyntaxhighlight>
{{out}}
<pre>
Line 1,605:
=={{header|Quackery}}==
{{trans|Julia}} (Mostly translated from Julia, although 'translated' doesn't do the process justice.)
<syntaxhighlight lang="quackery">
<lang Quackery>
[ dup negate & ] is hpo2 ( n --> n )
Line 1,669:
say " 10547 (+) 14447 = " 10547 14447 nim+ echo cr
say " 10547 (*) 14447 = " 10547 14447 nim* echo cr
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,722:
Not limited by integer size. Doesn't rely on twos complement bitwise and.
 
<syntaxhighlight lang="raku" perl6line>sub infix:<⊕> (Int $x, Int $y) { $x +^ $y }
 
sub infix:<⊗> (Int $x, Int $y) {
Line 1,751:
 
put "2150821508215082150821508 ⊕ 4268942689426894268942689 = ", 2150821508215082150821508 ⊕ 4268942689426894268942689;
put "2150821508215082150821508 ⊗ 4268942689426894268942689 = ", 2150821508215082150821508 ⊗ 4268942689426894268942689;</langsyntaxhighlight>
{{out}}
<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
Line 1,826:
The table size &nbsp; (for nimber sum and nimber products) &nbsp; may be specified on the <u>c</u>ommand <u>l</u>ine ('''CL''') &nbsp; as well as the
<br>two test numbers.
<langsyntaxhighlight 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.*/
Line 1,866:
if hpo2(y)<y then return nprod(y, x)
ands= c2d(bitand(d2c(lhpo2(x), d), d2c(lhpo2(y), d))); if ands==0 then return x*y
h= hpo2(ands); return nprod( nprod( shr(x,h), shr(y,h) ), shl(3, h-1) )</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 25 </tt>}}
<pre>
Line 1,939:
=={{header|Rust}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="rust">// highest power of 2 that divides a given number
fn hpo2(n: u32) -> u32 {
n & (0xFFFFFFFF - n + 1)
Line 2,009:
println!("\n{} + {} = {}", a, b, nimsum(a, b));
println!("{} * {} = {}", a, b, nimprod(a, b));
}</langsyntaxhighlight>
 
{{out}}
Line 2,057:
=={{header|Swift}}==
{{trans|Rust}}
<langsyntaxhighlight lang="swift">import Foundation
 
// highest power of 2 that divides a given number
Line 2,127:
let b: Int = 42689
print("\n\(a) + \(b) = \(nimSum(x: a, y: b))")
print("\(a) * \(b) = \(nimProduct(x: a, y: b))")</langsyntaxhighlight>
 
{{out}}
Line 2,176:
{{trans|FreeBASIC}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
// Highest power of two that divides a given number.
Line 2,227:
var b = 42689
System.print("%(a) + %(b) = %(nimsum.call(a, b))")
System.print("%(a) * %(b) = %(nimprod.call(a, b))")</langsyntaxhighlight>
 
{{out}}
Line 2,271:
21508 + 42689 = 62149
21508 * 42689 = 35202
</pre>
 
=={{header|XPL0}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
function HPo2(N); \Highest power of 2 that divides a given number
integer N;
return N and -N;
 
function LHPo2(N);
\Base 2 logarithm of the highest power of 2 dividing a given number
integer N, Q, M;
[Q:= 0; M:= HPo2(N);
while (M and 1) = 0 do
[M:= M >> 1;
Q:= Q+1;
];
return Q;
];
 
function NimSum(X, Y); \Nim-sum of two numbers
integer X, Y;
return X xor Y;
 
function NimProd(X, Y); \Nim-product of two numbers
integer X, Y, H, XP, YP, Comp;
[if X < 2 or Y < 2 then return X*Y;
H:= HPo2(X);
\Recursively break X into its powers of 2
if X > H then return NimProd(H, Y) xor NimProd(X xor H, Y);
\Recursively break Y into its powers of 2 by flipping its operands
if HPo2(Y) < Y then return NimProd(Y, X);
\Now both X and Y are powers of two
XP:= LHPo2(X); YP:= LHPo2(Y); Comp:= XP and YP;
if Comp = 0 then return X*Y; \there is no Fermat power in common
H:= HPo2(Comp);
\A Fermat number square is its sequimultiple
return NimProd(NimProd(X>>H, Y>>H), 3<<(H-1));
];
 
integer A, B;
[Format(3, 0);
Print(" + |");
for A:= 0 to 15 do RlOut(0, float(A));
Print("\n --- -------------------------------------------------\n");
for B:= 0 to 15 do
[RlOut(0, float(B));
Print(" |");
for A:= 0 to 15 do
RlOut(0, float(NimSum(A,B)));
Print("\n");
];
Print("\n * |");
for A:= 0 to 15 do RlOut(0, float(A));
Print("\n --- -------------------------------------------------\n");
for B:= 0 to 15 do
[RlOut(0, float(B));
Print(" |");
for A:= 0 to 15 do
RlOut(0, float(NimProd(A,B)));
Print("\n");
];
A:= 21508;
B:= 42689;
Print("\n%5d + %5d = %10d\n", A, B, NimSum(A,B));
Print("%5d + %5d = %10d\n", A, B, NimProd(A,B));
]</syntaxhighlight>
{{out}}
<pre>
+ | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
--- -------------------------------------------------
0 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 | 1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14
2 | 2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13
3 | 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12
4 | 4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11
5 | 5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10
6 | 6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9
7 | 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8
8 | 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7
9 | 9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6
10 | 10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5
11 | 11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4
12 | 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3
13 | 13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2
14 | 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1
15 | 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
 
* | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
--- -------------------------------------------------
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
2 | 0 2 3 1 8 10 11 9 12 14 15 13 4 6 7 5
3 | 0 3 1 2 12 15 13 14 4 7 5 6 8 11 9 10
4 | 0 4 8 12 6 2 14 10 11 15 3 7 13 9 5 1
5 | 0 5 10 15 2 7 8 13 3 6 9 12 1 4 11 14
6 | 0 6 11 13 14 8 5 3 7 1 12 10 9 15 2 4
7 | 0 7 9 14 10 13 3 4 15 8 6 1 5 2 12 11
8 | 0 8 12 4 11 3 7 15 13 5 1 9 6 14 10 2
9 | 0 9 14 7 15 6 1 8 5 12 11 2 10 3 4 13
10 | 0 10 15 5 3 9 12 6 1 11 14 4 2 8 13 7
11 | 0 11 13 6 7 12 10 1 9 2 4 15 14 5 3 8
12 | 0 12 4 8 13 1 9 5 6 10 2 14 11 7 15 3
13 | 0 13 6 11 9 4 15 2 14 3 8 5 7 10 1 12
14 | 0 14 7 9 5 11 2 12 10 4 13 3 15 1 8 6
15 | 0 15 5 10 1 14 4 11 2 13 7 8 3 12 6 9
 
21508 + 42689 = 62149
21508 + 42689 = 35202
</pre>
9,476

edits