如何在Go语言中找到第n个回文素数

huangapple go评论66阅读模式
英文:

How to find nth prime palindrome in Go

问题

我在Go语言方面还是新手,我试图找到第n个回文素数,但它总是返回0,我在这里做错了什么?我的代码输出应该是131。

为了清楚起见,下面我将给出n的示例及其返回值。

n: 1,输出:2

n: 2,输出:3

n: 3,输出:5

n: 5,输出:11

n: 6,输出:101

n: 7,输出:111

n: 8,输出:151

n := 7

counter := 0

currNum := 1

palindromeNumber := 0

for counter < int(n) {
	currNum++
	isPrime := false
	for i := 2; i <= int(math.Sqrt(float64(currNum))); i++ {
		if currNum%i == 0 {
			isPrime = true

			var number, remainder, temp int

			var reverse int = 0

			temp = currNum

			// For Loop used in format of While Loop
			for {
				remainder = number % 10
				reverse = reverse*10 + remainder
				number /= 10
				if number == 0 {
					// Break Statement used to exit from loop
					break
				}
			}

			if temp == reverse {
				palindromeNumber = reverse
			}

			break
		}
	}
	if !isPrime {
		counter++
	}
}

return int64(palindromeNumber)
英文:

I am kinda new in Go, I tried to find nth prime palindrome numbers but it always return 0, what did I do wrong here, output from my code should be 131.

For the sake of clearance, below I'll give sample of n and its return.

n: 1, Output: 2

n: 2, Output: 3

n: 3, Output: 5

n: 5, Output: 11

n: 6, Output: 101

n: 7, Output: 111

n: 8, Output: 151

n := 7

counter := 0

currNum := 1

palindromeNumber := 0

for counter &lt; int(n) {
	currNum++
	isPrime := false
	for i := 2; i &lt;= int(math.Sqrt(float64(currNum))); i++ {
		if currNum%i == 0 {
			isPrime = true

			var number, remainder, temp int

			var reverse int = 0

			temp = currNum

			// For Loop used in format of While Loop
			for {
				remainder = number % 10
				reverse = reverse*10 + remainder
				number /= 10
				if number == 0 {
					// Break Statement used to exit from loop
					break
				}
			}

			if temp == reverse {
				palindromeNumber = reverse
			}

			break
		}
	}
	if !isPrime {
		counter++
	}
}

return int64(palindromeNumber)

答案1

得分: 1

你的主要问题是变量number。你创建了它,也许你想在使用number计算reverted之前将currNum复制到number中,但你没有这样做。

英文:

Your main problem is the variable number. You create it and maybe you wanted to copy currNum into number before using number to calculate the reverted, but you are not doing that.

答案2

得分: 1

质数是只有两个因数的数:1和它本身。这意味着如果一个数n是质数,它不能被从2sqrt(n)范围内的任何数整除。但是在你的代码中,我看到了以下内容:

if currNum%i == 0 {
        isPrime = true
        ...
}

如果currNum可以被从2sqrt(n)范围内的i整除,为什么它是质数呢?这是错误的。如果currNum满足这个条件,它必定是一个非质数。
此外,我没有看到对var number的赋值,所以它将是它的零值0,这也导致了palindromeNumber = 0

我认为你应该将质数检查和回文检查放在两个单独的函数中,以避免代码中的混淆(例如func isPrimefunc isPalindrome)。

英文:

Prime numbers are numbers that have only 2 factors: 1 and themselves, that means if a number n is a prime number, it won't be divisible by any numbers in range from 2 to sqrt(n). But in your code, I see

if currNum%i == 0 {
        isPrime = true
        ...
}

Why is currNum prime if it's divisible by i with i in range from 2 to sqrt(n) ? It's wrong. If currNum meets this condition, it have to be a non-prime number.
Moreover, I don't see the assignment for var number, so it will be its zero value 0 that also make palindromeNumber = 0.

I think you should keep the prime check and the palindrome check in two separate functions to avoid confusions in your code (such as func isPrime and func isPalindrome).

huangapple
  • 本文由 发表于 2022年4月28日 18:22:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/72042015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定