FBB::PrimeFactors(3bobcat) | Prime Factorization | FBB::PrimeFactors(3bobcat) |
FBB::PrimeFactors - Performs the prime-number factorization of (BigInt) values
#include <bobcat/primefactors>
Linking option: -lbobcat
Integral values fall into two categories: prime numbers, whose only integral divisors are their own values and 1, and composite numbers, which also have at least one other (prime number) integral divisor. All composite integral values can be factorized as the product of prime numbers. E.g., 6 can be factorized as 2 * 3; 8 can be factorized as 2 * 2 * 2. Finding these prime factors is called the prime number factorization, or `prime factorization’. When factorizing a value its prime factors may sometimes repeatedly be used as integral divisors: 8 is factorized as pow(2, 3), and 36 is factorized as
36 = pow(2, 2) * pow(3, 2)
The class FBB::PrimeFactors performs prime number factorizations of FBB::BigInt values. When factorizing a value prime numbers up to sqrt(value) must be available, as prime numbers up to sqrt(value) may be factors of value. Currently PrimeFactors uses the sieve of Eratosthenes to find these prime numbers. To find the next prime number beyond lastPrime, the sieve of Eratosthenes must be used repeatedly for lastPrime += 2 until lastPrime is prime. Once determined, prime numbers can of course be used directly to determine the next prime number or to factorize an integral value. To accellerate prime number factorization and Eratosthenes’s sieve PrimeFactors saves all its computed prime numbers in either a std::vector or in a file. Once determined, these prime numbers may again be used when factorizing the next integral value.
After factorizing an integral value its prime number factors and associated powers are made available in a vector of (PrimeFactors::PrimePower) structs, containing the value’s sorted prime factors and associated powers.
FBB
All constructors, members, operators and manipulators, mentioned in this
man-page, are defined in the namespace FBB.
-
Copy and move constructors (and assignment operators) are available.
FBB::PrimeFactor objects created using the copy constructor or receiving their values using the copy assignment operator share the prime numbers storage device (the BigIntVector or the stream containing the primes) with their source objects.
#include <iostream> #include <bobcat/primefactors> using namespace std; using namespace FBB; int main(int argc, char **argv) {
PrimeFactors pf1("/tmp/primes");
PrimeFactors::Factors const *factors = &pf1.factorize(stoull(argv[1]));
cout << "Using /tmp/primes:\n";
for (auto &factor: *factors)
cout << factor.prime << "**" << factor.power << ’ ’;
vector<BigInt> primes;
PrimeFactors pf2(primes);
factors = &pf2.factorize(stoull(argv[1]));
cout << "\n"
"Using BigIntVector:\n";
for (auto &factor: *factors)
cout << factor.prime << "**" << factor.power << ’ ’;
cout << "\n"
"Collected primes: ";
for (auto &prime: primes)
cout << prime << ’ ’;
cout << ’\n’; }
If this program is run with argument 1950 it produces the following output:
Using /tmp/primes:
2**1 3**1 5**2 13**1
Using BigIntVector:
2**1 3**1 5**2 13**1
Collected primes: 2 3 5 7 11 13
bobcat/primefactors - defines the class interface
None Reported.
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.
This is free software, distributed under the terms of the GNU General Public License (GPL).
Frank B. Brokken (f.b.brokken@rug.nl).
2005-2020 | libbobcat-dev_5.07.00 |