Advertisement
Kaelygon

Integer to polynomial

Oct 24th, 2024 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <cstdint>
  8. #include <vector>
  9. #include <limits.h>
  10. #include <algorithm>
  11. #include <tuple>
  12.  
  13. #include <thread>
  14. #include <mutex>
  15. #include <condition_variable>
  16. #include <queue>
  17. #include <functional>
  18.  
  19.  
  20. //Math functions
  21. int64_t intPow(int64_t base, int64_t expo){
  22.     int64_t result = 1;
  23.     while (expo){
  24.         if (expo & 1) result *= base;
  25.         base *= base;
  26.         expo >>= 1;
  27.     }
  28.     return result;
  29. }
  30.  
  31. int64_t digitLen(int64_t n){
  32.     n = abs(n);
  33.     if (n < 10) return 1;
  34.     if (n < 100) return 2;
  35.     if (n < 1000) return 3;
  36.     if (n < 10000) return 4;
  37.     if (n < 100000) return 5;
  38.     if (n < 1000000) return 6;
  39.     if (n < 10000000) return 7;
  40.     if (n < 100000000) return 8;
  41.     if (n < 1000000000) return 9;
  42.     if (n < 10000000000) return 10;
  43.     if (n < 100000000000) return 11;
  44.     if (n < 1000000000000) return 12;
  45.     if (n < 10000000000000) return 13;
  46.     if (n < 100000000000000) return 14;
  47.     if (n < 1000000000000000) return 15;
  48.     if (n < 10000000000000000) return 16;
  49.     if (n < 100000000000000000) return 17;
  50.     if (n < 1000000000000000000) return 18;
  51.     return 19;
  52. }
  53.  
  54. int64_t int64Hash(const char* cstr){
  55.     int64_t result = 0;
  56.     while(cstr[0]){
  57.         result = (result<<8) | (result>>56);
  58.         result |= (uint8_t)cstr[0];
  59.         cstr++;
  60.     };
  61.     return result;
  62. }
  63.  
  64. void reverseCstr(char* cstr){
  65.     size_t l = strlen(cstr)-1;
  66.     for(size_t i=0; i<=l/2; i++){
  67.         char buf=cstr[i];
  68.         cstr[i]=cstr[l-i];
  69.         cstr[l-i]=buf;
  70.     }
  71. }
  72.  
  73. void int64ToChar(int64_t value, char* cstr, size_t byteSize) {
  74.     if(cstr==NULL){ return; }
  75.     memset(cstr, 0, byteSize);
  76.     size_t maxSize = byteSize-1;
  77.     maxSize = maxSize < sizeof(int64_t) ? maxSize : sizeof(int64_t);
  78.     memcpy(cstr, &value, maxSize);
  79.     if(cstr[0]=='\0'){ return; }
  80.     reverseCstr(cstr);
  81. }
  82.  
  83. template <class T>
  84. bool isPrime(T n){
  85.     if(n<=1){return false;}
  86.     if(n==2){return true;}
  87.     if(n%2==0){return false;}
  88.     T max = sqrt(n);
  89.     for(T i=3; i <= max; i+=2)
  90.         if (n % i == 0)
  91.             return false;
  92.     return true;
  93. }
  94.  
  95. bool is_numeric(char const *string)
  96. {
  97.     return std::all_of(string, string+strlen(string),
  98.                        [](unsigned char c) { return ::isdigit(c); });
  99. }
  100.  
  101.  
  102.  
  103. class ThreadPool {
  104. public:
  105.     ThreadPool(size_t numThreads);
  106.     ~ThreadPool();
  107.  
  108.     template<class F>
  109.     void enqueue(F&& f);
  110.  
  111.     void waitUntilFinished();
  112.  
  113. private:
  114.     std::vector<std::thread> workers;
  115.     std::queue<std::function<void()>> tasks;
  116.  
  117.     std::mutex queueMutex;
  118.     std::condition_variable condition;
  119.     std::condition_variable finishedCondition;
  120.     bool stop;
  121.     int tasksRemaining = 0;
  122. };
  123.  
  124. ThreadPool::ThreadPool(size_t numThreads) : stop(false) {
  125.     for (size_t i = 0; i < numThreads; ++i) {
  126.         workers.emplace_back([this] {
  127.             while (true) {
  128.                 std::function<void()> task;
  129.                 {
  130.                     std::unique_lock<std::mutex> lock(this->queueMutex);
  131.                     this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); });
  132.                     if (this->stop && this->tasks.empty()) return;
  133.                     task = std::move(this->tasks.front());
  134.                     this->tasks.pop();
  135.                 }
  136.  
  137.                 task();
  138.  
  139.                 {
  140.                     std::lock_guard<std::mutex> lock(this->queueMutex);
  141.                     tasksRemaining--;
  142.                     if (tasksRemaining == 0) {
  143.                         finishedCondition.notify_all();
  144.                     }
  145.                 }
  146.             }
  147.         });
  148.     }
  149. }
  150.  
  151. ThreadPool::~ThreadPool() {
  152.     {
  153.         std::unique_lock<std::mutex> lock(queueMutex);
  154.         stop = true;
  155.     }
  156.     condition.notify_all();
  157.     for (std::thread &worker : workers) {
  158.         worker.join();
  159.     }
  160. }
  161.  
  162. template<class F>
  163. void ThreadPool::enqueue(F&& f) {
  164.     {
  165.         std::unique_lock<std::mutex> lock(queueMutex);
  166.         tasks.emplace(std::forward<F>(f));
  167.         tasksRemaining++;
  168.     }
  169.     condition.notify_one();
  170. }
  171.  
  172. void ThreadPool::waitUntilFinished() {
  173.     std::unique_lock<std::mutex> lock(queueMutex);
  174.     finishedCondition.wait(lock, [this] { return tasksRemaining == 0; });
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. //Polynomial
  186. struct Polynom {
  187.  
  188.     //Term
  189.     struct Term {
  190.         int64_t mult;
  191.         int64_t base;
  192.         int64_t expo;
  193.  
  194.         Term(int64_t m = 1, int64_t b = 1, int64_t e = 1) : mult(m), base(b), expo(e){}
  195.  
  196.         bool operator==(const Term& other) const;
  197.         std::string getString() const;
  198.         int64_t value() const;
  199.         int64_t symbolCount() const;
  200.     };
  201.  
  202.     struct SearchCache {
  203.         std::vector<Polynom> *storage; //Pointer to unique polynom vector in thread scope
  204.         const Polynom *curPolynom; //Pointer of mainList[i]
  205.         int maxSymbols;
  206.         int maxBranches;
  207.         int termCount;
  208.         int64_t maxCoef;
  209.         int64_t maxExpo;
  210.  
  211.         SearchCache(
  212.             std::vector<Polynom> *localeStorage = nullptr,
  213.             const Polynom *polynomCur = nullptr,
  214.             int symbolsMax = 8,
  215.             int branchesMax = 8,
  216.             int countTerm = 3,
  217.             int64_t coefMax = 10000,
  218.             int64_t expoMax = 62
  219.         ):
  220.             storage(localeStorage),
  221.             curPolynom(curPolynom),
  222.             maxSymbols(symbolsMax),
  223.             maxBranches(branchesMax),
  224.             termCount(countTerm),
  225.             maxCoef(coefMax),
  226.             maxExpo(expoMax)
  227.         {}
  228.     };
  229.  
  230.     std::vector<Term> terms; //polynomial terms, form a*b^c
  231.     int64_t target;
  232.     int64_t remainder; //How much needs to be added to reach the target
  233.     bool validEnd=1;
  234.  
  235.     //Not enough redundatn calls to make caching useful
  236.     //  mutable int64_t cacheSymCount = -1;
  237.     //  mutable std::string cacheString;
  238.  
  239.     void add(Term term){
  240.         remainder+=term.value();
  241.         terms.push_back(term);
  242.     }
  243.  
  244.     void terminate(){
  245.         if(remainder==0){return;}
  246.         terms.push_back({-1*remainder,1,1});
  247.         remainder=0;
  248.     }
  249.  
  250.     std::string getString() const;
  251.     int64_t value() const;
  252.     int symbolCount() const;
  253.  
  254.     int64_t scoreTerm(const Term& term, int64_t remainder) const;
  255.     void sortTerms(SearchCache cfg, std::vector<Term> *candiTerms) const;
  256.     int searchTerms(SearchCache cfg) const;
  257.     bool isPrimesOnly() const;
  258. };
  259.  
  260. std::string Polynom::getString() const {
  261. //  if(cacheString!=""){return cacheString;}
  262.     std::string str;
  263.     for(int i=0;i<terms.size();i++){
  264.         if(i>0 && (terms[i].mult>=0) ){str+= "+";} //positive sign for next term
  265.         str+= terms[i].getString();
  266.     }
  267. //  cacheString=str;
  268.     return str;
  269. }
  270.  
  271. int Polynom::symbolCount() const {
  272. //  if(cacheSymCount!=-1){return cacheSymCount;}
  273.  
  274.     int count=0;
  275.     for(int i=0; i<terms.size();i++){
  276.         count+=terms[i].symbolCount();
  277.     }
  278.     count+=terms.size()-1; // sign and digit
  279. //  cacheSymCount=count;
  280.     return count;
  281. }
  282.  
  283. int64_t Polynom::value() const {
  284.     int64_t sum=0;
  285.     for(int i=0;i<terms.size();i++){
  286.         sum+=terms[i].value();
  287.     }
  288.     return sum;
  289. }
  290.  
  291. bool Polynom::isPrimesOnly() const {
  292.     for(int i=0;i<terms.size();i++){
  293.         Term term = terms[i];
  294.         if(
  295.             ( !isPrime(term.mult) && term.mult!=1 ) || //if any term isn't prime or 1
  296.             ( !isPrime(term.base) && term.base!=1 ) ||
  297.             ( !isPrime(term.expo) && term.expo!=1 )
  298.         ){
  299.             return 0;
  300.         };
  301.     }
  302.     return 1;
  303. }
  304.  
  305.  
  306. //Polynom::Term::
  307. bool Polynom::Term::operator==(const Term& other) const {
  308.  
  309.     if (
  310.         intPow(other.base,other.expo)==1 && //ignore base^expo==1
  311.         intPow(base,expo)==1
  312.     ) {
  313.         return (mult == other.mult);
  314.     }
  315.     return (mult == other.mult) && (base == other.base) && (expo == other.expo);
  316. }
  317.  
  318. std::string Polynom::Term::getString() const {
  319.     std::string str="";
  320.     if(
  321.         mult!=1 ||
  322.         ( mult==1 && (mult==1 && base ==1) )
  323.     ){
  324.         str+= std::to_string(mult);
  325.     }
  326.     if(base==1){return str;} //a
  327.  
  328.     if(mult!=1){
  329.         str+= "*";
  330.     }
  331.     str+= std::to_string(base);
  332.     if(expo==1){return str;} //a*b
  333.  
  334.     str+= "^" + std::to_string(expo);
  335.     return str; //a*b^c
  336. }
  337.  
  338. int64_t Polynom::Term::value() const{
  339.     return mult*intPow(base,expo);
  340. }
  341.  
  342. int64_t Polynom::Term::symbolCount() const { //remove trivial components and return term a*b^c as a string
  343.     bool trivExpo = intPow(base,expo)==1;
  344.  
  345.     int symCount = 0;
  346.     if(mult!=1){
  347.         symCount+=digitLen(mult);
  348.     }
  349.     if( trivExpo ){
  350.         if(symCount==0){return 1;} // 1*1^1 (1 digit)
  351.         return symCount; //a
  352.     }
  353.  
  354.     symCount += mult!=1; //multiply sign *
  355.     symCount+=digitLen(base);
  356.     if(expo!=1){
  357.         symCount+=digitLen(expo);
  358.     }
  359.  
  360.     return symCount; //b or b^c or a*b^c
  361. }
  362.  
  363.  
  364.  
  365.  
  366.  
  367. //Polynomial::
  368. //Fitness value for new term. Higher is better
  369. int64_t Polynom::scoreTerm(const Term& term, int64_t remainder) const {
  370.     int64_t termLength = term.symbolCount(); //how many chars the term has
  371.  
  372.     int64_t reduceWeight = 1+termLength; //Each new term adds plus or minus sign, so we prefer remainder reduction
  373.     int64_t lengthWeight = termLength;
  374.  
  375.     int64_t newRemainder = remainder + term.value();
  376.     int64_t charsReduced = digitLen(remainder) - digitLen(newRemainder); //how many chars the term reduces from remainder
  377.  
  378.     //weight
  379.     int64_t score = 0;
  380.     score += charsReduced * reduceWeight; //Higher = better
  381.     score -= termLength * lengthWeight; //Higher = worse
  382.     return score;
  383. }
  384.  
  385. void Polynom::sortTerms(SearchCache cfg, std::vector<Term> *candiTerms) const {
  386.     //Precompute scores
  387.     std::vector<std::pair<Term, int>> scoredTerms;
  388.     for (const auto& term : *candiTerms){
  389.         scoredTerms.emplace_back(term, scoreTerm(term, cfg.curPolynom->remainder));
  390.     }
  391.  
  392.     //sort highest score last
  393.     std::sort(scoredTerms.begin(), scoredTerms.end(), [](const auto& a, const auto& b){
  394.         return a.second > b.second;
  395.     });
  396.  
  397.     Term prevTerm;
  398.     candiTerms->clear();
  399.     for (const auto& pair : scoredTerms){
  400.         //Don't add terms with same value
  401.         if( !(pair.first==prevTerm) ){
  402.             candiTerms->push_back(pair.first);
  403.             prevTerm=pair.first;
  404.         }
  405.     }
  406. }
  407.  
  408. //Find candidates for polynomial term close to remainder
  409. int Polynom::searchTerms(SearchCache cfg) const {
  410.     if(cfg.curPolynom->remainder==0){return 0;}
  411.  
  412.     std::vector<Term> candiTerms; //candidate terms
  413.     Term newTerm = {0};
  414.     int64_t sign = cfg.curPolynom->remainder < 0 ? 1 : -1; //if remainder is negative, next coefficient should be positive
  415.  
  416.     for(int64_t base=2;base<=cfg.maxCoef;base++){
  417.         for(int64_t expo=1;expo<=cfg.maxExpo;expo++){
  418.  
  419.             int64_t power = intPow(base,expo);
  420.             if( power<=0 || ( power>cfg.maxCoef+abs(cfg.curPolynom->remainder) ) ){break;} //ignore overflown powers
  421.  
  422.             //Scale the power near reminder
  423.             int64_t mult = (abs(cfg.curPolynom->remainder)+power/2)/power;
  424.             if(mult<=0){mult=1;}
  425.             newTerm={ sign*mult, base, expo };
  426.  
  427.             int64_t newTermDiff = abs(cfg.curPolynom->remainder+newTerm.value()); //new term difference from remainder
  428.             if( newTermDiff > abs( cfg.curPolynom->remainder ) ){ break; } //break if the new term results in bigger abs(remainder)
  429.            
  430.             //If the new term meets the conditions, add it to candidate term list
  431.             if(newTerm.symbolCount() <= cfg.maxSymbols){ //Limit term symbol count
  432.                 candiTerms.push_back(newTerm);
  433.             }
  434.         }
  435.     }
  436.  
  437.     if(candiTerms.empty()){ return 1; }
  438.  
  439.     sortTerms(cfg,&candiTerms);
  440.  
  441.     //Add new branchest to main list. Indices 0 to maxBranches
  442.     for (int i = 0; (i < cfg.maxBranches) && (i < candiTerms.size()); i++){
  443.         Polynom bufCandi = *cfg.curPolynom;
  444.         bufCandi.add(candiTerms[i]);
  445.         cfg.storage->push_back(bufCandi);
  446.     }
  447.  
  448.     return 0;
  449. }
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456. //Main
  457. void sortPolynoms(std::vector<Polynom> *polys, uint maxCoef=10000) {
  458.    
  459.     //invalidate branches that didn't reach remainder=0
  460.     for (Polynom& polynom : *polys) {
  461.         if(abs(polynom.remainder) < maxCoef){
  462.             polynom.terminate();
  463.         }else
  464.         if(polynom.remainder != 0){
  465.             polynom.validEnd=0;
  466.         }
  467.     }
  468.    
  469.     //Remove incomplete polynomials
  470.     polys->erase(std::remove_if(polys->begin(), polys->end(), [](const Polynom& p) {
  471.         return !p.validEnd;
  472.     }), polys->end());
  473.  
  474.     //Precompute strings and symbol counts
  475.     struct PolynomWithString {
  476.         Polynom poly;
  477.         std::string cachedString;
  478.         int symbolCount;
  479.  
  480.         PolynomWithString(const Polynom& p) : poly(p), cachedString(p.getString()), symbolCount(p.symbolCount()) {}
  481.     };
  482.  
  483.     //Copy original polynomials to
  484.     std::vector<PolynomWithString> cachedPolys;
  485.     cachedPolys.reserve(polys->size());
  486.     for (const auto& poly : *polys) {
  487.         cachedPolys.emplace_back(poly);
  488.     }
  489.  
  490.     // Sort using the cached values
  491.     std::sort(cachedPolys.begin(), cachedPolys.end(), [](const PolynomWithString& a, const PolynomWithString& b) {
  492.         if (a.symbolCount != b.symbolCount) {
  493.             return a.symbolCount > b.symbolCount; // Sort by symbol count ascending
  494.         }
  495.         return a.cachedString > b.cachedString; // Sort by ASCII order
  496.     });
  497.  
  498.     //Remove duplicates
  499.     auto last = std::unique(cachedPolys.begin(), cachedPolys.end(), [](const PolynomWithString& a, const PolynomWithString& b) {
  500.         return a.cachedString == b.cachedString; // Remove duplicates based on cached string
  501.     });
  502.  
  503.     polys->clear(); //copy back to polys
  504.     for (auto it = cachedPolys.begin(); it != last; ++it) {
  505.         polys->push_back(it->poly);
  506.     }
  507. }
  508.  
  509.  
  510.  
  511. void printManual(char** argv){
  512.         std::string path = argv[0];
  513.         std::string programName = path.substr(path.find_last_of("/\\") + 1);
  514.         std::cout << "This program searches polynomial for a given number \n";
  515.         std::cout << "Usage    : ./" << programName << " [integer] [terms] [branches] [polynoms] [digits]\n";
  516.         std::cout << "Argument : Default : Explanation\n";
  517.         std::cout << "integer  : 2^31-1  : Number 0 to 2^63-10001. Non-numerics are hashed\n";
  518.         std::cout << "terms    : 2       : Number of terms in polynomial\n";
  519.         std::cout << "branches : 100     : Branch count per node in the tree search\n";
  520.         std::cout << "polynoms : 20      : Number of displayed polynomials\n";
  521.         std::cout << "digits   : Varies  : Number of digits in term constants\n";
  522. }
  523.  
  524. int main(int argc, char** argv){
  525.     if(argc>1){
  526.         std::string firstArg = argv[1];
  527.         if( firstArg=="-h" || firstArg=="-?" || firstArg=="--help" ){
  528.             printManual(argv);
  529.             return 0;
  530.         }
  531.     }
  532.  
  533.     const int64_t MAX_THREADS = 24;
  534.  
  535.     Polynom starter;
  536.     int64_t defaultTarget = int64Hash("Therma");
  537.     starter.target=defaultTarget;
  538.     //starter.target = 3083;
  539.     //starter.target = 13238717;
  540.     //starter.target=4611686018427387904;
  541.  
  542.     if(argc>0){
  543.         if(argv[1]){
  544.             if(is_numeric(argv[1])){
  545.                 size_t num=std::strtol(argv[1], NULL, 10);
  546.                 starter.target=num;
  547.             }
  548.             else{
  549.                 starter.target=int64Hash(argv[1]);
  550.             }
  551.         }
  552.     }
  553.  
  554.     starter.remainder = -starter.target;
  555.     //starter.target = 128;
  556.  
  557.     std::vector<Polynom> mainList; //Main list of polynom candidates
  558.     mainList.push_back(starter);
  559.  
  560.     int64_t MAX_COEF=pow(10,digitLen(starter.target)/2);
  561.     MAX_COEF = MAX_COEF > 10000 ? 10000 : MAX_COEF;
  562.  
  563.     //config
  564.     Polynom::SearchCache cache;
  565.     cache.maxBranches=300;
  566.     cache.termCount=2;
  567.     cache.maxSymbols=digitLen(starter.target);
  568.     cache.maxExpo=62;
  569.     cache.maxCoef = MAX_COEF;
  570.     size_t maxLeaves = 100000;
  571.     size_t maxDisplay = 20;
  572.     bool primalityCheck = 0;
  573.  
  574.     if(argc>1){
  575.         if(argv[2]){
  576.             size_t num=std::strtol(argv[2], NULL, 10);
  577.             cache.termCount=num;
  578.         }
  579.     }
  580.     if(argc>2){
  581.         if(argv[3]){
  582.             size_t num=std::strtol(argv[3], NULL, 10);
  583.             if(num){
  584.                 cache.maxBranches=num;
  585.             }
  586.         }
  587.     }
  588.     if(argc>3){
  589.         if(argv[4]){
  590.             size_t num=std::strtol(argv[4], NULL, 10);
  591.             if(num){
  592.                 maxDisplay=num;
  593.             }
  594.         }
  595.     }
  596.     if(argc>4){
  597.         if(argv[5]){
  598.             size_t num=std::strtol(argv[5], NULL, 10);
  599.             if(num){
  600.                 MAX_COEF=num;
  601.                 cache.maxCoef = MAX_COEF;
  602.             }
  603.         }
  604.     }
  605.  
  606.     size_t testedTerms = 0;
  607.  
  608.     ThreadPool pool(MAX_THREADS); // Adjust number of threads to hardware
  609.     for(int j=0;j<cache.termCount;j++){
  610.  
  611.         const std::vector<Polynom> prevList=mainList; //Make a copy that the list won't change
  612.         size_t prevSize=prevList.size();
  613.         prevSize= std::min(maxLeaves,prevSize); //Limit maximum leaf node count
  614.  
  615.         std::mutex mainListMutex;
  616.         std::vector<std::vector<Polynom>> threadStorage; //New branches storage. unique per thread
  617.         threadStorage.resize(prevSize);
  618.  
  619.        
  620.         //searchTerms function will be adding new elements each loop
  621.         for(int i=0;i<prevSize;i++){
  622.             if( prevList[i].validEnd==0 || prevList[i].remainder==0 ){ continue; } //Skip invalid or terminated branches
  623.  
  624.             Polynom::SearchCache localCache=cache; //Copy config for each thread. Usually better for small caches
  625.             localCache.curPolynom=&prevList[i]; //Address of polynom copy that the thread will branch out
  626.             localCache.storage=&threadStorage[i];
  627.  
  628.             // Enqueue work for the thread pool
  629.             pool.enqueue([i, localCache, &mainList, &prevList, &mainListMutex] {
  630.                 bool err = localCache.curPolynom->searchTerms(localCache); //populater threadStorage aka localCache.storage
  631.             });
  632.         }
  633.         pool.waitUntilFinished();
  634.  
  635.         //invalidate previous branches that weren't terminated and therefore become part of the trunk
  636.         for (int i=0; i<prevSize; i++){            
  637.             if (!threadStorage[i].empty()) {//If valid
  638.                 size_t branchCount = std::min(threadStorage[i].size(), (size_t)cache.maxBranches); //Insert last N elements to maunList
  639.                 mainList.insert(mainList.end(), threadStorage[i].end() - branchCount, threadStorage[i].end());
  640.             }else if(mainList[i].remainder!=0){
  641.                 mainList[i].validEnd = 0;
  642.                 if (i != 0) {
  643.                     std::cout << "No branches found for " << mainList[i].getString() << "\n";
  644.                 }
  645.             }
  646.  
  647.             if( abs(prevList[i].remainder) < cache.maxCoef ){
  648.                 mainList[i].terminate(); //if remainder is X digits or shorter, add it as constant +N*1^1
  649.             }else
  650.             if (mainList[i].remainder!=0){
  651.                 mainList[i].validEnd=0;
  652.             }
  653.         }
  654.  
  655.         std::cout << "Depth: " << j << " Branches: " << mainList.size() << "\n";
  656.         testedTerms+=mainList.size();
  657.     }
  658.  
  659.     sortPolynoms(&mainList,MAX_COEF);
  660.  
  661.     //Print
  662.     int count = 0;
  663.  
  664.     size_t startPrint=0;
  665.     if(maxDisplay!=-1 && !primalityCheck){
  666.         startPrint=0;
  667.         if(mainList.size()>maxDisplay){
  668.             startPrint=mainList.size()-maxDisplay;
  669.         }
  670.     }
  671.     for (size_t i = startPrint; i < mainList.size(); i++) {
  672.         if( primalityCheck && !mainList[i].isPrimesOnly() ){ continue; }
  673.         std::string bufStr=mainList[i].getString();
  674.         std::cout << bufStr;
  675. //      std::cout << " " << mainList[i].symbolCount();
  676.         std::cout << "\n";
  677.         count++;
  678.     }
  679.     if(mainList.size()>0){
  680.         int64_t polyValue = mainList[0].value();
  681.         char polyAscii[9];
  682.         int64ToChar(polyValue,polyAscii,sizeof(polyAscii));
  683.         std::cout << "Decimal: " << polyValue << "\n";
  684.         std::cout << "Ascii: " << polyAscii << "\n";
  685.     }
  686.     std::cout << "Listed: " << count << "\n";
  687.     std::cout << "Terms tested: " << testedTerms << "\n";
  688.    
  689.     if(argc<=1){
  690.             printManual(argv);
  691.     }
  692.  
  693.     return 0;
  694. }
Tags: #math
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement