Hi.. Is there any possible way of calculating some values at compile time? I have the following code of genereting upto 2000000010 palindrome numbers.
As you see, I have taken input from the user just after calculating the whole palindromes. So cant we calculate this at compile time? because runtime of this program is extremely slow.Code:#include <iostream> #include <string> #include <sstream> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <vector> using namespace std; vector<unsigned long> palindrome; int main() { for(long number=0,index=0;index<(20000000010);++number) { ostringstream stream; string strNumber; stream<<number; strNumber=stream.str(); int middle=strNumber.size()/2; bool flag=1; for(int j=0,k=strNumber.size()-1;j<=middle;++j,--k){ if(strNumber[j]!=strNumber[k]) { flag=0; break; } } if(flag) { unsigned long inNumber; istringstream convert(strNumber); convert>>inNumber; ++index; palindrome.push_back(inNumber); } } int i; while(cin>>i&&i) cout<<palindrome[i]<<endl; }
Another qs. I first tried to use array but It didnt allow 2*10^9 sized array. so what should I do whenever I need that size of array?