c progran查找最接近的原初诺贝尔 [英] c progran to find nearest prime nomber
本文介绍了c progran查找最接近的原初诺贝尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
progran查找最接近的素数琥珀色
progran to find nearestprime nomber
推荐答案
从给定的整数n
开始,以降序搜索n
以下的整数和n
以上的整数以上升的方式.这样您将获得两个素数整数.然后确定从n
到每个素数的距离:
Starting from a given integern
search the integers belown
in descending fashion and the integers aboven
in ascending fashion. You will get two prime integers this way. Then you determine the distance fromn
to each of the primes:
distanceToBelow = n - primeBelowN
distanceToAbove = primeAboveN - n
result = primeAboveN
if(distanceToBelow <= distanceToAbove)
result = primeBelowN
实际上,除非n == 2,否则您总是向下搜索,因为2以下最接近的素数是-2,距离为4,而3则是距离3,距离为1. Y < Z并找出为什么在Y> = 3时,最接近Y的素数始终是X.
如果您不确定整数是否为素数,请在此处查看我的解决方案:素数程序 [ ^ ]
干杯!
Actually you''d always search downwards except for when n == 2 because the nearest prime below 2 is -2 with a distance of 4 compared to 3 with a distance of 1. Consider three consecutive primes X < Y < Z and figure out why with Y >=3 the nearest prime to Y is always X.
If you have trouble figuring out whether an integer is prime check my solution here: Prime number program[^]
Cheers!
这篇关于c progran查找最接近的原初诺贝尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文