[英]Same program in Kotlin produces different result with the one in Python
I wrote two functions in different languages according to the algorithm that tests the primality of a number given by the overview for Project Euler problem 7. I don't find any difference in these two functions, but they give me different results. 我根据测试Euler问题7概述给出的数字的素性的算法,用不同的语言编写了两个函数。我发现这两个函数没有任何区别,但它们给出的结果不同。 Why?
为什么?
First one in Kotlin: 科特林的第一个:
import kotlin.math.floor
import kotlin.math.sqrt
fun isPrime(n: Int): Boolean {
if (n == 1) return false
else if (n < 4) return true
else if (n % 2 == 0) return false
else if (n < 9) return true
else if (n % 3 == 0) return false
else {
val r = floor(sqrt(n.toDouble())).toInt()
var f = 5
while (f <= r) {
if (n % f == 0) return false
else if (n % (f + 2) == 0) return false
f += 6
}
return true
}
}
fun main(args: Array<String>) {
var sum = 5
var n = 5
while (n <= 2000000) {
if (isPrime(n)) sum += n
n += 2
if (n <= 2000000 && isPrime(n)) sum += n
n += 4
}
println(sum)
}
the output is 1179908154 输出是1179908154
Then the one in Python: 然后是Python中的一个:
import math
def isPrime(n):
if n==1 :
return False
elif n<4 :
return True
elif n%2==0:
return False
elif n<9:
return True
elif n%3==0:
return False
else:
r=math.floor(math.sqrt(n))
f=5
while(f<=r):
if n%f==0:
return False
elif n%(f+2)==0:
return False
f+=6
return True
sum =5
n =5
while n<=2000000:
if isPrime(n):
sum+=n
n+=2
if n<=2000000 and isPrime(n):
sum+=n
n+=4
print(sum)
The output is 142913828922 输出是142913828922
These two program are same, but why they gave me different answer? 这两个程序是相同的,但是为什么它们给了我不同的答案? And.... First time ask a question in English.
并且....第一次用英语提问。 Sorry for the language.
语言很抱歉。
This is a case of integer limit jumping ( overflow ). 这是整数极限跳跃( 溢出 )的情况。
The output of python is correct. python的输出是正确的。 If you see actual value
142913828922
this is above 32bit limit of kotlin int ie 2^37. 如果您看到实际值
142913828922
该值高于kotlin int的32位限制,即2 ^ 37。 I dont know kotlin code but you can use java equivalent long
for this purpose. 我不知道kotlin代码,但是您可以为此目的
long
使用Java等效语言。
Looks like an overflow on the part of Kotlin. 看起来像是Kotlin的溢出。 Kotlin is still limited to the JVM, and you're likely processing values which are just a bit too big for
Int
. 科特林仍仅限于JVM的,你很可能会处理这只是一个有点太大价值
Int
。
If you convert everything in Kotlin to a Long
, you get the same number as you do in Python. 如果将Kotlin中的所有内容转换为
Long
,则得到的数字与Python中的数字相同。 Note: Python implicitly converts large numbers over to a long-like type, so you don't have to do that on your own, but that's a striking difference between the two languages. 注意:Python隐式地将大量数字转换为类似long的类型,因此您不必自己执行此操作,但这是两种语言之间的显着差异。
fun isPrime(n: Long): Boolean {
if (n == 1L) return false
else if (n < 4) return true
else if (n % 2 == 0L) return false
else if (n < 9) return true
else if (n % 3 == 0L) return false
else {
val r = floor(sqrt(n.toDouble())).toInt()
val r1 = floor(sqrt(n.toDouble()))
var f = 5
while (f <= r) {
if (n % f == 0L) return false
else if (n % (f + 2L) == 0L) return false
f += 6
}
return true
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.