如何确定一个数字是否为素数 [英] How to determine if a number is prime

查看:184
本文介绍了如何确定一个数字是否为素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的问题不在于如何弄清楚一个数字是否是主要的,因为我认为我想到了,但更多的是如何让它正常显示。



这是我的代码:

  public static void main(String [] args){
//声明变量
int randomNumbers = 0;
int sum = 0;
//循环生成数字并打印出数字
System.out.print(五个随机数字为:); (int i = 0; i< = 4; i ++)
{
randomNumbers =(int)(Math.random()* 20)
sum + = randomNumbers;

if(i == 4){
System.out.println(and+ randomNumbers +。
}
else {
System.out.print(randomNumbers +,);
}
}
//显示Sum
System.out.println(\\\
这五个数字之和为+ sum +.\\\
);

//确定总和是素数,并显示结果
(int p = 2; p if(sum%p == 0 )
System.out.println(总和不是素数。);
else
System.out.println(sum是素数);
break;
}
}


}

现在我的问题是,如果这个数字最终是像9这样的话,那么它会说是一个素数,而不是。我认为这个问题是,在一个循环之后,休息是停止的,所以它不会增加变量p,所以它只是测试除以2(我认为)。但是,如果我删除断点,它将打印出总和/不是一个素数,每一次通过,直到它退出循环。不确定在这里做什么。

解决方案

您的方法是查找您的号码是否为素数是正确的方法。
为了使它不一致地打印出数字是否为素数,您可以有一个外部变量,表示数字是否为素数。



  boolean prime = true; $($ p $ 2); 
for(int p = 2; p if(sum%p == 0){
prime =
break;
}
}
if(prime)
System.out.println(sum是素数);
else
System.out.println(总和不是素数);

通过这种方法,程序将假定该数字是素数,直到证明是错误的。所以当它发现它不是素数时,将变量设置为false并突破循环。



然后循环完成后,您只需要打印是否数字是素数。



你可以使这个循环更快的一种方法是从p = 2到p =平方根的总和。所以使用这个方法你的for循环将如下所示:

  double sq = Math.sqrt((double)sum); $ p 



$ / code $ $ p>

希望这有助于


Okay my issue is less of how to figure out if a number is prime, because I think I figured that out, but more of how to get it to display properly.

Here's my code:

public static void main(String[] args) {
    // Declare Variables
    int randomNumbers = 0;
    int sum = 0;
    //Loop for number generation and print out numbers
    System.out.print("The five random numbers are: ");
    for (int i = 0; i <= 4; i++)
    {
        randomNumbers = (int)(Math.random()*20);
        sum += randomNumbers;

        if (i == 4) {
            System.out.println("and " + randomNumbers + ".");
        }
        else {
            System.out.print(randomNumbers + ", ");
        }
    }
    //Display Sum
    System.out.println("\nThe sum of these five numbers is " + sum + ".\n");

    //Determine if the sum is prime and display results
    for(int p = 2; p < sum; p++) {
        if(sum % p == 0)
            System.out.println("The sum is not a prime number.");
        else 
            System.out.println("The sum is a prime number.");
        break;
        }
    }


}

Now my problem is, if the number ends up being something like 9, it'll say it is a prime number, which it is not. I think the issue is that the break is stopping it after one loop so it's not incrementing variable p so it's only testing dividing by 2 (I think). But if I remove the break point it will print out "The sum is/is not a prime number" on every pass until it exits the loop. Not sure what to do here.

解决方案

Your method for finding if your number is prime is the correct method. To make it so that it does not consistently print out whether or not the number is prime, you could have an external variable, which represents whether or not the number is prime.

Such as

    boolean prime = true;
    for (int p = 2; p < sum; p++) {
        if (sum % p == 0) {
            prime = false;
            break;
        }
    }
    if (prime)
        System.out.println("The sum is a prime number.");
    else
        System.out.println("The sum is not a prime number.");

By doing this method the program will assume the number is prime until it proves that wrong. So when it finds it is not prime it sets the variable to false and breaks out of the loop.

Then after the loop finishes you just have to print whether or not the number was prime.

A way that you could make this loop faster is to go from when p = 2 to when p = the square root of sum. So using this method your for loop will look like this:

    double sq = Math.sqrt((double)sum);
    for (int p = 2; p < sq; p++) {
        //Rest of code goes here
    }

Hope this helps

这篇关于如何确定一个数字是否为素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆