Math::...erator(3pm)
π Math::Prime::Util::PrimeIterator π
okay bestie, welcome to the cutest little prime-spitting machine ever. This page lives on the User Contributed Perl Documentation shelf, its header is literally Math::...erator(3pm) (they truncated it, tragic) and the actual module is Math::Prime::Util::PrimeIterator β yes, the full name, the whole glow-up. An object iterator for primes. Periodt. π
β¨ VERSION β¨
Version 0.74. She's current, she's cunty, she's numerically stable. π
πΈ 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)
like, look at that. no cap, the whole API is just vibing in one block. π¦
π DESCRIPTION π
It's an iterator over the primes. new hands you back an iterator object, and it'll happily take an optional starting position β the initial value becomes the least prime greater than or equal to the argument you feed it.
And if the value gets so big it overflows a Perl unsigned integer, you don't get garbage β you get a Math::BigInt object instead. main character energy, honestly. π
π METHODS π
π«Ά new
Creates an iterator object with an initial value of 2. If you pass an argument, the initial value will be the least prime greater than or equal to that argument. Slay, constructor.
β value
Returns the value at the current position. Will always be a prime (as she should be). If the value is greater than ~0, it comes back as a Math::BigInt object instead. π₯
π next
Moves the current position to the next prime. Returns self, so you can chain calls like a little necklace. $it->next()->next()->next() β obsessed.
π§ prev
Moves the current position to the previous prime β unless the current value is 2, in which case it just stays at 2. (Two is the floor, bestie. She refuses to go lower.) Returns self so calls can be chained.
π iterate
Returns the value at the current position and moves the position to the next prime. Two things at once, multitasking queen.
π rewind
Resets the current position to either 2, or β if you give it an integer argument β the least prime not less than the argument.
β¨ peek
Returns the value at the next position without moving the iterator. A little look-see. No commitment. π
π« tell_i
Returns the index of the current position, starting at 1 (which corresponds to the value 2). The iterator is unchanged after this call β she's just telling you where she's at, not moving.
π pred
Returns true if the argument is a prime, false otherwise. The iterator is unchanged after this call. It's a yes/no girlie.
π 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. Like, given a value n, we expect a prime approximately equal to n to occur at this index. Approximate! Not exact! Don't @ me.
The estimate is performed using prime_count_approx in Math::Prime::Util, which uses the estimates of Dusart 2010 (or better for small values). Academic royalty. π
π 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. Not prime? Not invited. π
π 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. Floor and ceil, twin sisters.
π₯ seek_to_i
Resets the position to the prime corresponding to the given index.
π¦ seek_to_value
An alias for rewind. Same girl, different name.
π i_start =head2 description =head2 values_min =head2 values_max =head2 oeis_anum
Methods to match Math::NumSeq::Primes.
yes that heading above is genuinely cursed β the raw =head2 markers are showing up where they shouldn't. That's how it landed in the source, and I'm keeping it, because facts are sacred even when the formatting is a mess. π
π 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.
and that's the whole page, bestie π«Ά this gorgeous thing is perl v5.42.3, dated 2026-08-20, and it came to you straight from Math::...erator(3pm). go generate some primes, you absolute icon πβ¨π
π¦ where this page comes from
- ubuntu stonking: libmath-prime-util-perl 0.74-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms