File indexing completed on 2022-11-29 18:12:06

0001 /*
0002     PrimeNumber.cpp  -  source code of class PrimeNumber
0003     SPDX-FileCopyrightText: 2001 Sebastian Stein <seb.kde@hpfsc.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef PRIMENUMBER_H
0009 #define PRIMENUMBER_H
0010 
0011 #include <QVector>
0012 
0013 typedef QVector<uint> UnsignedIntArray;
0014 
0015 /** Class to handle prime numbers.
0016  *  The prime numbers are stored in a static vector, so that different instances
0017  *  can use them. Each time a higher prime number is needed, the new found prime
0018  *  number is stored in the vector. To check if a given number is a prime number
0019  *  this vector is scanned and if needed new prime numbers are generated. This
0020  *  algorithm should reduce calculation time and speed up every program using
0021  *  this prime number class.
0022  *  \author Sebastian Stein */
0023 class PrimeNumber
0024 {
0025 public:
0026     /** constructor */
0027     PrimeNumber();
0028 
0029     /** destructor */
0030     ~PrimeNumber();
0031 
0032     /** returns whether the given number is a prime number */
0033     short isPrimeNumber(uint number);
0034 
0035     /** returns the next prime number */
0036     unsigned int get_next();
0037 
0038     /** returns the first prime number */
0039     unsigned int get_first() const;
0040 
0041     /** return the last known prime number */
0042     unsigned int get_last() const;
0043 
0044     /** returns the current prime number */
0045     unsigned int get_current() const;
0046 
0047     /** moves the internal pointer to the first prime number */
0048     void move_first();
0049 
0050     /** moves the internal pointer to the last prime number */
0051     void move_last();
0052 
0053     /** moves the internal pointer to the next prime number */
0054     void move_forward();
0055 
0056     /** moves the internal pointer to the previous prime number */
0057     void move_back();
0058 
0059     /** Displays all known prime numbers, mainly used for debugging. */
0060     void display_all();
0061 private:
0062     /** a vector storing all known prime numbers, access for all objects;
0063      * we are using the vector<T> template; so we do not have to think
0064      * about dynamic mem manipulation */
0065     static UnsignedIntArray prim_vector;
0066 
0067     /** current selected prime number */
0068     UnsignedIntArray::iterator current_pos;
0069 
0070     /** finds next prime number and adds it to the vector */
0071     void find_next();
0072 };
0073 #endif