---
name: "Math::Prime::Util::PrimeIterator"
section: "3pm"
platform: "linux"
package: "libmath-prime-util-perl"
version: "0.73-2+b5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/Math::Prime::Util::PrimeIterator"
---

# Math::Prime::Util::PrimeIterator(3pm)

An object iterator for primes

## NAME

Math::Prime::Util::PrimeIterator - An object iterator for primes

## VERSION

Version 0.73

## SYNOPSIS

```
  use Math::Prime::Util::PrimeIterator;
  my $it = Math::Prime::Util::PrimeIterator->new();
  # Simple use: return current value and move forward.
  my $sum = 0;  $sum += $it->iterate() for 1..10000;
  # Methods
  my $v = $it->value();     # Return current value
  $it->next();              # Move to next prime (returns self)
  $it->prev();              # Move to prev prime (returns self)
  $v = $it->iterate();      # Returns current value; moves to next prime
  $it->rewind();            # Resets position to 2
  $it->rewind($n);          # Resets position to next_prime($n-1)
  # Methods similar to Math::NumSeq, do not change iterator
  $it->tell_i();            # Returns the index of the current position
  $it->pred($n);            # Returns true if $n is prime
  $it->ith($i);             # Returns the $ith prime
  $it->value_to_i($n);      # Returns the index of the first prime >= $n
  $it->value_to_i_estimate($n);  # Approx index of value $n
  # Methods similar to Math::NumSeq, changes iterator
  $it->seek_to_i($i);       # Resets position to the $ith prime
  $it->seek_to_value($i);   # Resets position to next_prime($i-1)
```

## DESCRIPTION

An iterator over the primes.  "new" returns an iterator object and takes an optional starting position (the initial value will be the least prime greater than or equal to the argument).  BigInt objects will be returned if the value overflows a Perl unsigned integer value.

## METHODS

### new

Creates an iterator object with initial value of 2.  If an argument is given, the initial value will be the least prime greater than or equal to the argument.

### value

Returns the value at the current position.  Will always be a prime.  If the value is greater than ~0, it will be a Math::BigInt object.

### next

Moves the current position to the next prime. Returns self so calls can be chained.

### prev

Moves the current position to the previous prime, unless the current value is 2, in which case the value remains 2. Returns self so calls can be chained.

### iterate

Returns the value at the current position and also moves the position to the next prime.

### rewind

Resets the current position to either 2 or, if given an integer argument, the least prime not less than the argument.

### peek

Returns the value at the next position without moving the iterator.

### tell\_i

Returns the index of the current position, starting at 1 (corresponding to the value 2). The iterator is unchanged after this call.

### pred

Returns true if the argument is a prime, false otherwise. The iterator is unchanged after this call.

### ith

Returns the i'th prime, where the first prime is 2. The iterator is unchanged after this call.

### value\_to\_i\_estimate

Returns an estimate of the index corresponding to the argument.  That is, given a value `"n"`, we expect a prime approximately equal to `"n"` to occur at this index.

The estimate is performed using "prime\_count\_approx" in Math::Prime::Util, which uses the estimates of Dusart 2010 (or better for small values).

### value\_to\_i

If the argument is prime, returns the corresponding index, such that:

```
  ith( value_to_i( $n ) ) == $n
```

Returns `"undef"` if the argument is not prime.

### value\_to\_i\_floor

### value\_to\_i\_ceil

Returns the index corresponding to the first prime less than or equal to the argument, or greater than or equal to the argument, respectively.

### seek\_to\_i

Resets the position to the prime corresponding to the given index.

### seek\_to\_value

An alias for "rewind".

### i\_start =head2 description =head2 values\_min =head2 values\_max =head2 oeis\_anum

Methods to match Math::NumSeq::Primes.

## SEE ALSO

Math::Prime::Util

"forprimes" in Math::Prime::Util

"prime\_iterator" in Math::Prime::Util

"prime\_iterator\_object" in Math::Prime::Util

Math::Prime::Util::PrimeArray

Math::NumSeq::Primes

List::Gen

## AUTHORS

Dana Jacobsen \<dana@acm.org\>

## COPYRIGHT

Copyright 2013 by Dana Jacobsen \<dana@acm.org\>

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