简体   繁体   English

Kotlin中的同一个程序与Python中的同一个程序会产生不同的结果

[英]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.

相关问题 为什么 python 中的 pow() function 和 dart 中的 pow() function 对相同的输入产生不同的结果? - Why pow() function in python and pow() function in dart produces different result for same input? 是什么让这两个不同缩进的 Python 代码产生不同的结果 - What makes these two Python codes with different indentation produces different result Python 上的 3DES 加密产生与 Nodejs/Java 不同的结果 - 3DES crypto on Python produces different result from Nodejs/Java 新编程,同样结果不同的程序 - New to programming, same result different program 为什么python json模块在同一文件上产生不同的编码 - Why python json module produces different encoding on same file Python —重构后的程序返回不同的结果 - Python — program after refactoring returns different result Plotly-dash python - 完全相同的代码但不同的结果? 一个在工作,一个不工作? - Plotly-dash python - exactly same code but different result? One working and one are not? PHP中的3DES结果与Python中的3DES结果不同 - 3DES result in PHP produces different result from 3DES in Python Python 中的并行程序不产生任何输出 - Parallel Program in Python produces no output 用Python 3.5编写一个程序,该程序读取一个文件,然后使用第一个文件中的相同文本以及其他内容写入另一个文件? - Write a program in Python 3.5 that reads a file, then writes a different file with the same text that was in the first one as well as more?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM