得到平方根和最近的素数

本文关键字:最近 平方根 | 更新日期: 2023-09-27 17:56:12

>我正在尝试制作一个程序来获取输入的平方根和输入最接近的素数。我无法弄清楚在获得最接近的素数时可能出现的问题是什么,但它没有返回正确答案。

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();
    }
}
}

得到平方根和最近的素数

我建议首先提取方法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));

正如建议的那样,您应该将一些逻辑放在辅助方法中,这里有一个简单的解决方案,用于获取数字的平方根以及下一个和最后一个素数。

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;
        }
    }