得到素数

时间:2012-07-01 08:09:02

标签: java math

请查看以下代码

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之间的所有素数。这是从一个非常大的数字中获取数千个素数的测试案例。但是,它没有显示唯一的素数,它显示每个数字!

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

6 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);
        }
    }
}

答案 2 :(得分:0)

使用筛子,EratosthenesAtkins

以下是Robert Sedgewick在Java中实施的Eratosthenes:

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

答案 3 :(得分: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++;

        }

    }
}

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

答案 4 :(得分: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);
   }
}

它将显示所有素数。

答案 5 :(得分:0)

以最小迭代次数查找素数

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

}