Math::Prime::TiedArray - Simulate an infinite array of prime numbers


Version 0.04


    use Math::Prime::TiedArray;
    tie my @primes, "Math::Prime::TiedArray";


Allows access to an array of prime numbers, that will be extended as-needed:

    use Math::Prime::TiedArray;

    my @primes;
    tie @primes, "Math::Prime::TiedArray";

    # print the first 100 primes:
    print join ", ", @primes[0..99];

    # print the 200th prime:
    print $primes[199];

    # print all the primes smaller than 500
    while ((my $prime = shift @primes) < 500) {
      print "$prime, ";


precompute => number (default: 1000)

Pre-calculate all primes smaller than 10,000:

    my @primes;
    tie @primes, "Math::Prime::TiedArray", precompute => 10_000;

cache => path

Use a persistant cache:

    my @primes;
    tie @primes, "Math::Prime::TiedArray", cache => "/path/to/cache.dbm"

extend_step => number (default: 10)

How many new primes should be calculated when the cache runs out.

extend_ceiling => number

Set a limit, triggering an exception (croak) if an attempt is made to find a prime larger than the ceiling.

debug => level (defualt: 0)

    Output debug messages:
    0 - none
    1 - progress updates for atkin
    2 - prime calculations
    3 - tie API
    4 - internal progress for atkin


Dan Boger, <cpan at>


Please report any bugs or feature requests to bug-math-prime-tiedarray at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc Math::Prime::TiedArray

You can also look for information at:


Copyright 2007 Dan Boger, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.