# python代码不能正确运行，在java中也是这样(python code not running right, same thing in java does)

``` 我试图用python解决Project Euler问题10，但是我的程序给出了错误的结果。 由于我是一个完整的pyob到python并且我在我的（显然是暴力）逻辑中找不到任何错误，我在java中编写了一个程序（几乎翻译了它），它给出了一个不同的结果，结果证明是正确的。
这里是python代码：
```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...')
```
等效的java代码是：
```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;
}
}

}
```
我想我正在犯一些愚蠢的错误或者错过了一些微妙的观点。
ps我知道我的`isPrime`实现不太好。 我不打印输出，因为它可能会破坏别人的问题。
欢迎任何关于python程序中（坏）风格的评论。 I was trying to solve Project Euler problem 10 using python, but my program gave a wrong result. Since I am a complete noob to python and I could not find any fault in my (apparently brute-force) logic, I wrote a program in java (almost translated it), and it gave a different result, which turned out to be right.
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...')
```
The equivalent java code is:
```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 doing some silly mistake or missing some subtle point.
p.s. I know my `isPrime` implementation is not too good. I am not printing the outputs because it may spoil the problem for others.

## 最满意答案

``` 尝试使用代码运行，例如`isPrime(49)` 。 你应该从那里弄清楚你的问题。 在`if (tag > rootOfNumber)` ，用`>=`代替了`>` 。同样也可以用一些编码风格替换第一行：
```if i in (2, 3, 5, 7): return 1
elif number % 2 == 0: return 0
else:
......
```Try running with your code for example `isPrime(49)`. You should figure out your problem from there. You have replaced a `>` with a `>=` in `if (tag > rootOfNumber)` .Also as some coding style, you could just replace the first lines with:
```if i in (2, 3, 5, 7): return 1
elif number % 2 == 0: return 0
else:
......
``````
2012-03-09