Nimber arithmetic: Difference between revisions
Content added Content deleted
(J: not convinced I understand the negative infinities in the math, but this seems to be the algorithm) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 27: | Line 27: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F hpo2(n) |
||
R n [&] (-n) |
R n [&] (-n) |
||
Line 70: | Line 70: | ||
V (a, b) = (21508, 42689) |
V (a, b) = (21508, 42689) |
||
print(a‘ + ’b‘ = ’nimsum(a, b)) |
print(a‘ + ’b‘ = ’nimsum(a, b)) |
||
print(a‘ * ’b‘ = ’nimprod(a, b))</ |
print(a‘ * ’b‘ = ’nimprod(a, b))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 118: | Line 118: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
Line 179: | Line 179: | ||
printf("%d * %d = %d\n", a, b, nimprod(a, b)); |
printf("%d * %d = %d\n", a, b, nimprod(a, b)); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 227: | Line 227: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="cpp">#include <cstdint> |
||
#include <functional> |
#include <functional> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 291: | Line 291: | ||
std::cout << a << " * " << b << " = " << nimprod(a, b) << '\n'; |
std::cout << a << " * " << b << " = " << nimprod(a, b) << '\n'; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 340: | Line 340: | ||
{{libheader| System.Math}} |
{{libheader| System.Math}} |
||
{{Trans|Go}} |
{{Trans|Go}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Nimber_arithmetic; |
program Nimber_arithmetic; |
||
Line 455: | Line 455: | ||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
{{works with|Factor|0.99 2020-07-03}} |
{{works with|Factor|0.99 2020-07-03}} |
||
< |
<syntaxhighlight lang="factor">USING: combinators formatting io kernel locals math sequences ; |
||
! highest power of 2 that divides a given number |
! highest power of 2 that divides a given number |
||
Line 504: | Line 504: | ||
33333 77777 |
33333 77777 |
||
[ 2dup nim-sum "%d + %d = %d\n" printf ] |
[ 2dup nim-sum "%d + %d = %d\n" printf ] |
||
[ 2dup nim-prod "%d * %d = %d\n" printf ] 2bi</ |
[ 2dup nim-prod "%d * %d = %d\n" printf ] 2bi</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 550: | Line 550: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function hpo2( n as uinteger ) as uinteger |
||
'highest power of 2 that divides a given number |
'highest power of 2 that divides a given number |
||
return n and -n |
return n and -n |
||
Line 624: | Line 624: | ||
print using "##### + ##### = ##########"; a; b; nimsum(a,b) |
print using "##### + ##### = ##########"; a; b; nimsum(a,b) |
||
print using "##### * ##### = ##########"; a; b; nimprod(a,b)</ |
print using "##### * ##### = ##########"; a; b; nimprod(a,b)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 671: | Line 671: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 743: | Line 743: | ||
fmt.Printf("%d + %d = %d\n", a, b, nimsum(a, b)) |
fmt.Printf("%d + %d = %d\n", a, b, nimsum(a, b)) |
||
fmt.Printf("%d * %d = %d\n", a, b, nimprod(a, b)) |
fmt.Printf("%d * %d = %d\n", a, b, nimprod(a, b)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 792: | Line 792: | ||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="j">nadd=: 22 b. NB. bitwise exclusive or on integers |
||
and=: 17 b. NB. bitwise exclusive or on integers |
and=: 17 b. NB. bitwise exclusive or on integers |
||
Line 812: | Line 812: | ||
end. |
end. |
||
end. |
end. |
||
}}M."0</ |
}}M."0</syntaxhighlight> |
||
Task examples: |
Task examples: |
||
< |
<syntaxhighlight lang="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│ |
│nadd│ _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15│ |
||
Line 869: | Line 869: | ||
80139 |
80139 |
||
12345 nmul 67890 |
12345 nmul 67890 |
||
809054384</ |
809054384</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="java">import java.util.function.IntBinaryOperator; |
||
public class Nimber { |
public class Nimber { |
||
Line 936: | Line 936: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 984: | Line 984: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="julia">""" highest power of 2 that divides a given number """ |
||
hpo2(n) = n & -n |
hpo2(n) = n & -n |
||
Line 1,024: | Line 1,024: | ||
println("nim-sum: $a ⊕ $b = $(nimsum(a, b))") |
println("nim-sum: $a ⊕ $b = $(nimsum(a, b))") |
||
println("nim-product: $a ⊗ $b = $(nimprod(a, b))") |
println("nim-product: $a ⊗ $b = $(nimprod(a, b))") |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
⊕ | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
⊕ | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
||
Line 1,070: | Line 1,070: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="nim">import bitops, strutils |
||
type Nimber = Natural |
type Nimber = Natural |
||
Line 1,121: | Line 1,121: | ||
const B = 42689 |
const B = 42689 |
||
echo "$1 ⊕ $2 = $3".format(A, B, ⊕(A, B)) |
echo "$1 ⊕ $2 = $3".format(A, B, ⊕(A, B)) |
||
echo "$1 ⊗ $2 = $3".format(A, B, ⊗(A, B))</ |
echo "$1 ⊗ $2 = $3".format(A, B, ⊗(A, B))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,167: | Line 1,167: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,216: | Line 1,216: | ||
say nim_prod(21508, 42689); |
say nim_prod(21508, 42689); |
||
say nim_sum(2150821508215082150821508, 4268942689426894268942689); |
say nim_sum(2150821508215082150821508, 4268942689426894268942689); |
||
say nim_prod(2150821508215082150821508, 4268942689426894268942689); # pretty slow</ |
say nim_prod(2150821508215082150821508, 4268942689426894268942689); # pretty slow</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> + │ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<pre> + │ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
||
Line 1,263: | Line 1,263: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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: | 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;">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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,388: | Line 1,388: | ||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
< |
<syntaxhighlight lang="prolog">% highest power of 2 that divides a given number |
||
hpo2(N, P):- |
hpo2(N, P):- |
||
P is N /\ -N. |
P is N /\ -N. |
||
Line 1,465: | Line 1,465: | ||
nimprod(A, B, Product), |
nimprod(A, B, Product), |
||
writef('%w + %w = %w\n', [A, B, Sum]), |
writef('%w + %w = %w\n', [A, B, Sum]), |
||
writef('%w * %w = %w\n', [A, B, Product]).</ |
writef('%w * %w = %w\n', [A, B, Product]).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,513: | Line 1,513: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="python"># Highest power of two that divides a given number. |
||
def hpo2(n): return n & (-n) |
def hpo2(n): return n & (-n) |
||
Line 1,558: | Line 1,558: | ||
a, b = 21508, 42689 |
a, b = 21508, 42689 |
||
print(f"{a} + {b} = {nimsum(a,b)}") |
print(f"{a} + {b} = {nimsum(a,b)}") |
||
print(f"{a} * {b} = {nimprod(a,b)}")</ |
print(f"{a} * {b} = {nimprod(a,b)}")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,605: | Line 1,605: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
{{trans|Julia}} (Mostly translated from Julia, although 'translated' doesn't do the process justice.) |
{{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 ) |
[ dup negate & ] is hpo2 ( n --> n ) |
||
Line 1,669: | Line 1,669: | ||
say " 10547 (+) 14447 = " 10547 14447 nim+ echo cr |
say " 10547 (+) 14447 = " 10547 14447 nim+ echo cr |
||
say " 10547 (*) 14447 = " 10547 14447 nim* echo cr |
say " 10547 (*) 14447 = " 10547 14447 nim* echo cr |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,722: | Line 1,722: | ||
Not limited by integer size. Doesn't rely on twos complement bitwise and. |
Not limited by integer size. Doesn't rely on twos complement bitwise and. |
||
<lang |
<syntaxhighlight lang="raku" line>sub infix:<⊕> (Int $x, Int $y) { $x +^ $y } |
||
sub infix:<⊗> (Int $x, Int $y) { |
sub infix:<⊗> (Int $x, Int $y) { |
||
Line 1,751: | Line 1,751: | ||
put "2150821508215082150821508 ⊕ 4268942689426894268942689 = ", 2150821508215082150821508 ⊕ 4268942689426894268942689; |
put "2150821508215082150821508 ⊕ 4268942689426894268942689 = ", 2150821508215082150821508 ⊕ 4268942689426894268942689; |
||
put "2150821508215082150821508 ⊗ 4268942689426894268942689 = ", 2150821508215082150821508 ⊗ 4268942689426894268942689;</ |
put "2150821508215082150821508 ⊗ 4268942689426894268942689 = ", 2150821508215082150821508 ⊗ 4268942689426894268942689;</syntaxhighlight> |
||
{{out}} |
{{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 |
<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: | Line 1,826: | ||
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 |
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. |
<br>two test numbers. |
||
< |
<syntaxhighlight 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. */ |
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.*/ |
||
Line 1,866: | Line 1,866: | ||
if hpo2(y)<y then return nprod(y, x) |
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 |
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) )</ |
h= hpo2(ands); return nprod( nprod( shr(x,h), shr(y,h) ), shl(3, h-1) )</syntaxhighlight> |
||
{{out|output|text= when using the input of: <tt> 25 </tt>}} |
{{out|output|text= when using the input of: <tt> 25 </tt>}} |
||
<pre> |
<pre> |
||
Line 1,939: | Line 1,939: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="rust">// highest power of 2 that divides a given number |
||
fn hpo2(n: u32) -> u32 { |
fn hpo2(n: u32) -> u32 { |
||
n & (0xFFFFFFFF - n + 1) |
n & (0xFFFFFFFF - n + 1) |
||
Line 2,009: | Line 2,009: | ||
println!("\n{} + {} = {}", a, b, nimsum(a, b)); |
println!("\n{} + {} = {}", a, b, nimsum(a, b)); |
||
println!("{} * {} = {}", a, b, nimprod(a, b)); |
println!("{} * {} = {}", a, b, nimprod(a, b)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,057: | Line 2,057: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|Rust}} |
{{trans|Rust}} |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
// highest power of 2 that divides a given number |
// highest power of 2 that divides a given number |
||
Line 2,127: | Line 2,127: | ||
let b: Int = 42689 |
let b: Int = 42689 |
||
print("\n\(a) + \(b) = \(nimSum(x: a, y: b))") |
print("\n\(a) + \(b) = \(nimSum(x: a, y: b))") |
||
print("\(a) * \(b) = \(nimProduct(x: a, y: b))")</ |
print("\(a) * \(b) = \(nimProduct(x: a, y: b))")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,176: | Line 2,176: | ||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
// Highest power of two that divides a given number. |
// Highest power of two that divides a given number. |
||
Line 2,227: | Line 2,227: | ||
var b = 42689 |
var b = 42689 |
||
System.print("%(a) + %(b) = %(nimsum.call(a, b))") |
System.print("%(a) + %(b) = %(nimsum.call(a, b))") |
||
System.print("%(a) * %(b) = %(nimprod.call(a, b))")</ |
System.print("%(a) * %(b) = %(nimprod.call(a, b))")</syntaxhighlight> |
||
{{out}} |
{{out}} |