-4

请看下面的代码

public class Prime
{
    public static void main(String[]args)
    {
        int i = 2; 
        int counter = 0;

        while(true)
        {


            if(counter==6)//Count still 6
            {
                break;
            }
            else
            {
                if(getPrimes(i)==true)
                {

                    i++;
                    counter++;
                    System.out.println("Counter: "+counter);
                }
                else
                {
                    System.out.println("No");
                }
            }
        }
    }

    static boolean getPrimes(int num)
    {

        boolean result = false;
        int i = 2;

            while(true)
            {
                if((num%i) != 0) //if the number cannot be divided by any other number (except 1 and it self) it is prime
                {
                    result = true;
                    System.out.println(num);
                    System.out.println("I is: "+i);
                    i=2;
                    break;
                }

                else //Not a prime. Repeat the process
                {
                    result = false;
                    i++;
                }
            }

            return result;
     }       
}

在这里,我试图获得 0-6 之间的所有素数。这是从一个非常大的数字中获取数千个素数的测试用例。但是,它不是显示唯一的素数,而是显示每一个数字!

我在这里做错了什么?请帮忙!

4

6 回答 6

4

试试这个答案......在你的循环中

static boolean getPrimes(int num)
{

    boolean result=true;     // incase u gave 1 or 2 as input.....
    int i = 2;
    int mid=num/2; 

    while(i<mid)
    {
         if(num%i==0)
         {
               result=false;   // not a prime and breaks...
               break;
         }
         else
         {
               result=true;    // the loop make result as true.....  

         }
         i++;
     }
     return result;
}
于 2012-07-01T08:27:54.973 回答
2

我认为你需要这样的东西:

public void getPrimes(int a){
    for(int i = 2; i < a; i++){
        int inCounter = 0;
        if(counter%i==0){
            System.out.println("false");
            inCounter++;
        }
        if(inCounter == 0){
            System.out.println("Prime: "+counter);
        }
    }
}
于 2012-07-01T08:16:22.503 回答
0

我想我找到了解决方案。至少,我找到了我需要的答案。这是我的答案

import java.math.BigInteger;

public class Problem7
{
    public static void main(String[]args)
    {
        int i = 0;
        int counter = 0;

        while(true)
        {

            if(i>6)
            {
                break;
            }

            if(i>1)
            {
                String str = String.valueOf(i);

                if (new BigInteger(str).isProbablePrime(i/2))
                {
                    System.out.println(str);
                     counter++;
                }
            }
            i++;

        }

    }
}

我想这是最简单的方法...

于 2012-07-01T17:51:46.893 回答
0

使用筛子,EratosthenesAtkins

这是 Robert Sedgewick 在 Java 中的 Eratosthenes 实现:

http://introcs.cs.princeton.edu/java/14array/PrimeSieve.java.html

于 2012-07-01T14:05:42.113 回答
0
import java.util.Scanner;
class PrimeNumbers2
{
   public static void main (String[] args)
   {        
      Scanner scanner = new Scanner(System.in);
      int i =0;
      int num =0;
      //Empty String
      String  primeNumbers = "";
      System.out.println("Enter the value of n:");
      int n = scanner.nextInt();
      for (i = 1; i <= n; i++)         
      {                   
         int counter=0;           
         for(num =i; num>=1; num--)
         {
        if(i%num==0)
        {
        counter = counter + 1;
        }
     }
     if (counter ==2)
     {
        //Appended the Prime number to the String
        primeNumbers = primeNumbers + i + " ";
     }  
      } 
      System.out.println("Prime numbers from 1 to n are :");
      System.out.println(primeNumbers);
   }
}

它将显示所有质数。

于 2016-02-02T08:46:54.653 回答
0

找到具有最小迭代的素数

布尔 IsPrimeNumber(int num) {

    boolean isPrime = true;
    if (num == 1 || num ==0)
        isPrime = false;
    else{

        int limit = (int) Math.sqrt(num);
        for (long i = 2L; i <=limit ; i++)
            if (num % i == 0) {
                isPrime = false;
                break;
            }
    }

    return isPrime;

}
于 2017-02-27T05:04:16.347 回答