Skip to content

Commit

Permalink
- Added the Mod .sqr method.
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Nov 3, 2022
1 parent 3896b9a commit 426b3d3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Sidef/Types/Number/Mod.pm
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ package Sidef::Types::Number::Mod {
__PACKAGE__->new($x->{n}->sqrtmod($x->{m}), $x->{m});
}

sub sqr {
my ($x) = @_;
__PACKAGE__->new($x->{n}->sqr, $x->{m});
}

sub znorder {
my ($x) = @_;
$x->{n}->znorder($x->{m});
Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Types/Number/Mod.pod
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ Aliases: I<real>

=cut

=head2 sqr

Mod.sqr()

Returns the

=cut

=head2 sqrt

Mod.sqrt()
Expand Down
1 change: 1 addition & 0 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,7 @@ package Sidef::Types::Number::Number {
Scalar__Scalar: {
if ($x > 0 and $y >= 0) {
if (CORE::log($x) * $y < CORE::log(ULONG_MAX)) {
$x == 1 and return 1;
my $r = (HAS_NEW_PRIME_UTIL ? Math::Prime::Util::powint($x, $y) : Math::Prime::Util::GMP::powint($x, $y));
if ($r < ULONG_MAX and $r > LONG_MIN) {
return $r;
Expand Down
31 changes: 31 additions & 0 deletions scripts/Tests/mod_class.sf
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,35 @@ do {
assert_eq(B.inv**2 -> flat.map{.mod(m)}, B.powmod(-2, m).flat)
}

do {
var n = 1e40.random_prime

assert_eq(Mod(2, n)**n, 2)
assert_eq(Mod(2, n)**(n-1), 1)

assert_eq(Gauss(Mod(2, n), 0)**(n-1), 1)
assert_eq(Gauss(Mod(2, n), Mod(0, n))**(n-1), 1)
assert_eq(Gauss(Mod(3, n), 0)**n, 3)

assert_eq(Quadratic(Mod(3, n))**n, 3)
assert_eq(Quadratic(Mod(3, n))**(n-1), 1)

assert_eq(Quaternion(Mod(3, n))**n, 3)
assert_eq(Quaternion(Mod(3, n))**(n-1), 1)

assert_eq(Mod(Fraction(2, 1), n)**n, 2)
assert_eq(Mod(Fraction(2, 1), n)**(n-1), 1)
assert_eq(Mod(Fraction(2, 1), Fraction(n, 1))**n, 2)
assert_eq(Mod(Fraction(2, 1), Fraction(n, 1))**(n-1), 1)

assert_eq(Fraction(Mod(2, n), Mod(1, n))**n, 2)
assert_eq(Fraction(Mod(2, n), Mod(1, n))**(n-1), 1)

assert_eq(Fraction(Mod(2, n), 1)**n, 2)
assert_eq(Fraction(Mod(2, n), 1)**(n-1), 1)

assert_eq(Fraction(Mod(2, n), 1.rat)**n, 2)
assert_eq(Fraction(Mod(2, n), 1.rat)**(n-1), 1)
}

say "** Test passed!"

0 comments on commit 426b3d3

Please sign in to comment.