# Python code not working correctly, same thing in java does

I tried to solve Project Euler 10 problem with python, but my program gave the wrong result. Since I'm a complete noob to python and I couldn't find any error in my (obviously brute-force) logic, I wrote the program in java (almost translated it) and this gave a different result that turned out to be correct.

Here is the python code:

``````from math import *

limit = 2000000

def isPrime(number):
if number == 2: return 1
elif number % 2 == 0: return 0
elif number == 3: return 1
elif number == 5: return 1
elif number == 7: return 1
else:
rootOfNumber = sqrt(number)
tag = 3
while tag < rootOfNumber:
if number % tag != 0:
tag += 2
else:
break           ###
if tag >= rootOfNumber: ###EDIT: it should by only tag > rootOfNumber here
return 1            ###       Thats what the problem was.
else:
return 0

sum = 2 # 2 is an even prime, something we are not iterating for
for i in range(3, limit, 2):
if isPrime(i) == 1:
sum += i

print(sum)
print('done...')
```

```

Equivalent Java Code:

``````public class Prob10{
static int limit = 2000000;
static long sum = 2L; // 2 is an even prime, something we are not iterating for

public static void main (String[] args) {
for(int i = 3; i < limit; i+=2) {
if( isPrime(i) )
sum += i;
}
System.out.println(sum);
}

private static boolean isPrime (int number) {
if (number == 2) return true;
else if (number == 3 || number == 5 || number == 7) return true;
else {
double rootOfNumber = Math.sqrt(number);
int tag = 3;
while (tag < rootOfNumber) {
if (number % tag != 0)
tag +=2;
else
break;
}
if (tag > rootOfNumber)
return true;
else
return false;
}
}

}
```

```

I think I am making some kind of stupid mistake or missing some subtle point.

ps I know my implementation is `isPrime`

not very good. I am not printing the outputs because it might mess up the problem for others.

Any comments about (bad) style in a python program are welcome.

+3

source to share

Try using code like `isPrime(49)`

. From there, you should figure out your problem. You replaced `>`

on `>=`

in `if (tag > rootOfNumber)`

. As well as some coding style, you can simply replace the first lines:

``````if i in (2, 3, 5, 7): return 1
elif number % 2 == 0: return 0
else:
......
```

```
+4

source

After a quick look, it seems to me that this line in the Python version is overkill and that might be the cause of the problem:

``````elif number % 2 == 0: return 0
```

```
+2

source

Why don't you return False for the return value 0? This would make it simpler.

+1

source

All Articles