我正在尝试创建一个程序来获取输入的平方根和输入的最接近的素数。我无法弄清楚当得到最接近的素数时会出现什么问题,但它没有返回正确的答案。
using System;
namespace SqrtPrime
{
class SqrtPrime
{
static void Main(string[] args)
{
int num, ctr;
Console.Write("Enter number: ");
num = int.Parse(Console.ReadLine());
int[] inp = new int[num];
for (ctr = 0; ctr < num; ctr++)
{
Console.Write("\n\nEnter number {0}: ", ctr + 1);
inp[ctr] = int.Parse(Console.ReadLine());
double sqrt = Math.Sqrt(inp[ctr]);
Console.Write("\nSquare root: {0} ", sqrt);
int cnvt = inp[ctr];
Console.Write("\nNearest prime number/s: ");
int i, Prime1 = 0, Prime2 = 0;
bool checkPrime1 = true, checkPrime2 = true;
while (checkPrime1)
{
cnvt--;
for (i = 2; i <= cnvt; i++)
{
if (cnvt % i == 0)
{
break;
}
}
if (i == cnvt)
{
checkPrime1 = false;
Prime1 = cnvt;
break;
}
}
while (checkPrime2)
{
cnvt++;
for (i = 2; i <= cnvt; i++)
{
if (cnvt % i == 0)
{
break;
}
}
if (i == cnvt)
{
Prime2 = cnvt;
checkPrime2 = false;
break;
}
}
if (Prime2 - cnvt > cnvt - Prime1)
{
Console.WriteLine(Prime1);
}
else if (Prime2 - cnvt < cnvt - Prime1)
{
Console.WriteLine(Prime2);
}
else if (Prime2 - cnvt == cnvt - Prime1)
{
Console.WriteLine(Prime1 + ", " + Prime2);
}
}
Console.ReadLine();
}
}
}
答案 0 :(得分:5)
我建议提取方法,首先是IsPrime
public static bool IsPrime(int value) {
if (value <= 1)
return false;
else if (value % 2 == 0)
return value == 2;
int n = (int) (Math.Sqrt(value) + 0.5);
for (int i = 3; i <= n; i += 2)
if (value % i == 0)
return false;
return true;
}
然后primeBelow
和primeAbove
private static int primeBelow(double value) {
int n = (int) value;
for (int i = n; i >= 2; --i)
if (IsPrime(i))
return i;
return -1; // technical value, e.g. there's no prime below for sqrt(3)
}
private static int primeAbove(double value) {
int n = (int) value;
for (int i = n; ; ++i)
if (IsPrime(i) && (i > value))
return i;
return -1;
}
最后,NearestPrime
private static string NearestPrime(double value) {
int below = primeBelow(value);
int above = primeAbove(value);
if (below < 0)
return above.ToString();
if (Math.Abs(value - below) < Math.Abs(value - above))
return below.ToString();
else if (Math.Abs(value - below) > Math.Abs(value - above))
return above.ToString();
else
return string.Format("{0}, {1}", below, above);
}
然后使用它们
...
double sqrt = Math.Sqrt(inp[ctr]);
Console.Write("\nSquare root: {0} ", sqrt);
Console.WriteLine(NearestPrime(sqrt));
答案 1 :(得分:0)
正如所建议的那样,你应该把你的一些逻辑放在辅助方法中,这里是一个简单的解决方案,可以获得数字的平方根以及下一个和最后一个素数。
class SqrtPrime
{
static void Main(string[] args)
{
Console.WriteLine("Enter number: ");
int number = int.Parse(Console.ReadLine());
Console.WriteLine("Square Root is: " + Math.Sqrt(number) + "\n");
Console.WriteLine("Next Prime is " + CheckPrimeUpwards(number) + "\n");
Console.WriteLine("Last Prime was " + CheckPrimeDownwards(number) + "\n");
Console.ReadLine();
}
static int CheckPrimeDownwards(int number)
{
int result = number;
while(!IsNumberAPrime(result))
{
result--;
}
return result;
}
static int CheckPrimeUpwards(int number)
{
int result = number;
while (!IsNumberAPrime(result))
{
result++;
}
return result;
}
public static bool IsNumberAPrime(int number)
{
int boundary = (int)Math.Floor(Math.Sqrt(number));
if (number < 2)
{
return false;
}
if (number == 2)
{
return true;
}
for (int i = 2; i <= boundary; ++i)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
}