Skip to content

getPrimesInRange and randomPrime doubling truncating range #1296

@djordan99

Description

@djordan99

$self->Error("Start or range must be before end of range") if $index > $end;

while ($index < $end) {

I suggest changing the less than into less than or equal to in the while condition in line 159. The strict less than prevents the list of primes generated from including the $end. This means randomPrime(3,11), for example, cannot return 11. This seems counterintuitive given the behavior of random(3,11).

Alternatively, changing the condition in line 151 to include the equality would make the error message more useful. (Although also note the typo "or" in line 151).

$self->Error("Could not find any prime numbers in range.") if $#primes == 0;
my $primeIndex = $main::PG_random_generator->random(0, ($#primes - 1), 1);

In line 238, I believe the 0 was meant to be -1. If there were no primes in the range, then _getPrimesInRange would return an empty array with the final index negative one.

In line 239, ($#primes - 1) should be $#primes. As is, the code prevents the largest prime in the range from ever being selected.

Perhaps the intent was for randomPrime(a,b) to return a prime in [a,b). Although, again, that seems counterintuitive. But, in any case, as things stand, neither b nor the greatest prime less than b can result from randomPrime. For example, randomPrime(2,5) is always 2.

Thanks,
Dan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions