c++ - "integer constant is too large for ‘long’ type" when finding largest prime factor -
i working on solving euler project 3:
description: prime factors of 13195 5, 7, 13 , 29. largest prime factor of number 600851475143 ?
this code generate answer. need integer type hold 600851475143
. when compile on gcc on mac get:
integer constant large ‘long’ type".
i expect long long hold number. tried making unsigned. why doesn't code hold small number , can make work?
#include <iostream> #include <vector> using namespace std; static bool isprimefactor(int numtotest,int testnum,vector<int> factors) { if(numtotest%testnum==0) // factor? { // see if prime factor for(unsigned int i=0; < factors.size(); i++) { if(testnum%factors[i]==0) // see if factor { //is divisble 1 have return false; } } return true; } return false; } int main() { unsigned long long numtotest=600851475143; unsigned int testnum=2; // 0 , 1 assumed vector<int> factors; while(testnum<numtotest) // don't go higher max num { if(isprimefactor(numtotest,testnum,factors)) // factor? { factors.push_back(testnum); // got through prime factors } // , none divided testnum++; } for(unsigned int i=0; < factors.size(); i++) { cout << "factor " <<factors[i] << endl; } cout<<"highest factor: " << factors[factors.size()-1]<<endl; return 0; }
check this question. have specify literal this:
600851475143ll
Comments
Post a Comment