### ----------------------------------------------------
### -- Math::NumberTheory
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/01-integer-factors.rakutest
### ----------------------------------------------------
#!/usr/bin/env raku
use v6.d;

use Test;

#use lib <. lib>;
use Math::NumberTheory;

plan *;

## 1
subtest {
    is gcd-gaussian(10 + 15i, -3 + 2i) ∈ (2 + 3i, -3 + 2i), True;
    is (10 + 15i) gcd (-3 + 2i) ∈ (2 + 3i, -3 + 2i), True;
    is (10 + 15i) gcd 25, 0 - 5i;
    is 105 gcd (7 + 49i), 7 + 14i;
    is (10 + 15i) gcd 25, 25 gcd (10 + 15i);
}, 'GCD for Gaussian integers';

## 2
subtest {
    is gcd-rational(1 / 3, 1 / 5), 1 / 15;
    is gcd-rational(1 / 3, 2 / 5), 1 / 15;
    is gcd-rational(1 / 546, 2 / 980), 1 / 19110;
    is gcd-rational(1 / 3, 2 / 5, 1 / 7), 1 / 105;
    is (1 / 3) gcd (2 / 5), 1 / 15;
    is (1 / 3) gcd (2 / 5) gcd (1 / 7), 1 / 105;
}, 'GCD for Rationals';

## 3
subtest {
    is lcm-gaussian(-4 + 5i, 2 + 3i) ∈ (23 + 2i, -2 + 23i), True;
    is ((-4 + 5i) lcm (2 + 3i)) ∈ (23 + 2i, -2 + 23i), True;
    is ((10 + 15i) lcm 25) ∈ (50 + 75i, -75 + 50i), True;
    is 105 lcm (7 + 49i), 315 + 105i;
    is (10 + 15i) lcm 25, 25 lcm (10 + 15i);
}, 'LCM for Gaussian integers';

## 4
subtest {
    is lcm-rational(1 / 3, 1 / 5), 1;
    is lcm-rational(1 / 3, 2 / 5), 2;
    is lcm-rational(1 / 546, 2 / 980), 1 / 14;
    is lcm-rational(1 / 3, 2 / 5, 3 / 7), 6;
    is (1/3) lcm (2/5), 2;
    is (1 / 3) lcm (2 / 5) lcm (3 / 7), 6;
}, 'LCM for Rationals';

## 5
is-deeply factor-integer(11).Array, [(11, 1),];

## 6
is-deeply factor-integer(1).Array, [(1, 1),];

## 7
my $n3 = 30;
is ([*] factor-integer($n3).map({ $_.head ** $_.tail })), $n3;

## 8
is-deeply factor-integer(120).Array, [(2, 3), (3, 1), (5, 1)];

## 9
subtest {
    my $n = factorial(20);
    is factor-integer($n, 3).elems, 3;
    is factor-integer($n, 3, method => 'trial').elems ≤ 3, True;
    is factor-integer(20 ** 4, 5).elems ≤ 5, True;
    is factor-integer(20 ** 4, 5, method => 'trial').elems ≤ 5, True;
}, "max number of factors";

### 10
#is-deeply
#        factor-integer(120, :gaussian-integers).Array,
#        [(1 + i, 6), (1 + 2i, 1), (2 + i, 1), (3, 1)];

## 10
subtest {

    is is-composite(17), False, '17 is not composite';
    is is-composite(125), True, '125 is composite';
    is is-prime-power(125), True, '125 is prime power';
    is is-prime-power(147), False, '147 is not prime power';
    is mangold-lambda(25), 5.log, 'Mangold lambda of 25';
}, '&factor-integer related';

## 11
subtest {
    is are-coprime(8, 11), True, '8 and 11 are coprime';
    is are-coprime(8, 16), False, '8 and 16 are not coprime';
    is are-coprime([8, 16, 12]), False, '8, 16, 12 are not coprime';
    is are-coprime([4, 5, 3]), True, '4, 5, 3 are coprime';
    is are-coprime([3, 5, 3]), False, '3, 5, 3 are not coprime';
}, 'coprimes';

done-testing;


