Skip to content

Commit

Permalink
- Added several cube_* and cubefree_* aliases for 3.power_* and…
Browse files Browse the repository at this point in the history
… `3.powerfree_`.
  • Loading branch information
trizen committed Aug 27, 2023
1 parent a2cdf75 commit bb0784e
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 24 deletions.
65 changes: 52 additions & 13 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23929,6 +23929,10 @@ package Sidef::Types::Number::Number {
(TWO)->powerfree_divisors($_[0]);
}

sub cubefree_divisors {
(THREE)->powerfree_divisors($_[0]);
}

my $power_divisors_func = sub {
my ($k, $factor_exp) = @_;

Expand Down Expand Up @@ -24002,6 +24006,10 @@ package Sidef::Types::Number::Number {
(TWO)->power_divisors($_[0]);
}

sub cube_divisors {
(THREE)->power_divisors($_[0]);
}

my $power_udivisors_func = sub {
my ($k, $factor_exp) = @_;

Expand Down Expand Up @@ -24078,8 +24086,9 @@ package Sidef::Types::Number::Number {
(TWO)->power_udivisors($_[0]);
}

*unitary_square_divisors = \&square_udivisors;
*square_unitary_divisors = \&square_udivisors;
sub cube_udivisors {
(THREE)->power_udivisors($_[0]);
}

sub powerfree_udivisors {
my ($k, $n) = @_;
Expand Down Expand Up @@ -24128,15 +24137,13 @@ package Sidef::Types::Number::Number {
Sidef::Types::Array::Array->new(\@d);
}

*unitary_powerfree_divisors = \&powerfree_udivisors;
*powerfree_unitary_divisors = \&powerfree_udivisors;

sub squarefree_udivisors {
(TWO)->powerfree_udivisors($_[0]);
}

*unitary_squarefree_divisors = \&squarefree_udivisors;
*squarefree_unitary_divisors = \&squarefree_udivisors;
sub cubefree_udivisors {
(THREE)->powerfree_udivisors($_[0]);
}

sub prime_divisors {
my $n = _big2pistr($_[0]) // return Sidef::Types::Array::Array->new();
Expand Down Expand Up @@ -25771,16 +25778,32 @@ package Sidef::Types::Number::Number {
(TWO)->powerfree_usigma0($_[0]);
}

sub cubefree_usigma0 {
(THREE)->powerfree_usigma0($_[0]);
}

sub squarefree_usigma {
(TWO)->powerfree_usigma($_[0], $_[1]);
}

sub cubefree_usigma {
(THREE)->powerfree_usigma($_[0], $_[1]);
}

*squarefree_sigma0 = \&usigma0;

sub cubefree_sigma0 {
(THREE)->powerfree_sigma0($_[0]);
}

sub squarefree_sigma {
(TWO)->powerfree_sigma($_[0], $_[1]);
}

sub cubefree_sigma {
(THREE)->powerfree_sigma($_[0], $_[1]);
}

sub power_sigma0 {
my ($k, $n) = @_;

Expand Down Expand Up @@ -25862,6 +25885,14 @@ package Sidef::Types::Number::Number {
(TWO)->power_sigma($_[0], $_[1]);
}

sub cube_sigma0 {
(THREE)->power_sigma0($_[0]);
}

sub cube_sigma {
(THREE)->power_sigma($_[0], $_[1]);
}

sub power_usigma0 {
my ($k, $n) = @_;

Expand Down Expand Up @@ -25936,6 +25967,14 @@ package Sidef::Types::Number::Number {
(TWO)->power_usigma($_[0], $_[1]);
}

sub cube_usigma0 {
(THREE)->power_usigma0($_[0]);
}

sub cube_usigma {
(THREE)->power_usigma($_[0], $_[1]);
}

sub powerfree_sigma0 {
my ($k, $n) = @_;

Expand Down Expand Up @@ -29677,12 +29716,6 @@ package Sidef::Types::Number::Number {
Math::GMPz::Rmpz_odd_p($n) or return Sidef::Types::Bool::Bool::FALSE;
Math::GMPz::Rmpz_cmp_ui($n, 1729) >= 0 or return Sidef::Types::Bool::Bool::FALSE;

# Must also be an Euler pseudoprime to base 2
if (!Math::GMPz::Rmpz_fits_ulong_p($n)) {
Math::Prime::Util::GMP::is_euler_pseudoprime(Math::GMPz::Rmpz_get_str($n, 10), 2)
|| return Sidef::Types::Bool::Bool::FALSE;
}

state $nm1 = Math::GMPz::Rmpz_init_nobless();
state $nm1d2 = Math::GMPz::Rmpz_init_nobless();
state $pm1 = Math::GMPz::Rmpz_init_nobless();
Expand All @@ -29703,6 +29736,12 @@ package Sidef::Types::Number::Number {
}
}

# Must also be an Euler pseudoprime to base 2
if (!Math::GMPz::Rmpz_fits_ulong_p($n)) {
Math::Prime::Util::GMP::is_euler_pseudoprime(Math::GMPz::Rmpz_get_str($n, 10), 2)
|| return Sidef::Types::Bool::Bool::FALSE;
}

# Check: p^((n-1)/2) == +/-1 (mod n), for random primes p
foreach my $k (1 .. 7) {
my $p = _random_prime(CORE::int(CORE::sqrt(ULONG_MAX)));
Expand Down
146 changes: 135 additions & 11 deletions lib/Sidef/Types/Number/Number.pod
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,20 @@ Returns the cube of C<x>. Equivalent with C<x**3>.

=cut

=head2 cube_divisors

n.cube_divisors

Returns the cube divisors of C<n>.

say 10!.cube_divisors #=> [1, 8, 27, 64, 216, 1728]

Equivalent with:

n.divisors.grep { .is_cube }

=cut

=head2 cubefree

cubefree(n)
Expand All @@ -2022,6 +2036,16 @@ Returns the count of cubefree numbers <= C<n>, or in the range C<a..b>.

=cut

=head2 cubefree_divisors

n.cubefree_divisors

Returns an array with the cubefree divisors of C<n>.

say cubefree_divisors(120) #=> [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]

=cut

=head2 cubefree_each

n.cubefree_each { ... }
Expand All @@ -2033,6 +2057,25 @@ Aliases: I<each_cubefree>

=cut

=head2 cubefree_sigma

n.cubefree_sigma(k=1)

Sum of the cubefree divisors of C<n>, each divisor raised to power C<k>.

say cubefree_sigma(5040) #=> 4368
say cubefree_sigma(5040, 2) #=> 2484300

=cut

=head2 cubefree_sigma0

n.cubefree_sigma0

Returns the count of cubefree divisors of C<n>.

=cut

=head2 cubefree_sum

cubefree_sum(n)
Expand All @@ -2042,6 +2085,37 @@ Returns the sum of cubefree numbers <= C<n>, or in the range C<a..b>.

=cut

=head2 cubefree_udivisors

n.cubefree_udivisors

Returns the unitary cubefree divisors of C<n>.

say cubefree_udivisors(5040) #=> [1, 5, 7, 9, 35, 45, 63, 315]

=cut

=head2 cubefree_usigma

n.cubefree_usigma(k=1)

Sum of the unitary cubefree divisors of C<n>, each divisor raised to power C<k>.

say 5040.cubefree_usigma #=> 480
say 5040.cubefree_usigma(2) #=> 106600

=cut

=head2 cubefree_usigma0

n.cubefree_usigma0

Returns the number of unitary cubefree divisors of C<n>.

say cubefree_usigma0(5040) #=> 8

=cut

=head2 cubefull

cubefull(n)
Expand Down Expand Up @@ -2080,6 +2154,62 @@ Returns the sum of cubefull numbers <= C<n>, or in the range C<a..b>.

=cut

=head2 cube_sigma

n.cube_sigma(k=1)

Sum of the cube divisors of C<n>, each divisor raised to the power C<k>.

say 10!.cube_sigma #=> 2044
say 10!.cube_sigma(2) #=> 3037530

Equivalent with:

n.cube_divisors.sum {|d| d**k }

=cut

=head2 cube_sigma0

n.cube_sigma0

Returns the count of cube divisors of C<n>.

=cut

=head2 cube_udivisors

n.cube_udivisors

Returns the unitary cube divisors of C<n>, such that each divisor C<d> is a cube and C<gcd(n/d, d) = 1>.

say 15!.cube_udivisors #=> [1, 125, 729, 91125]

=cut

=head2 cube_usigma

n.cube_usigma(k=1)

Sum of the unitary cube divisors of C<n>, each divisor raised to power C<k>.

say cube_usigma(15!) #=> 91980
say cube_usigma(15!, 2) #=> 8304312692

Equivalent with:

n.cube_udivisors.sum {|d| d**k }

=cut

=head2 cube_usigma0

n.cube_usigma0

Returns the count of unitary cube divisors of C<n>.

=cut

=head2 cubic_formula

cubic_formula(a, b, c, d)
Expand Down Expand Up @@ -4589,8 +4719,8 @@ Aliases: I<is_positive>

Returns true when all the exponents in the prime-power factorization of C<n> are less than C<k>.

say 15.by { .is_powerfree(2) } # square-free numbers
say 15.by { .is_powerfree(3) } # cube-free numbers
say 15.by { .is_powerfree(2) } # squarefree numbers
say 15.by { .is_powerfree(3) } # cubefree numbers

=cut

Expand Down Expand Up @@ -6729,8 +6859,6 @@ Equivalent with (but much faster):
n.udivisors.grep { .is_powerfree(k) }
k.powerfree_divisors(n).grep {|d| gcd(n/d, d) == 1 }

Aliases: I<powerfree_unitary_divisors>, I<unitary_powerfree_divisors>

=cut

=head2 powerfree_usigma
Expand Down Expand Up @@ -8313,7 +8441,7 @@ Aliases: I<square_free_count>

squarefree_divisors(n)

Returns an array with the square-free divisors of C<n>.
Returns an array with the squarefree divisors of C<n>.

say squarefree_divisors(120) #=> [1, 2, 3, 5, 6, 10, 15, 30]

Expand Down Expand Up @@ -8385,7 +8513,7 @@ Aliases: I<squarefree_semiprimes_sum>

squarefree_sigma(n,k=1)

Sum of the square free divisors of C<n>, each divisor raised to power C<k>.
Sum of the squarefree divisors of C<n>, each divisor raised to power C<k>.

say squarefree_sigma(5040) #=> 576
say squarefree_sigma(5040, 2) #=> 65000
Expand Down Expand Up @@ -8424,15 +8552,13 @@ Returns the unitary squarefree divisors of C<n>.

say squarefree_udivisors(5040) #=> [1, 5, 7, 35]

Aliases: I<squarefree_unitary_divisors>, I<unitary_squarefree_divisors>

=cut

=head2 squarefree_usigma

n.squarefree_usigma(k=1)

Sum of the unitary squarefree divisors of C<n>, each divisor raised to the k-th power.
Sum of the unitary squarefree divisors of C<n>, each divisor raised to power C<k>.

say 5040.squarefree_usigma #=> 48
say 5040.squarefree_usigma(2) #=> 1300
Expand Down Expand Up @@ -8521,8 +8647,6 @@ Returns the unitary square divisors of C<n>, such that each divisor C<d> is a sq

say 5040.square_udivisors #=> [1, 9, 16, 144]

Aliases: I<square_unitary_divisors>, I<unitary_square_divisors>

=cut

=head2 square_usigma
Expand Down
4 changes: 4 additions & 0 deletions scripts/Tests/divisor_functions.sf
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ var h = Hash(
prime_divisors => 'prime_sigma',
prime_power_divisors => 'prime_power_sigma',
square_divisors => 'square_sigma',
cube_divisors => 'cube_sigma',
squarefree_divisors => 'squarefree_sigma',
cubefree_divisors => 'cubefree_sigma',

udivisors => 'usigma',
prime_udivisors => 'prime_usigma',
prime_power_udivisors => 'prime_power_usigma',
square_udivisors => 'square_usigma',
cube_udivisors => 'cube_usigma',
squarefree_udivisors => 'squarefree_usigma',
cubefree_udivisors => 'cubefree_usigma',

edivisors => 'esigma',
idivisors => 'isigma',
Expand Down

0 comments on commit bb0784e

Please sign in to comment.