简体   繁体   English

Java程序显示素数

[英]Java program showing primes

I would like to have a program that show range in specific range and show "no primes" once only if there is no primes in that range like 24 to 28. 我希望有一个程序可以显示特定范围内的范围,并且仅当该范围内没有质数(例如24到28)时显示一次“无质数”。

int count=0;

for(prime = lowerLimit ;prime < upperLimit; prime++) 
{   
    count=0;                 
    for(divisor = 2; divisor <prime; divisor++)

        {
           if(prime % divisor== 0)                            

                      count++;

    }

if(count==0)
System.out.println(prime);
}

if (count>0)
System.out.println("nope"); 

I have tried to put 我试图把

if (count>0)
System.out.println("nope"); 

outside the loop,however it also prints when the range having primes. 在循环外,但是当范围为质数时,它也会打印。 How can i tackle it? 我该如何解决?

Keep one extra variable like noOfPrime , which will count the number of prime number with in a range. 保留一个额外的变量,例如noOfPrime ,该变量将计算一个范围内的素数的数量。 And increase by 1 if you found any prime, so that out side of loop you could determine the number prime number as well there is any prime number or not. 如果发现任何质数,则加1,这样就可以在循环的外面确定质数是否也有质数。

int count = 0;
int noOfPrime = 0;
...
for(prime = lowerLimit ;prime < upperLimit; prime++){
    ...
    if(count==0){
       System.out.println(prime);
       noOfPrime+=1;
    }
}
if(noOfPrime >0)
    System.out.println("no primes);

First of all, your method for detecting primes is terrible. 首先,您检测质数的方法很糟糕。 It works, but it's super slow. 它可以工作,但是非常慢。 I'd suggest you look into using sieves if you want to improve that inner loop. 如果您要改善内循环,建议您使用筛子。

Secondly, what exactly are you trying to count? 其次,您到底想算什么? Right now your count variable stores the amount of divisors a number has, and then you set it to zero when you check the next number. 现在,您的count变量存储一个数字具有的除数,然后在检查下一个数字时将其设置为零。 How is that going to tell you anything about how many primes you have in a certain range? 这将如何告诉您有关在一定范围内有多少个素数的任何信息? You can just do something like this: 您可以执行以下操作:

notPrime = false;
for(prime = lowerLimit ;prime < upperLimit; prime++) 
{                    
    for(divisor = 2; divisor <prime; divisor++)
    {
        if(prime % divisor== 0){
            notPrime = true;
            break;
    }
    if(notPrime)
        break;
}

if(notPrime) System.out.println("There's a prime");

You could design a function to determine if a number is prime something like: 您可以设计一个函数来确定数字是否为质数,例如:

//checks whether an int is prime or not.

boolean isPrime(int n) {
    //check if n is a multiple of 2
    if (n%2==0) return false;
    //if not, then just check the odds
    for(int i=3;i*i<=n;i+=2) {
        if(n%i==0)
            return false;
    }
    return true;
}

and within a for loop, you call the function to each element of the interval: 在for循环中,您可以对间隔的每个元素调用该函数:

for(int i=lowerLimit;i<=upperLimit;i++){      
  if (!(isPrime(i))){
     System.out.println("nope");
     break;
  }
}

Sorry if I have some syntactic errr, I replied from the cell phone. 抱歉,如果我有一些语法错误,我从手机中回复。

Everytime you get to the end of the outer loop and count is still 0, it means that you've found a prime number. 每当您到达外循环的末尾并且count仍然为0时,这意味着您已经找到了质数。 So if this happens even once, then you're not going to print "nope" at the end. 因此,即使这种情况发生一次,也不会在最后打印“ nope”。 Use a boolean variable to keep track of whether or not you've seen a prime. 使用boolean变量来跟踪您是否看过素数。 Since this is homework, I'll let you figure out exactly how to use it. 由于这是家庭作业,因此我将让您确切地知道如何使用它。 Hint: declare the boolean above both loops. 提示:在两个循环上方声明boolean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM