### Convert from Python to c++?

/code

code/
Last edited on
Gee, posting a language that absolutely depends on indentation to make sense of it, without using code tags to preserve that indentation.

> Convert from Python to c++?
What kind of a question is that?

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.
 ``123456789101112131415161718192021222324`` `````` 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 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(); } ``````
Last edited on
@afatperson, a friendly reminder on How To Use Code Tags
http://www.cplusplus.com/articles/jEywvCM9/
 Convert from Python to c++?

Since you know Python, you can tell what does the program achieve. (The how is Python-specific and thus not relevant.)

Since you know C++, you can reimplement the what.

In other words the conversion is via more abstract, language-independent description of the logic.
Topic archived. No new replies allowed.