tcal_rs 0.2.0

Number theory functions library - Rust port of libqalculate number theory module
Documentation
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# tcal_rs Library Documentation

## Overview

`tcal_rs` is a Rust port of libqalculate number theory module, providing comprehensive mathematical operations including number theory, calculator functionality, and utility functions.

---

## Module Structure

```
tcal_rs/
├── lib.rs                    # Main library entry point
├── number_theory.rs          # Core number theory functions
├── number_theory/
│   ├── parity.rs            # Even/odd checking
│   ├── primes.rs            # Prime number operations
│   ├── rounding.rs          # Rounding and absolute value
│   ├── totient.rs           # Euler's totient function
│   └── traits.rs            # Extension traits for i64/i128
├── calculator/
│   ├── mod.rs               # Calculator module
│   ├── lexer.rs             # Lexical analysis (tokenization)
│   ├── parser.rs            # Expression parsing (AST generation)
│   ├── ast.rs               # Abstract Syntax Tree definitions
│   ├── evaluator.rs         # Expression evaluation
│   ├── engine.rs            # Calculator engine with preprocessing
│   ├── formatter.rs         # Result formatting
│   └── converter.rs         # Unicode conversion utility
└── fprice.rs                # Price formatting with thousand separators
```

---

## Core Number Theory Module (`number_theory.rs`)

### Functions Overview

| Function | Description | Signature |
|----------|-------------|-----------|
| `gcd` | Greatest common divisor (Euclidean) | `(i64, i64) -> i64` |
| `gcd_i128` | GCD for i128 integers | `(i128, i128) -> i128` |
| `lcm` | Least common multiple | `(i64, i64) -> i64` |
| `lcm_i128` | LCM for i128 integers | `(i128, i128) -> i128` |
| `frac` | Fractional part of rational | `(i64, i64) -> (i64, i64)` |
| `rem` | Remainder of Euclidean division | `(i64, i64) -> i64` |
| `modulo` | Modulo operation (always non-negative) | `(i64, i64) -> i64` |
| `powmod` | Modular exponentiation | `(u64, u64, u64) -> u64` |
| `powmod_i64` | Modular exponentiation for signed integers | `(i64, u64, i64) -> i64` |
| `numerator` | Returns numerator of rational | `(i64, i64) -> i64` |
| `denominator` | Returns denominator of rational | `(i64, i64) -> i64` |

### Detailed Documentation

#### `gcd(a: i64, b: i64) -> i64`
Computes the greatest common divisor using the Euclidean algorithm.

**Algorithm:**
```rust
let mut a = a.abs();
let mut b = b.abs();
while b != 0 {
    let t = b;
    b = a % b;
    a = t;
}
a  // GCD
```

**Time Complexity:** O(log(min(a, b)))

**Examples:**
```rust
gcd(48, 18)  // Returns: 6
gcd(17, 23)  // Returns: 1 (coprime)
gcd(0, 5)    // Returns: 5
```

#### `lcm(a: i64, b: i64) -> i64`
Computes the least common multiple using the formula: `lcm(a,b) = |a/b| * gcd(a,b)`

**Edge Cases:**
- Returns 0 if either argument is 0

**Examples:**
```rust
lcm(21, 6)  // Returns: 42
lcm(5, 7)   // Returns: 35
lcm(0, 5)   // Returns: 0
```

#### `powmod(base: u64, exp: u64, modulus: u64) -> u64`
Computes `(base^exp) mod modulus` using binary exponentiation.

**Algorithm (Square-and-Multiply):**
```rust
while exp > 0 {
    if exp % 2 == 1 {
        result = (result * base) % modulus;
    }
    exp /= 2;
    base = (base * base) % modulus;
}
```

**Time Complexity:** O(log exp)

**Cryptographic Applications:**
- RSA encryption/decryption
- Diffie-Hellman key exchange
- Digital signatures

---

## Parity Module (`number_theory/parity.rs`)

### Functions

| Function | Description | Returns |
|----------|-------------|---------|
| `is_even` | Check if integer is even | `bool` |
| `is_odd` | Check if integer is odd | `bool` |
| `is_even_i128` | Check i128 parity | `bool` |
| `is_odd_i128` | Check i128 parity | `bool` |

### Implementation
```rust
pub fn is_even(n: i64) -> bool {
    n % 2 == 0
}

pub fn is_odd(n: i64) -> bool {
    n % 2 != 0
}
```

**Mathematical Definition:**
- n is even if n = 2k for some integer k
- n is odd if n = 2k + 1 for some integer k

---

## Primes Module (`number_theory/primes.rs`)

### Functions Overview

| Function | Description | Signature |
|----------|-------------|-----------|
| `is_prime` | Primality test (Miller-Rabin for large) | `u64 -> bool` |
| `next_prime` | Find smallest prime ≥ n | `i64 -> i64` |
| `prev_prime` | Find largest prime ≤ n | `i64 -> i64` |
| `nth_prime` | Get the nth prime (1-indexed) | `u64 -> u64` |
| `prime_count` | Count primes ≤ x (π function) | `i64 -> i64` |
| `primes_up_to` | Sieve of Eratosthenes | `i64 -> Vec<i64>` |
| `bernoulli` | Compute Bernoulli number B_n | `u64 -> Option<(i64,i64)>` |

### Algorithm Details

#### `is_prime(n: u64) -> bool`
Hybrid approach:
1. **Small numbers (< 1,000,000)**: Trial division with 6k±1 optimization
2. **Large numbers**: Miller-Rabin with 12 deterministic bases

**Miller-Rabin Witnesses for u64:**
```rust
const WITNESSES: &[u64] = &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];
```

These bases are proven deterministic for all 64-bit integers.

#### `primes_up_to(n: i64) -> Vec<i64>`
Implements the Sieve of Eratosthenes:

```rust
let mut sieve = vec![true; n + 1];
sieve[0] = false;
sieve[1] = false;

for i in 2..=sqrt(n) {
    if sieve[i] {
        for j in (i * i..=n).step_by(i) {
            sieve[j] = false;
        }
    }
}
```

**Time Complexity:** O(n log log n)
**Space Complexity:** O(n)

#### `nth_prime(n: u64) -> u64`
Two-phase approach:
1. **n ≤ 10,000**: Lookup from precomputed table
2. **n > 10,000**: Use Prime Number Theorem approximation:
   ```
   p_n ≈ n(ln(n) + ln(ln(n)))
   ```

#### `bernoulli(n: u64) -> Option<(i64, i64)>`
Computes Bernoulli numbers using the Akiyama-Tanigawa algorithm:

```rust
let mut a: Vec<(i64, i64)> = (0..=m).map(|j| (1, j as i64 + 1)).collect();

for m_curr in 1..=m {
    for k in (1..=m_curr).rev() {
        // a[k-1] = k * (a[k-1] - a[k])
        let diff_num = num1 * den2 - num2 * den1;
        let new_num = k * diff_num;
        a[k - 1] = simplify(new_num, diff_den);
    }
}
```

**Special Cases:**
- B₀ = 1
- B₁ = -1/2
- Bₙ = 0 for odd n > 1

---

## Rounding Module (`number_theory/rounding.rs`)

### Rounding Modes

```rust
pub enum RoundingMode {
    HalfToEven,        // 0: Banker's rounding
    HalfAwayFromZero,  // 1: Round away from zero at 0.5
    HalfTowardZero,    // 2: Round toward zero at 0.5
    HalfUp,           // 3: Always round up at 0.5
    HalfDown,         // 4: Always round down at 0.5
    Up,               // 5: Ceiling (toward +∞)
    Down,             // 6: Floor (toward -∞)
    TowardZero,       // 7: Truncation
    AwayFromZero,     // 8: Away from zero
}
```

### Functions

| Function | Description | Signature |
|----------|-------------|-----------|
| `abs` | Absolute value of rational | `(i64, i64) -> (i64, i64)` |
| `abs_integer` | Absolute value of integer | `i64 -> i64` |
| `ceil` | Ceiling (round toward +∞) | `(i64, i64) -> i64` |
| `floor` | Floor (round toward -∞) | `(i64, i64) -> i64` |
| `trunc` | Truncate (toward zero) | `(i64, i64) -> i64` |
| `round` | Round with specified mode | `(i64, i64, RoundingMode) -> i64` |
| `signum` | Sign of a number (-1, 0, 1) | `i64 -> i64` |

### Implementation Details

#### `ceil(numerator: i64, denominator: i64) -> i64`
```rust
let mut result = numerator / denominator;
let remainder = numerator % denominator;

if remainder > 0 && denominator > 0 || remainder < 0 && denominator < 0 {
    result += 1;
}
result
```

#### `round(numerator: i64, denominator: i64, mode: RoundingMode) -> i64`
Uses floating-point conversion for precise fractional comparison:

```rust
let integer_part = numerator / denominator;
let remainder = numerator.abs() % denominator.abs();
let fraction = (remainder as f64) / (denominator.abs() as f64);

match mode {
    RoundingMode::HalfToEven => {
        if fraction > 0.5 {
            integer_part + signum(integer_part)
        } else if fraction < 0.5 {
            integer_part
        } else {
            // At exactly 0.5, round to nearest even
            if integer_part % 2 == 0 { integer_part }
            else { integer_part + signum(integer_part) }
        }
    }
    // ... other modes
}
```

---

## Totient Module (`number_theory/totient.rs`)

### Euler's Totient Function

```rust
pub fn totient(n: i64) -> i64
```

**Algorithm (using prime factorization):**
```rust
let mut result = n;
let mut n_abs = n.abs();

// For each distinct prime factor p
if n_abs % 2 == 0 {
    while n_abs % 2 == 0 { n_abs /= 2; }
    result -= result / 2;  // Apply: result *= (1 - 1/2)
}

// Check odd factors
for i in (3..=sqrt(n)).step_by(2) {
    if n_abs % i == 0 {
        while n_abs % i == 0 { n_abs /= i; }
        result -= result / i;  // Apply: result *= (1 - 1/i)
    }
}

// If remainder > 1, it's prime
if n_abs > 1 {
    result -= result / n_abs;
}

result
```

**Time Complexity:** O(√n)

**Mathematical Formula:**
```
φ(n) = n × Π(1 - 1/p_i) for all distinct prime factors p_i
```

---

## Traits Module (`number_theory/traits.rs`)

### Extension Traits for i64/i128

```rust
pub trait Divisors {
    fn divisors(&self) -> Vec<i64>;
}

pub trait Gcd<T> {
    fn gcd(&self, other: &T) -> i64;
}

pub trait Lcm<T> {
    fn lcm(&self, other: &T) -> i64;
}
```

### Usage Example

```rust
use tcal_rs::number_theory::traits::{Divisors, Gcd, Lcm};

let n = 12i64;
let divisors = n.divisors();  // [1, 2, 3, 4, 6, 12]

let a = 48i64;
let b = 18i64;
let gcd = a.gcd(&b);  // 6
let lcm = a.lcm(&b);  // 144
```

### Implementation Notes

**Divisors Algorithm:**
```rust
let sqrt_n = (n as f64).sqrt() as i64;

for i in 1..=sqrt_n {
    if n % i == 0 {
        divisors.push(i);
        if i != n / i {
            divisors.push(n / i);
        }
    }
}
divisors.sort();
```

**Time Complexity:** O(√n)

---

## Calculator Module (`calculator/`)

### Architecture Overview

```
Input String
[Lexer] → Tokens
[Parser] → AST (Abstract Syntax Tree)
[Preprocessor] → Handle hex/bin/oct
[Evaluator] → Numeric Result
[Formatter] → Formatted Output
```

### Lexer (`lexer.rs`)

**Token Types:**
```rust
pub enum Token {
    Number(f64),
    Ident(String),
    Plus, Minus, Mul, Div, Pow,
    And, Or, Shl, Shr,
    Assign,
    LParen, RParen, Comma,
}
```

**Supported Operators:**
- Arithmetic: `+`, `-`, `*`, `/`, `^`
- Bitwise: `&` (AND), `|` (OR), `<<`, `>>`
- Assignment: `=`
- Grouping: `(`, `)`, `,`

### Parser (`parser.rs`)

**AST Definition:**
```rust
pub enum Expr {
    Number(f64),
    Variable(String),
    Assign { name: String, expr: Box<Expr> },
    Unary { op: UnaryOp, expr: Box<Expr> },
    Binary { left: Box<Expr>, op: BinaryOp, right: Box<Expr> },
    Call { name: String, args: Vec<Expr> },
}
```

**Operator Precedence (Highest to Lowest):**
1. Function calls
2. Power (`^`)
3. Unary minus
4. Multiplication/Division (`*`, `/`)
5. Addition/Subtraction (`+`, `-`)
6. Bitwise shifts (`<<`, `>>`)
7. Bitwise AND (`&`)
8. Bitwise OR (`|`)
9. Assignment (`=`)

### Evaluator (`evaluator.rs`)

**Built-in Constants:**
- `pi` → π (3.14159...)
- `e` → e (2.71828...)
- `res` → Previous result

**Built-in Functions:**
| Category | Functions |
|----------|-----------|
| Trigonometric | `sin`, `cos`, `tan`, `asin`, `acos`, `atan` |
| Roots | `sqrt`, `cbrt` |
| Logarithms | `ln`, `log`, `log10` |
| Other | `abs`, `exp` |
| Number Theory | `totient`, `gcd`, `lcm` |

### Engine (`engine.rs`)

**Features:**
1. **Hexadecimal Support:** `0x1A` → 26
2. **Binary Support:** `0b1010` → 10
3. **Octal Support:** `0o755` → 493
4. **Variable Storage:** `x = 5`
5. **Result Reference:** `res` holds last result

### Formatter (`formatter.rs`)

**Output Format:**
```
        42.000000
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
HEX : "0x2A"
DEC : "42"
OCT : "0o52"
BIN : "0010 1010"
11111111111111111111111111111111 11111111111111111111111111010110
63                      47                  32

11111111111111111111111111010110
31                      15                   0
```

**Features:**
- Thousand separators in decimal
- Hexadecimal, octal, binary representations
- 64-bit binary visualization
- 4-bit grouping for readability

### Converter (`converter.rs`)

**Unicode Conversion:**
```
Input: "to unicode A"
Output: [0] 'A' → U+0041

Input: "Hello"
Output:
[0] 'H' → U+0048
[1] 'e' → U+0065
[2] 'l' → U+006C
[3] 'l' → U+006C
[4] 'o' → U+006F
```

---

## Price Formatter (`fprice.rs`)

### Functionality

Adds thousand separators to integers:

```rust
PriceFormatter::format(1234567)  // "1,234,567"
PriceFormatter::format(42)       // "42"
```

**Implementation:**
```rust
for (i, c) in chars.iter().enumerate() {
    if i > 0 && (chars.len() - i).is_multiple_of(3) {
        result.push(',');
    }
    result.push(*c);
}
```

---

## Usage Examples

### Number Theory Operations

```rust
use tcal_rs::*;

// GCD and LCM
let gcd_val = gcd(48, 18);   // 6
let lcm_val = lcm(21, 6);    // 42

// Modular arithmetic
let remainder = powmod(4, 13, 497);  // 445

// Totient
let phi = totient(30);  // 8

// Prime operations
let prime = is_prime(7919);        // true
let next = next_prime(100);        // 101
let count = prime_count(100);      // 25
```

### Calculator Usage

```rust
use tcal_rs::calculator::Engine;

let mut engine = Engine::new();

// Basic arithmetic
engine.eval("2 + 2");          // "4"
engine.eval("2^10");           // "1024"

// Trigonometry
engine.eval("sin(pi/2)");      // "1"

// Number theory
engine.eval("totient(30)");    // "8"
engine.eval("gcd(48, 18)");    // "6"

// Variables
engine.eval("x = 5");          // "5"
engine.eval("x * 2 + 3");      // "13"

// Different bases
engine.eval("0xFF + 1");       // "256"
engine.eval("0b1010 + 0b10");  // "12"
```

---

## Performance Considerations

| Operation | Time Complexity | Space Complexity |
|-----------|-----------------|------------------|
| GCD (Euclidean) | O(log min(a,b)) | O(1) |
| LCM | O(log min(a,b)) | O(1) |
| Totient | O(√n) | O(1) |
| Miller-Rabin | O(k log³n) | O(1) |
| Sieve of Eratosthenes | O(n log log n) | O(n) |
| Divisors | O(√n) | O(√n) |
| Modular Exponentiation | O(log exp) | O(1) |

---

## Thread Safety

Most functions in the library are pure functions with no shared state, making them safe to use in multi-threaded contexts. The `Engine` struct maintains internal state (variables, last result) and should not be shared between threads without synchronization.

---

## Error Handling

- **Calculator functions**: Return `Result<String, String>` for error propagation
- **Number theory functions**: May panic on invalid inputs (e.g., division by zero)
- **Parser**: Returns `Result<Expr, String>` with descriptive error messages