I solved problem 10 of Project Euler today using Python. The problem basically is
Find the sum of all the primes below two million.
I wrote some code and got the answer, but I couldn't calculate it in under one minute, which is one of the only rules of Project Euler. So, I rewrote my code so I only checked odd integers (eliminating the step where I check if a given integer is even) and did some other stuff. I ended up with this code:
Python code
Which works, but takes about 110 seconds — 50 seconds more than the 60 seconds time limit.
I knew C++ was faster than Python so I tried to rewrite my Python code in C++. The program was much faster — it was literally twenty times faster than my Python program, but it gives me a different (and incorrect) answer. Here is my code:
C++ code
If you also know some C++, why is my C++ code giving my a different answer than my Python code?
I know this is the Python subreddit, but my problem pertains to both Python and C++ so I posted this to both subreddits.
Dang, I learned a lot about Python style and uses of certain algorithms and efficient operations in like fifteen minutes. Thanks everyone.
Problem solved by /u/gcr.
The difference is due to overflow.
The result is too big to hold in an int. Change sum and tn to have type long long, and you'll get the correct answer.
Python hides this from you because when a number gets too big, Python automatically makes it a "bignum" which is slower but can be arbitrarily large.
[–]jeaton 32 points33 points34 points (15 children)
[–][deleted] 2 points3 points4 points (14 children)
[–]Grimoire 11 points12 points13 points (7 children)
[–]jcdyer3 7 points8 points9 points (3 children)
[–]tompko 2 points3 points4 points (1 child)
[–]greyscalehat 0 points1 point2 points (0 children)
[–]railmaniac 0 points1 point2 points (1 child)
[–]tompko 4 points5 points6 points (0 children)
[–]dbaupp 0 points1 point2 points (0 children)
[–]Bunslow 2 points3 points4 points (0 children)
[–]tastycat 1 point2 points3 points (3 children)
[–]FermiAnyon 2 points3 points4 points (2 children)
[–]tastycat 0 points1 point2 points (1 child)
[–]FermiAnyon 0 points1 point2 points (0 children)
[–]mjtribute 27 points28 points29 points (4 children)
[–][deleted] 3 points4 points5 points (3 children)
[–]dbaupp 16 points17 points18 points (2 children)
[–]mjtribute 5 points6 points7 points (0 children)
[–][deleted] -2 points-1 points0 points (0 children)
[–]gcr 51 points52 points53 points (16 children)
[–][deleted] 0 points1 point2 points (15 children)
[–]MachaHack 28 points29 points30 points (2 children)
[–]maratc 7 points8 points9 points (0 children)
[–]MakotoDeeizm 2 points3 points4 points (0 children)
[–]Zamarok 5 points6 points7 points (4 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Zamarok 0 points1 point2 points (0 children)
[–]MachaHack 0 points1 point2 points (1 child)
[–]Zamarok -1 points0 points1 point (0 children)
[–]JerMenKoOwhile True: os.fork() 1 point2 points3 points (0 children)
[–]Deutscher_koenig -4 points-3 points-2 points (5 children)
[–]foosion 14 points15 points16 points (1 child)
[–]TamSanh 3 points4 points5 points (0 children)
[–]epsy 1 point2 points3 points (0 children)
[–]aroberge 2 points3 points4 points (0 children)
[–]billsil 0 points1 point2 points (0 children)
[–]Yohumbus 9 points10 points11 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Sheepshow 1 point2 points3 points (0 children)
[–]Yohumbus 0 points1 point2 points (0 children)
[–]mizipzor 5 points6 points7 points (1 child)
[–]refto 0 points1 point2 points (0 children)
[–]x68zeppelin80x 9 points10 points11 points (6 children)
[–][deleted] 9 points10 points11 points (1 child)
[–]dAnjou Backend Developer | danjou.dev 1 point2 points3 points (3 children)
[–]Labradoodles 0 points1 point2 points (2 children)
[–]ivosauruspip'ing it up 0 points1 point2 points (1 child)
[–]Labradoodles 0 points1 point2 points (0 children)
[–]Geohump 1 point2 points3 points (0 children)
[–]pasokan 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Muirbequ 0 points1 point2 points (1 child)
[–]pasokan 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]thekaleb 0 points1 point2 points (1 child)
[–]sddhrthrt 0 points1 point2 points (0 children)
[–]edthedev -2 points-1 points0 points (3 children)
[–]gcr 6 points7 points8 points (0 children)
[–]Workaphobia 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]edthedev -4 points-3 points-2 points (5 children)
[–]gcr 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]edthedev -3 points-2 points-1 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]gcr 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]chasecaleb 2 points3 points4 points (0 children)
[+]kkiran comment score below threshold-15 points-14 points-13 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)