Is there anyone who can translate my code from C ++ to Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there anyone who can translate my code from C ++ to Python?

ENG: I wrote code for finding prime numbers based on the "Sieve Eratosthenes" algorithm in C ++. I'm having a problem with size > 234 million numbers, I think it's a memory leak. I also think that Python with its long arithmetic will cope even with size > 234 000 000. I'll be sincerely glad if you rewrite this code (this code is as it is) in Python or as an option to show how to fix a memory leak. RUS: Я написал код для поиска простых чисел на основе алгоритма "Решето Эратосфена" на языке С++. У меня возникает проблема при size > 234 000 000 чисел, думаю, что утечка памяти. Так же думаю, что Python со своей длинной арифметикой справится даже при size > 234 000 000. Буду искренне рад, если Вы перепишите этот код (именно этот код как есть) на Python или как вариант показать как исправить утечку памяти. https://code.sololearn.com/cVEPFDDNwmeh/?ref=app

4th Jul 2018, 3:25 PM
Expert
Expert - avatar
5 Answers
+ 1
I ran the same program on my computer and got the following result : ( I have a 64 bit computer. The isue may be due to allocation of too much memory. ) info_file.txt : Всего в диапазоне [0, 234000000] 12850026 простых чисел Время: 56.502 сек (~ 0.9417 мин) The file 'list_primes_number.txt' generated after the operation was 128 MB in size. Perhaps if you optimize the code using a bool array instead of ints, you can generate the same output on your computer. Try this : https://code.sololearn.com/ceOkqrl72ODx/#cpp
5th Jul 2018, 10:14 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
Kinshuk Vasisht, That's not the problem. Output to the file is generated. Just for size > 234 000 000 gives an error "alloc_bad", if I am not mistaken. How to avoid it?
5th Jul 2018, 6:59 AM
Expert
Expert - avatar
+ 1
Kinshuk Vasisht, Oh, why the fuck didn't I catch up???!!!??? You do not need to use the num array as an integer because the array is already full. Need Boolean type of! Oh as soon as I did not understand... Thank you so much, man!
5th Jul 2018, 5:15 PM
Expert
Expert - avatar
+ 1
You're welcome! ^_^
5th Jul 2018, 5:16 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
In your program above, you declare the object for the file, but never call the functions required to open them. Maybe that is why the output is not generated. Try adding the following lines after the ofstream object declarations : info_file.open(); list_primes_number.open();
5th Jul 2018, 3:53 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar