Skip to content

Fix several issues with the contextInteger.pl macro. #1297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions macros/contexts/contextInteger.pl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ =head2 isPrime

=cut

loadMacros('MathObjects.pl');
loadMacros('PGbasicmacros.pl', 'MathObjects.pl');

sub _contextInteger_init { context::Integer::Init() }

Expand Down Expand Up @@ -124,7 +124,7 @@ sub Init {
sub _divisor {
my $power = abs(shift);
my $a = abs(shift);
$self->Error("Cannot perform divisor function on Zero") if $a == 0;
Value::Error('Cannot perform divisor function on Zero') if $a == 0;
$result = 1;
$sqrt_a = int(sqrt($a));
for (my $i = 2; $i < $sqrt_a; $i++) {
Expand All @@ -146,17 +146,17 @@ sub _divisor {
sub _getPrimesInRange {
my $index = shift;
my $end = shift;
$self->Error("Start of range must be a positive number.") if $index < 0;
$self->Error("End of range must be greater than or equal to 2") if $end < 2;
$self->Error("Start or range must be before end of range") if $index > $end;
Value::Error('Start of range must be a positive number.') if $index < 0;
Value::Error('End of range must be greater than or equal to 2') if $end < 2;
Value::Error('Start of range must be before end of range') if $index > $end;
@primes = ();

# consider switching to set upper limit and static array of primes

push(@primes, 2) if $index <= 2;
# ensure index is odd
$index++ if $index % 2 == 0;
while ($index < $end) {
while ($index <= $end) {
push(@primes, $index) if context::Integer::Function::Numeric::isPrime($index);
$index += 2;
}
Expand All @@ -172,8 +172,8 @@ package context::Integer::Function::Numeric;
#
sub primeFactorization {
my $a = abs(shift);
$self->Error("Cannot factor Zero into primes.") if $a == 0;
$self->Error("Cannot factor One into primes.") if $a == 1;
Value::Error('Cannot factor Zero into primes.') if $a == 0;
Value::Error('Cannot factor One into primes.') if $a == 1;

my %factors;
my $n = $a;
Expand All @@ -200,7 +200,7 @@ sub primeFactorization {
#
sub phi {
my $a = abs(shift);
$self->Error("Cannot phi on Zero.") if $a == 0;
Value::Error('Cannot phi on Zero.') if $a == 0;
$result = $a;
$n = $a;
for (my $i = 2; ($i**2) <= $n; $i++) {
Expand Down Expand Up @@ -235,9 +235,8 @@ sub isPrime {
sub randomPrime {
my ($start, $end) = @_;
my @primes = context::Integer::_getPrimesInRange($start, $end);
$self->Error("Could not find any prime numbers in range.") if $#primes == 0;
my $primeIndex = $main::PG_random_generator->random(0, ($#primes - 1), 1);
return $primes[$primeIndex];
Value::Error('Could not find any prime numbers in range.') unless @primes;
return main::list_random(@primes);
}

package context::Integer::Function::Numeric2;
Expand Down