aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Number
diff options
context:
space:
mode:
authorMarvin Borner2024-03-25 16:21:54 +0100
committerMarvin Borner2024-03-25 16:21:54 +0100
commitf8398804d351667a7b887b89f6f70c7d5c407d22 (patch)
treed33df8e6bbe60640beb9e99c778eb27b1aa77995 /std/Number
parent7bfc78b122ad9cbb65eed16f7de4d4021547c2a8 (diff)
More samples and definitions
Diffstat (limited to 'std/Number')
-rw-r--r--std/Number/Bruijn.bruijn13
-rw-r--r--std/Number/Ternary.bruijn22
-rw-r--r--std/Number/Unary.bruijn20
3 files changed, 42 insertions, 13 deletions
diff --git a/std/Number/Bruijn.bruijn b/std/Number/Bruijn.bruijn
index 585d3bc..e4d0b36 100644
--- a/std/Number/Bruijn.bruijn
+++ b/std/Number/Bruijn.bruijn
@@ -5,7 +5,7 @@
# very sad indeed
# increments de Bruijn numeral
-inc [[[2 1]]]
+inc [[[2 1]]] ⧗ Bruijn → Bruijn
++‣ inc
@@ -13,9 +13,18 @@ inc [[[2 1]]]
:test (++(+5d)) ((+6d))
# decrements de Bruijn numeral
-dec [[1 0 0]]
+dec [[1 0 0]] ⧗ Bruijn → Bruijn
--‣ dec
:test (--(+1d)) ((+0d))
:test (--(+5d)) ((+4d))
+
+# multiplies de Bruijn numeral with unary number
+mul [[1 0]] ⧗ Unary → Bruijn → Bruijn
+
+…⋅… mul
+
+:test ((+5u) ⋅ (+5d)) ((+25d))
+:test ((+0u) ⋅ (+5d)) ((+0d))
+:test ((+5u) ⋅ (+0d)) ((+0d))
diff --git a/std/Number/Ternary.bruijn b/std/Number/Ternary.bruijn
index 3695740..4eaaf5a 100644
--- a/std/Number/Ternary.bruijn
+++ b/std/Number/Ternary.bruijn
@@ -456,16 +456,16 @@ div² [z [[[[rec]]]] (+0) 0 0] ⧗ Number → Number
:test (/³*(-6) =? (-2)) (true)
:test (/³*(+5) =? (+1)) (true)
-# ceiled integer log3 by counting number of trits
+# ceiled integer log₃ by counting number of trits
# also counts leading 0s
-log3* [0 (+0) inc inc inc] ⧗ Number → Number
+log₃* [0 (+0) inc inc inc] ⧗ Number → Number
-# ceiled integer log3 by counting number of trits
-log3 log3* ∘ strip ⧗ Number → Number
+# ceiled integer log₃ by counting number of trits
+log₃ log₃* ∘ strip ⧗ Number → Number
-:test (log3 (+0)) ((+0))
-:test (log3 (+5)) ((+3))
-:test (log3 (+42)) ((+5))
+:test (log₃ (+0)) ((+0))
+:test (log₃ (+5)) ((+3))
+:test (log₃ (+42)) ((+5))
# returns the smallest number in a range such that a predicate is true
binary-search z [[[[rec]]]] ⧗ (Number → Boolean) → Number → Number → Number
@@ -487,10 +487,10 @@ ternary-search z [[[[rec]]]] ⧗ (Number → Number) → Number → Number → N
:test ((ternary-search [-((0 - (+3)) ⋅ (0 - (+3)))] (+0) (+5)) =? (+3)) (true)
# pads a ternary number with 0s until it's as long a another ternary number
-pad y [[[(log3* 0) <? (log3* 1) (2 1 →⁰0) 0]]] ⧗ Number → Number → Number
+pad y [[[(log₃* 0) <? (log₃* 1) (2 1 →⁰0) 0]]] ⧗ Number → Number → Number
# forces number to be exactly n trits long (either pad/trim)
-force [[[0 <? 2 pad trim] (log3* 0)]] ⧗ Number → Number
+force [[[0 <? 2 pad trim] (log₃* 0)]] ⧗ Number → Number
pad z [[[=?1 0 (2 --1 →⁰0)]]] (2 - 0) 1
trim z [[[=?1 0 (2 --1 ←0)]]] (0 - 2) 1
@@ -502,10 +502,10 @@ double-shift [[[[[left : right]] (force 2 1) (force 2 0)]]]
# "efficient" quotient/remainder implementation for balanced ternary
# technique by Douglas W. Jones
-# algorithm originally intended for fixed-width numbers (=> ugly hacks with force+log3)
+# algorithm originally intended for fixed-width numbers (=> ugly hacks with force+log₃)
# TODO: remove the final `huh` correction step (probably some off-by-one bug?)
# TODO: not actually that efficient right now
-quot-rem [[[[[z [[[[rec]]]] 1 (+0) 4]]] <?0 (max (log3 1) (log3 0)) 0]] ⧗ Number → Number → (Pair Number Number)
+quot-rem [[[[[z [[[[rec]]]] 1 (+0) 4]]] <?0 (max (log₃ 1) (log₃ 0)) 0]] ⧗ Number → Number → (Pair Number Number)
rec =?2 huh (double-shift 5 1 0 [[compare-case eq gt lt 1 (+0)]])
huh (>?1 ⋀? 6) ⋁? (<?1 ⋀? \6) (--0 : (1 + 7)) (0 : 1)
eq 5 --4 1 0
diff --git a/std/Number/Unary.bruijn b/std/Number/Unary.bruijn
index 5041614..3b58ceb 100644
--- a/std/Number/Unary.bruijn
+++ b/std/Number/Unary.bruijn
@@ -173,6 +173,26 @@ mod* [[1 &[[(0 ⋀? (2 ≤? 1)) case-rec case-end]] (1 : true) k]] ⧗ Unary →
case-rec (1 - 3) : true
case-end 1 : false
+# returns true if the number is even (remainder mod 2 == 0)
+even? [=?(0 % (+2u))] ⧗ Unary → Boolean
+
+=²?‣ even?
+
+:test (=²?(+0u)) (true)
+:test (=²?(+1u)) (false)
+:test (=²?(+41u)) (false)
+:test (=²?(+42u)) (true)
+
+# returns true if the number is odd (remainder mod 2 == 1)
+odd? ¬‣ ∘ even? ⧗ Unary → Boolean
+
+≠²?‣ odd?
+
+:test (≠²?(+0u)) (false)
+:test (≠²?(+1u)) (true)
+:test (≠²?(+41u)) (true)
+:test (≠²?(+42u)) (false)
+
# exponentiates two unary number
# doesn't give correct results for x^0
pow* [[1 0]] ⧗ Unary → Unary → Unary