接受 2 个正数并打印中间每个素数的代码有问题吗? - Swift
Issue with code that takes in 2 positive numbers and prints every prime number in between? - Swift
我正在尝试编写一个函数,它接受 2 个正整数并打印它们之间的每个素数,并且还包括这 2 个数字(如果它们是素数)。这就是我目前所拥有的,但它只会打印第一个数字是质数,其余的都不是。我在网上查过这个,但我似乎无法弄清楚这段代码有什么问题,因为许多其他网站都使用类似的逻辑。
func printPrimes(from firstNum: Int, to lastNum: Int) {
var newFirstNum = firstNum
if firstNum == 0 {
newFirstNum = 2
}
let numbers = newFirstNum...lastNum
// Goes through each number from 2-100 starting from 2.
for number in numbers {
var primeValue = false
let startPoint = 2
let number2 = startPoint...number
// Takes current number and divides by every number from 2 to the given number.
for currentIndex in number2 {
primeValue = false
if number % currentIndex != 0 {
primeValue = true
}
}
if primeValue == false {
print("\(number) is not prime")
} else {
print("\(number) is prime")
}
}
}
这是我正在使用的函数调用类型:
printPrimes(from: 0, to: 100)
如有任何帮助,我们将不胜感激。
几个问题。主要是你设置的 primeValue
都是错误的。您首先将其设置为 false。然后,即使您已经确定该数字是质数,您也可以为您将要尝试的每个因素将其重置为 false。
在找到一个因数之前最好假设这个数是质数。你需要避免检查一个数字本身。
这是您的固定代码:
func printPrimes(from firstNum: Int, to lastNum: Int) {
var newFirstNum = firstNum
if firstNum == 0 {
newFirstNum = 2
}
let numbers = newFirstNum...lastNum
// Goes through each number from 2-100 starting from 2.
for number in numbers {
var primeValue = true // assume prime unless we find a factor
let startPoint = 2
let number2 = startPoint..<number // Don't include number
// Takes current number and divides by every number from 2 to the given number.
for currentIndex in number2 {
if number % currentIndex == 0 {
primeValue = false // we found a factor, not prime
break // no need to check for any other factors
}
}
if primeValue {
print("\(number) is prime")
} else {
print("\(number) is not prime")
}
}
}
printPrimes(from: 0, to: 100)
我正在尝试编写一个函数,它接受 2 个正整数并打印它们之间的每个素数,并且还包括这 2 个数字(如果它们是素数)。这就是我目前所拥有的,但它只会打印第一个数字是质数,其余的都不是。我在网上查过这个,但我似乎无法弄清楚这段代码有什么问题,因为许多其他网站都使用类似的逻辑。
func printPrimes(from firstNum: Int, to lastNum: Int) {
var newFirstNum = firstNum
if firstNum == 0 {
newFirstNum = 2
}
let numbers = newFirstNum...lastNum
// Goes through each number from 2-100 starting from 2.
for number in numbers {
var primeValue = false
let startPoint = 2
let number2 = startPoint...number
// Takes current number and divides by every number from 2 to the given number.
for currentIndex in number2 {
primeValue = false
if number % currentIndex != 0 {
primeValue = true
}
}
if primeValue == false {
print("\(number) is not prime")
} else {
print("\(number) is prime")
}
}
}
这是我正在使用的函数调用类型:
printPrimes(from: 0, to: 100)
如有任何帮助,我们将不胜感激。
几个问题。主要是你设置的 primeValue
都是错误的。您首先将其设置为 false。然后,即使您已经确定该数字是质数,您也可以为您将要尝试的每个因素将其重置为 false。
在找到一个因数之前最好假设这个数是质数。你需要避免检查一个数字本身。
这是您的固定代码:
func printPrimes(from firstNum: Int, to lastNum: Int) {
var newFirstNum = firstNum
if firstNum == 0 {
newFirstNum = 2
}
let numbers = newFirstNum...lastNum
// Goes through each number from 2-100 starting from 2.
for number in numbers {
var primeValue = true // assume prime unless we find a factor
let startPoint = 2
let number2 = startPoint..<number // Don't include number
// Takes current number and divides by every number from 2 to the given number.
for currentIndex in number2 {
if number % currentIndex == 0 {
primeValue = false // we found a factor, not prime
break // no need to check for any other factors
}
}
if primeValue {
print("\(number) is prime")
} else {
print("\(number) is not prime")
}
}
}
printPrimes(from: 0, to: 100)