aboutsummaryrefslogtreecommitdiffhomepage
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/Char.bruijn4
-rw-r--r--std/Math.bruijn16
-rw-r--r--std/Number/Binary.bruijn13
-rw-r--r--std/Number/Conversion.bruijn22
4 files changed, 33 insertions, 22 deletions
diff --git a/std/Char.bruijn b/std/Char.bruijn
index a10f304..fc517b3 100644
--- a/std/Char.bruijn
+++ b/std/Char.bruijn
@@ -2,5 +2,7 @@
:input std/Number/Binary
+:import std/Number/Conversion .
+
# converts a char to a balanced ternary number
-number! [ternary! (0 - '0')] ⧗ Char → Number
+number! [binary→ternary (0 - '0')] ⧗ Char → Number
diff --git a/std/Math.bruijn b/std/Math.bruijn
index cfea07c..ae67136 100644
--- a/std/Math.bruijn
+++ b/std/Math.bruijn
@@ -77,9 +77,6 @@ pow* z [[[rec]]] ⧗ Number → Number → Number
³‣ [0 ⋅ 0 ⋅ 0]
case-end (+1)
-# prime number sequence
-primes nub ((…≠?… (+1)) ∘∘ gcd) (iterate ++‣ (+2)) ⧗ (List Number)
-
# factorial function
fac [∏ (+1) → 0 | i] ⧗ Number → Number
@@ -192,6 +189,19 @@ log* [z [[rec]] --0] ⧗ Number → Number
# TODO: something is wrong in here
pascal iterate [zip-with …+… ({}(+0) ++ 0) (0 ; (+0))] ({}(+1))
+# characteristic prime sequence by Tromp
+characteristic-primes ki : (ki : (sieve s0)) ⧗ (List Bool)
+ sieve y [[k : ([(2 0) (y' 0)] (ssucc 0))]]
+ y' [[0 0] [1 (0 0)]]
+ ssucc [[[[1 : (0 (3 2))]]]]
+ s0 [[[ki : (0 2)]]]
+
+# prime number sequence
+primes map fst (filter snd (zip (iterate ++‣ (+0)) characteristic-primes)) ⧗ (List Number)
+
+# slower but cooler prime number sequence
+primes* nub ((…≠?… (+1)) ∘∘ gcd) (iterate ++‣ (+2)) ⧗ (List Number)
+
# π as a list of decimal digits
# translation of unbounded spigot algorithm by Jeremy Gibbons
# TODO: faster!
diff --git a/std/Number/Binary.bruijn b/std/Number/Binary.bruijn
index fac9ba1..648af29 100644
--- a/std/Number/Binary.bruijn
+++ b/std/Number/Binary.bruijn
@@ -5,7 +5,6 @@
:import std/Combinator .
:import std/List .
:import std/Logic .
-:import std/Number/Ternary T
# bit indicating a one, compatible with std/Logic
b¹ true ⧗ Bit
@@ -182,16 +181,6 @@ dec [~(0 z a¹ a⁰)] ⧗ Binary → Binary
:test (--(+1b)) ([[[0 2]]])
:test (--(+3b)) ((+2b))
-# converts a binary number to a balanced ternary number
-# TODO: find a better solution
-ternary! z [[[rec]]] (+0t) ⧗ Binary → Ternary
- rec =?0 case-end case-inc
- case-inc 2 (T.inc 1) --0
- case-end 1
-
-:test (ternary! (+0b)) ((+0t))
-:test (ternary! (+42b)) ((+42t))
-
# flips the bits of a binary number (1's complement)
complement [[[[3 2 0 1]]]] ⧗ Binary → Binary
@@ -263,8 +252,6 @@ sub [[(0 =? 1) (+0b) -((pad 1 0) + -(pad 0 1))]] ⧗ Binary → Binary → Binar
:test ((+3b) - (+0b) =? (+3b)) (true)
:test ((+3b) - (+2b) =? (+1b)) (true)
-# TODO: mul/div
-
# rshifts least significant bit of a binary number
div² [~(0 z a¹ a⁰)] ⧗ Binary → Binary
z (+0b) : (+0b)
diff --git a/std/Number/Conversion.bruijn b/std/Number/Conversion.bruijn
index 41ddf38..c79e53f 100644
--- a/std/Number/Conversion.bruijn
+++ b/std/Number/Conversion.bruijn
@@ -1,17 +1,29 @@
# MIT License, Copyright (c) 2024 Marvin Borner
# convert bases to other bases
+:import std/Combinator .
:import std/Number/Unary U
+:import std/Number/Binary B
:import std/Number/Ternary T
# converts unary numbers to ternary
-unary→ternary [0 T.inc (+0)] ⧗ Unary → Ternary
+unary→ternary [0 T.inc (+0t)] ⧗ Unary → Ternary
-:test (unary→ternary (+0u)) ((+0))
-:test (unary→ternary (+2u)) ((+2))
+:test (unary→ternary (+0u)) ((+0t))
+:test (unary→ternary (+2u)) ((+2t))
# converts ternary numbers to unary
ternary→unary [T.apply 0 U.inc (+0u)] ⧗ Ternary → Unary
-:test (ternary→unary (+0)) ((+0u))
-:test (ternary→unary (+2)) ((+2u))
+:test (ternary→unary (+0t)) ((+0u))
+:test (ternary→unary (+2t)) ((+2u))
+
+# converts binary numbers to ternary
+# constructs reversed path of composed functions and applies to ternary
+binary→ternary [y [[[rec]]] [0] 0 (+0t)] ⧗ Binary → Ternary
+ rec B.zero? 0 case-end case-rec
+ case-rec B.odd? 0 (2 (1 ∘ T.inc) (B.dec 0)) (2 (1 ∘ (T.mul (+2t))) (B.div² 0))
+ case-end 1
+
+:test (T.eq? (binary→ternary (+0b)) (+0t)) ([[1]])
+:test (T.eq? (binary→ternary (+42b)) (+42t)) ([[1]])