he tried to use code tags, and failed, but at least he tried.
OP, the <> on the side edit bar will do code tags. You are close, wrap them in []
if the word tag were a tag it would be [tag] text [/tag]
This is a forehead-desk-smashing-question. Just write the thing in c++ (its a very simple problem) or, seeing as how its a common homework problem, get one of the 5 billion solutions in c++ to it online rather than try to convert it into what will be, after conversion, sloppy and poor c++ code. Python and C
++ do things very differently, and trying to use the python way of doing things in c++ is not pretty. Trying to use the c++ way of doing things in python isnt very nice either. I am not picking on you, but conversion between languages should be towards the back end of last resorts; it is rarely a good plan for a simple code segment.
this gets you the primes from 0-100, as a starting point. Its not the best, but it was sitting around in my other people's homework folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void pn()
{
unsigned long long i,j, s = 100; //s is how big a value you want to do. c++ unsigned long long is 2^64-1
//and cannot grow to be bigger like python's virtual integers.
// bigger than 64 bit int needs more work, if you are doing something huge like encrypts.
vector<bool> pn(s,true);
for(i = 3; i < pn.size(); i+=2)
{
if(pn[i])
for(j = i*2; j < pn.size(); j+=i)
pn[j] = false;
}
//below this line is just output. above is finding the primes.
cout << 2 << endl; //so you can use +=2 below.
for(i = 3; i < pn.size(); i+=2)
if(pn[i]) //this is your isprime function. if pn[value] then its prime.
cout << i << endl; //if you are going to ask nonsense like 0 and 1, set those false.
}
int main()
{
pn();
}
|