Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在Swift 4中发现如下错误:_Swift_Xcode_Swift4_Prime Factoring - Fatal编程技术网

我在Swift 4中发现如下错误:

我在Swift 4中发现如下错误:,swift,xcode,swift4,prime-factoring,Swift,Xcode,Swift4,Prime Factoring,执行被中断,原因: EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0) 进程被留在中断点,请使用thread return-x返回到表达式求值之前的状态 import UIKit func numberWhosePrimeFactorIsNeeded(_ number : Int) { var a = 1 for primefactor in 2...600851475143 where number % primefactor == 0 {

执行被中断,原因:

EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0)

进程被留在中断点,请使用
thread return-x
返回到表达式求值之前的状态

import UIKit

func numberWhosePrimeFactorIsNeeded(_ number : Int) {
    var a = 1

    for primefactor in 2...600851475143 where number % primefactor == 0 {
        let c = a * primefactor

        if c == 600851475143 {
            print(primefactor)
        } else {
            a = c
        }
    }
}
numberWhosePrimeFactorIsNeeded(600851475143)

只有当我将
var a>=1时,错误才会出现;如果a=0
则不会显示。即使错误存在,代码也会一直运行到最后。

您的Int溢出。以下是
(a,primefactor)
采用的路径:

1 71
71 839
59569 1471
87625999 6857
6857  // <-- a prime factor gets printed, then it continues....
87625999 59569
5219793134431 104441
545160414753108071 486847
171
71 839
59569 1471
87625999 6857

6857//那么解决办法是什么呢。当我尝试使用Int64时也会出现同样的错误。请帮助我也放一个if-else语句,这样它就不会在我得到6857@DrexFranco在
Int64
中无法进行如此大的运算(
Int
与64位平台上的
Int64
大小相同)。您需要一个BigInt类型;斯威夫特在stdlib中没有。您可以查看像osxgmp()或biginger()这样的项目。可能还有其他人。