Math::N...Numbers(3)
π¦ Math::N...Numbers(3) π¦
okay so real talk: this page lives in the User Contributed Perl Documentation, and the header truncates the title down to Math::N...Numbers(3) because terminals have zero chill π. but bestie, it's really about one very specific little sequence class, and it is so cute. let's go. π«Ά
π NAME π
Math::NumSeq::ProthNumbers -- Proth number sequence β¨
π SYNOPSIS π
use Math::NumSeq::ProthNumbers;
my $seq = Math::NumSeq::ProthNumbers->new;
my ($i, $value) = $seq->next;
look at that. four lines. immaculate. no notes. π
πΈ DESCRIPTION πΈ
The Proth numbers are literally these:
3, 5, 9, 13, 17, 25, 33, 41, 49, 57, 65, 81, 97, 113, ...
starting i=1
and they're the integers of the form k*2^n+1 for some k and n, where k < 2^n. that condition is the whole personality, bestie.
the k < 2^n condition means the values in binary have a low half of 00..01 and a high half of some value k, like this:
binary 1xxx0000000...0001
value binary k n 2^n
3 11 1 1 2
5 101 1 2 4
9 1001 2 2 4
13 1101 3 2 4
17 10001 2 3 8
25 11001 3 3 8
33 100001 4 3 8
41 101001 5 3 8
^^
||
k part --++-- low part
see how the k part and the low part just vibe there together? iconic. π
now here's the messy part π β taking all k < 2^n duplicates some values. for example 33 is k=4 n=3 as 4*2^3+1 and also k=2 n=4 as 2*2^4+1. and this happens for any even k, no cap. incrementing n on reaching k=2^n-1 makes a regular pattern, per the "Ith" section below. iykyk. β¨
π FUNCTIONS π
check "FUNCTIONS" in Math::NumSeq for the behaviour that's common to all the sequence classes, bestie β that's the shared lore. π
"$seq = Math::NumSeq::ProthNumbers->new()"β create and return a shiny new sequence object. fresh out the box. π
π Random Access π
"$value = $seq->ith($i)"β return the$i'th Proth number. heads up: the first number is3at"$i==1". not zero, bestie, one. don't embarrass yourself. π"$bool = $seq->pred($value)"β return true if$valueis a Proth number, meaning it's equal tok*2^n+1for somekandnwithk < 2^n. it's giving "are you one of us" energy. π«Ά"$i = $seq->value_to_i_estimate($value)"β return an estimate of theicorresponding to$value. it's roughlysqrt(2*$value). yes, roughly. it's an estimate, it said what it said. π
β FORMULAS β
Next
successive values can be calculated by keeping track of the 2^n power and incrementing k by adding such a power, like so:
initial
value = 1 # to give 3 on first next() call
inc = 2
limit = 4
next()
value += inc
if value >= limit
inc *= 2 # ready for next time
limit *= 4
return value
lowkey elegant, highkey obsessed. β¨
Ith
taking the values by their length in bits, the values are:
11 1 value i=1
101 1 value i=2
1x01 2 values i=3,4
1x001 2 values i=5,6
1xx001 4 values i=7 to 10
1xx0001 4 values
1xxx0001 8 values
1xxx00001 8 values
for a given 1xxx high part, the low zeros β which is the 2^n factor β is the same length and then repeated 1 bigger. that doubling can be controlled by a high bit of the i, so in the following, Z is either a zero bit or omitted, bestie:
1Z1
1xZ01
1xxZ001
1xxxZ0001
the ith Proth number can be formed from the bits of the index:
i+1 = 1zxx..xx binary
k = 1xx..xx
n = z + 1 + number of x's
the first 1zxxx desired is 10, which is had from i+1 starting from i=1. the z second highest bit makes n bigger, giving the Z above present or omitted. π
for example i=9 is bits i+1=1010 binary which as 1zxx is k=0b110=6, n=0+1+2=3, for value 6*2^3+1=49, or binary 110001. ate. π
it can be convenient to take the two highest bits of the index i together, so hhxx..xx so hh=2 or 3, then n = hh-1 + number of x's. slay. π
π SEE ALSO π
Math::NumSeqMath::NumSeq::CullenNumbersMath::NumSeq::WoodallNumbers
the whole friend group. go say hi. π«Ά
π HOME PAGE π
<http://user42.tuxfamily.org/math-numseq/index.html>
π LICENSE π
Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde
Math-NumSeq is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Math-NumSeq is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.
bestie the WITHOUT ANY WARRANTY part is in ALL CAPS for a reason β that's the legal version of "i'm not responsible for what happens next", so read it fr fr. π€
and that's the whole page, periodt π this was Math::N...Numbers(3) from perl v5.42.0, dated 2025-07-25. go forth and compute Proth numbers responsibly. mwah. ππ¦π
π¦ where this page comes from
- fedora 43: perl-Math-NumSeq 75-11.fc43 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms