-3

我需要用 Java 创建一个程序来确定一个数字是否为素数。

用户输入任意数字,程序将判断它是否为质数,并显示“非质数”或“质数”。我的代码现在可以编译并运行,但它总是说一个数字不是素数,即使它是。

import java.util.Scanner;

public class PrimeNumber
{
 public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
   int constant = 0;
   int variable = 0;
   System.out.println("Enter a Number to test if Prime or Not");
   constant = input.nextInt();
   variable = constant;
   double answer = 0.0;
   answer = testPrime(constant, variable);
   System.out.println(+answer);
   if (answer == 1)
    {
     System.out.println(+constant + " is a prime number.");
    }
   else
     {
     System.out.println(+constant + " is NOT a prime number.");
    }
   }

public static double testPrime(int number, int divide)
 {
  double prime = 0.0;
  prime = number%divide;
  if (prime > 0 && divide != number)
   {
    return testPrime(number, divide - 1);
   }
   else
   {
    return prime;
   }
 }
}
4

7 回答 7

2
if (prime > 0 && divide != number)

这永远不会是真的。因为你的除数和数字总是相等的。

看到您已分配variable=constant,这就是您传递给方法的内容

constant = input.nextInt();
variable = constant;
answer = testPrime(constant, variable);

也就是说,您需要非常复杂才能确定一个数字是否为素数。检查网络上的简单算法。例如,请参阅http://www.mkyong.com/java/how-to-determine-a-prime-number-in-java/

于 2013-04-26T18:50:59.900 回答
1

我看到你想要递归,所以我将 tgkprog 的答案转换为递归方法(尽管他肯定更有效)。此外,我认为如果输入不是素数,您可能想要返回素数?我只是从 OP 的双精度而不是布尔值的返回值来推测这一点。不过,我的会返回一个 int,因为返回一个 double 是愚蠢的。

int isPrime(int n){ //starter function
      if(n<=1) return n; //sanity check for weird inputs
      if(n % 2 == 0) return 2; //2 is a prime factor
      int start  = (int)java.lang.Math.pow(n, 0.5);
      return isPrime(n,start-(start%2)); //makes start odd if it wasn't already
}

int isPrime(int n, int testval){ //recursive function
       if(testval<=1) return 1; //n is prime, return n since it has no prime factors
       if(n % i == 0) 
           return i; //found a prime factor!
       return isPrime(n,i-2);
}
于 2013-04-27T16:04:31.667 回答
1

不是答案,因为 OP 想要递归(我猜是作业)。

你只需要直到 n 的平方根来查看它是否有一个除数(除 self 之外的除数将 < sqrt(n))

    boolean isPrime(int n) {
            if(n % 2 == 0)return false;
            int till  = (int)java.lang.Math.pow(n, 0.5); //(int)n / 2;
            for(int i= 3;i<till;i+=2) {
                if(n % i == 0) 
                    return false;
            }
            return true;
        }
于 2013-04-26T19:10:46.960 回答
0

递归

import java.util.Scanner;

public class PrimeRecursion
{
    static int dbg;
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter non 0 if you want debug :");
        dbg = input.nextInt();
        long constant = 3;
        long variable = 0;
        while(true){
            System.out.println("Enter a Number to test if Prime or Not (1 to exit)");
            constant = input.nextLong();
            if(constant < 3){
                if(constant == 2){
                    System.out.println("2 is a special prime");
                }
                return;
            }
            if(constant % 2 == 0){
                System.out.println(constant + " is NOT a prime number, even number.");
            }else{
                variable = (long)Math.pow(constant, 0.5);
                variable = (variable % 2 == 1) ? variable : variable + 1;//odd number
                double answer = 0.0;
                answer = testPrime(constant, variable);
                System.out.println("End answer : " + answer);
                if (answer == 1){
                    System.out.println(+constant + " is a prime number. answer : "  + answer);
                }
                else{
                    System.out.println(constant + " is NOT a prime number.answer : "  + answer);
                }
            }
            System.out.println();
        }
    }

    static double testPrime(long number, long divide)
    {
        double prime = 0.0;
        prime = (double)number / divide;
        if(dbg > 0){
            System.out.println("Debug number " + number + " | divide " + divide + " |prime : "  + prime + " | Math.abs(prime) " + Math.abs(prime));
        }
        if (prime == ((long)prime))//whats the best way to do this?
        {
            //divided evenly
            return divide;
        }
        else{
            return testPrime(number, divide - 2);
        }
    }
}
于 2013-04-27T21:12:58.037 回答
-1

好吧,我直接给你所有的代码,而不是写代码片段。希望你们都喜欢这个,因为我已经尽力让它尽可能简单。代码是:>

import java.util.*;
class Prime_Number
{
    static int c=0;
    public static void main(String args[])
    {
        int n,i,sq=0;
        Scanner in=new Scanner(System.in);
        Prime_Number ob=new Prime_Number();
        System.out.print("Enter a no.:>");
        n=in.nextInt();
        i=n;
        sq=(int)(Math.sqrt(n));//square root is been taken since a no. cannot have factors greater than its square root
        int k=ob.prime_Chk(n,i,sq);
           if(k==1)
              {
                 System.out.println("Prime");
              }
                 else
                     {
                       System.out.println("Non-Prime");
                      }
      }
public int prime_Chk(int g,int i,int sq)
    {
        if(i==sq)
        {
            return c;
        }
        else
        {
            if(g%i==0)
            {
                c++;
            }
            i--;
            return(prime_Chk(g,i,sq));
        }
    }
}

好吧,在 prime() 中,我将 int i 、 int sq 和 int g 作为参数。如果您愿意,您也可以使用其他变量来代替这些变量。

于 2014-08-15T12:49:02.983 回答
-1

递归函数对我来说就像 - 如果我错了,请纠正我。谢谢。调用语句 >Boolean b=isPrime(number,number-1); 递归函数-

void isPrime(int p,int d);
{
int prime=p%d;
if((p==0&&d>1)||p%2==0)
return true;//that the number is not prime
if(d==1)
return false;
else
return isPrime(p,d-2);//calls the function again
}
于 2013-11-01T14:46:05.030 回答
-1
import java.util.Scanner;

public class HW8R_T03 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number: ");
        int n = sc.nextInt();
        int simple = prime(n,n-1);
        System.out.println(simple==1 ? "prime" : "not prime");

    }

    static int prime(int x, int y){
        int div = 1;
        if (y==0 || y==1){
             return div;
        }
        if (x%y==0){
            div = y;
        } else {
            div = prime (x, --y);
        }
        return div;
    }


}
于 2015-11-24T15:05:32.353 回答