progran查找最接近的原初诺贝尔

progran查找最接近的原初诺贝尔

本文介绍了c progran查找最接近的原初诺贝尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

progran查找最接近的素数琥珀色

progran to find nearestprime nomber

推荐答案

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.

如果您不确定整数是否为素数,请在此处查看我的解决方案:素数程序 [ ^ ]

干杯!

—MRB



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!

—MRB


这篇关于c progran查找最接近的原初诺贝尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:41