Skip to content

Commit

Permalink
- Added the RangeNumber squarefree(a..b) method.
Browse files Browse the repository at this point in the history
Returns an array with the squarefree numbers in the given range.

Example:

	say squarefree(1..20)		#=> [1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19]
	say squarefree(1..20 `by` 2)	#=> [1, 3, 5, 7, 11, 13, 15, 17, 19]
  • Loading branch information
trizen committed Mar 6, 2021
1 parent c3033ad commit d495066
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10844,7 +10844,7 @@ package Sidef::Types::Number::Number {

my $n = _any2mpz($$from) // goto &nan;

Math::GMPz::Rmpz_cmp_ui($n, 3) >= 0
Math::GMPz::Rmpz_cmp_ui($n, 4) >= 0
or return ZERO;

my $pi = _set_int(_prime_count(Math::GMPz::Rmpz_get_str($n, 10)));
Expand Down
18 changes: 18 additions & 0 deletions lib/Sidef/Types/Range/RangeNumber.pm
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,24 @@ package Sidef::Types::Range::RangeNumber {
$self->grep(Sidef::Types::Block::Block->new(code => sub { $_[0]->is_prime }));
}

sub squarefree {
my ($self) = @_;

if ($self->{step}->abs->is_one) {

my $left = Sidef::Types::Number::Number->new($self->{from});
my $right = Sidef::Types::Number::Number->new($self->{to});

if ($self->{step}->is_neg) {
return Sidef::Types::Number::Number::squarefree($right, $left)->flip;
}

return Sidef::Types::Number::Number::squarefree($left, $right);
}

$self->grep(Sidef::Types::Block::Block->new(code => sub { $_[0]->is_squarefree }));
}

sub each_prime {
my ($self, $block) = @_;

Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Types/Range/RangeNumber.pod
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ Return the

=cut

=head2 squarefree

RangeNumber.squarefree() -> I<Obj>

Return the

=cut

=head2 sum_by

RangeNumber.sum_by() -> I<Obj>
Expand Down

0 comments on commit d495066

Please sign in to comment.