On 7/20/2013 12:06 AM, Dana Jacobsen via RT wrote:
Show quoted text> <URL:
https://rt.cpan.org/Ticket/Display.html?id=87042 >
>
> My reading of the documentation seems pretty clear that nth_prime returns the absolute nth prime or 0 (if the nth prime is past the sieve range). I've had no luck reproducing a result of 1 on various perl versions on Linux, Cygwin, and Windows, and there's nothing in the code that would make me think that would be output. nth_prime(1) returns either 2 or 0 (if initialized with 0, undef, negative, etc.). The lower bound for the sieve is always 2 (the first prime).
>
> I think perhaps you're getting the functionality of ranged_primes(lower, upper) confused with the sieving. The sieve in this module is always from 2 to N. Then the methods give fast answers within that range.
>
> Regarding Math::BigInt, this should be a non-issue with 64-bit Perl with this module. I could see the case on a 32-bit Perl where it is restricted to (I believe) ~2 billion, but I suspect that isn't what you're talking about.
>
> While it might be inefficient, some of the functions you're asking for could be done by indexing into the ranged_primes return. Methods on $sieve:
>
> # Return the absolute nth prime:
> $sieve->nth_prime($index)
> # Returns the 4th prime in the list (sorry, 0 based)
> $sieve->ranged_primes(5, 16)->[3]
> # Returns the count for the entire sieve
> $sieve->sieve_count()
> # Returns the count in this range
> scalar @{$sieve->ranged_primes(5,16)}
>
> You may want to look at Math::Prime::Util (and Math::Prime::Util::GMP) which has better support for ranged operations and big numbers. It will happily give you primes between 10**18 and 10**18+n, or 10**101 to 10**101+n, etc. It also does efficient ranged prime counts.
>
>
when I try to get the $aref or @aref list from ranged_primes() I get
various useless results:
size of scalar(@aref)=1 $#aref=0
when I should be getting much larger numbers. the list from this
function and probably primes also is inaccessible. it appears it is only
accessible through the API via nth_prime(). if this is intended, there
needs to be some forwards/backwards index lookup functionality to make
ranged_primes() usable. apparently, it does the whole set of prime
numbers regardless of the lower bounds I set. this may be by design due
to sieve algorithm I figure.
Specifically, some sort of functions that:
- looks up the index of a prime number in the list that augments
nth_prime(). it should return failure if it's not found.
- looks up the specific prime number of an index in the list that
augments nth_prime(). it should return failure if it's not in list.
in other words, some lookup functionality. otherwise, it's possible that
range_primes() should be deleted entirely if we cannot access the list
of it at all anyway and no functionality to access that list is going to
be provided.
otherwise, I would be spending my time re-generating the list of primes
in order to find out what the primes were in that list, and that would
basically be rewriting your code.
according to the pm file:
SV* Sieve::ranged_primes( long lower, long upper )
upper and lower appears to be a prime number.
when I run my twin-primes.pl code as you see in the bug report,
I get for center 10000 offset +/-500, which gives me:
(3, 5) index=1 difference=3
(5, 7) index=2 difference=1
(11, 13) index=3 difference=2
(17, 19) index=4 difference=2
(29, 31) index=5 difference=3
(41, 43) index=6 difference=3
(59, 61) index=7 difference=4
(71, 73) index=8 difference=3
(101, 103) index=9 difference=6
(107, 109) index=10 difference=2
...
(9857, 9859) index=204 difference=12
(9929, 9931) index=205 difference=8
(10007, 10009) index=206 difference=6
(10037, 10039) index=207 difference=2
(10067, 10069) index=208 difference=3
(10091, 10093) index=209 difference=3
(10139, 10141) index=210 difference=6
(10271, 10273) index=211 difference=16
(10301, 10303) index=212 difference=3
(10331, 10333) index=213 difference=4
(10427, 10429) index=214 difference=8
(10457, 10459) index=215 difference=4
center=10000
offset=500
lower bound=9500
upper bound=10502
min difference=
max difference=24
count=1285
this starts at 3, for a prime number, not 9500 which is consistent with
nth_prime().
no list access API function is provided, and so far, I have been unable
by any method I have tried to access this array/list returned by
ranged_primes(). a list access method/function in the API needed to be
provided, please. it looks like std::vector<bool>, and I should hope
that somehow this translates well to Perl, but apparently this does not.
try it for yourself.
use Math::Prime::FastSieve qw( primes );
my $sieve = Math::Prime::FastSieve::Sieve->new(10000);
my @aref = $sieve->ranged_primes( 9500, 10500);
print "size of aref=scalar:" . scalar(@aref) . " \$\#:" . $#aref . "\n";
for ($i=0; $i <= $#aref; $i++) {
printf $aref[$i] . "\n";
}
my $aref2 = $sieve->ranged_primes( 9500, 10500);
print "size of aref=scalar:" . scalar(@aref2) . " \$\#:" . $#aref2 . "\n";
for ($i=0; $i <= scalar(@aref2); $i++) {
printf $aref2[$i] . "\n";
}
-----Documentation:
- I believe the ranged_primes() example to be wrong, my $aref = returned
from ranged_primes should be my @aref = if it really is a standard perl
list.
- undocumented structures etc. I don't know what's in these structures
or data types or macros.
so I don't know how to access the elements in $aref in the examples. I
would like to see an example that walks the list and prints out the
primes for a ranged prime, because what I have doesn't work, and it's
only based on best-guess, not enough info.
the undocumented structure information is critical to accessing the
list. specifically:
newRV_noinc
AV
newAV
newSVuv
av_push
newSVuv
actual source code is not provided, so I can't tell what's in the
structures.
I think I said before that it would be a good idea to use int64_t. If I
remember right, Perl without Math::BigInt handles 64-bit numbers. so it
would be good to:
- use this as your integer number type in all functions (and translate
as such for return types)
- use this instead of bool? perl doesn't have bool to my knowledge.
I really like having the ranged_primes functionality (mainly because I
don't have to write it myself). I was just hoping to get it working. I
was also hoping for some enhancements in addition to the bug report on
ranged_primes().
--
Renewal Computer Services <
http://RenewalComputerServices.com>
Jim Michaels, Owner
1303 NE 87th Ave
<
http://www.mapquest.com/maps/1303+NE+87th+Ave+Vancouver+WA+98664-1959/>
(87th Ave & 13th St),
Vancouver, WA, 98664-1959 USA, 1-360-521-2060 Cell 1-10pm
Serving Vancouver, WA
DIY Computer Repair Info: Jesusnjim.com <
http://Jesusnjim.com>
jimm@RenewalComputerServices.com <mailto:jimm@RenewalComputerServices.com>
jmichae3@yahoo.com <mailto:jmichae3@yahoo.com>