web-dev-qa-db-ja.com

Java:素数を数える

だから私はこのコードを書きました、そして私は長い間コーディングしていないのでそれを誇りに思っています。それが何をするか、それは数を求めて、それから1からその数まであるすべての素数を印刷します。

import Java.util.Scanner;
class PrimeNumberExample {

    public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the number till which prime number to be printed: ");
        int limit = new Scanner(System.in).nextInt();

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        for(int number = 2; number<=limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number);
            }
        }

    }

    /*
     * Prime number is not divisible by any number other than 1 and itself
     * @return true if number is prime
     */
    public static boolean isPrime(int number){
        for(int i=2; i<number; i++){
            if(number%i == 0){
                return false; //number is divisible so its not prime
            }
        }
        return true; //number is prime now
    }
}

しかし、私がやりたいのは、数を求め、10を取り、最初の10の素数を印刷することです。方法を見つけることができるかどうかを確認しようとしましたが、方法がわからないので、未使用Javaそんなに。あなたが私を助けてくれることを願っています。

8
rareinator

これまでに印刷された素数の数を数えるだけです。この数が10を超える場合は、停止します。ループは次のようになります。

for(int number = 2; number<=limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number);
                count++; 
            }
        }

コード全体:

import Java.util.Scanner;
class PrimeNumberExample {

    public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the amount of prime numbers to be printed: ");
        int limit = new Scanner(System.in).nextInt();
        int count=0;

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        for(int number = 2; number<=limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number);
                count++; 
            }
        }

    }

    /*
     * Prime number is not divisible by any number other than 1 and itself
     * @return true if number is prime
     */
    public static boolean isPrime(int number){
        for(int i=2; i<number; i++){
            if(number%i == 0){
                return false; //number is divisible so its not prime
            }
        }
        return true; //number is prime now
    }
}
6
laaposto

これを試して:

public static void main(String[] args) throws Exception {

    // get input till which prime number to be printed
    System.out.println("Enter the number till which prime number to be printed: ");
    int limit = new Scanner(System.in).nextInt();

    // printing primer numbers till the limit ( 1 to 100)
    System.out.printf("Printing first %d prime numbers\n", limit);
    for (int number = 2; limit > 0; number++) {
        if (isPrime(number)) {
            System.out.println(number);
            limit--;
        }
    }
}

/*
 * Prime number is not divisible by any number other than 1 and itself
 * 
 * @return true if number is prime
 */
public static boolean isPrime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return false; // number is divisible so its not prime
        }
    }
    return true; // number is prime now
}
2
Harmlezz

この方法でもお試しいただけますか。

public static void main(String args[]) {

    //get input till which prime number to be printed
    System.out.println("Enter the number till which prime number to be printed: ");
    int limit = new Scanner(System.in).nextInt();

    //printing primer numbers till the limit ( 1 to 100)
    System.out.println("Printing prime number from 1 to " + limit);
    int number = 2;
    for(int i = 0; i < limit;){         
        //print prime numbers only
        if(isPrime(number)){
            System.out.println(number);
            i++;
        } 
        number = number + 1;
    }

}

/*
 * Prime number is not divisible by any number other than 1 and itself
 * @return true if number is prime
 */
public static boolean isPrime(int number){
    for(int i=2; i<number; i++){
        if(number%i == 0){
            return false; //number is divisible so its not prime
        }
    }
    return true; //number is prime now
}
2
Wundwin Born

これが必要なことをすることができる1つの方法です.....私は一定の10として制限を保ちました。あなたはそれをユーザーからも読むことができます。

public class PrimeNumberExample {

    public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the number till which prime number to be printed: ");
        int limit = 10;//new Scanner(System.in).nextInt();

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        int number = 0;
        while(true){
            if(isPrime(++number)){
                System.out.println(number);
                if(--limit <= 0)
                    break;
            }
        }

    }

    /*
     * Prime number is not divisible by any number other than 1 and itself
     * @return true if number is prime
     */
    public static boolean isPrime(int number){
        for(int i=2; i<(number/2); i++){
            if(number%i == 0){
                return false; //number is divisible so its not prime
            }
        }
        return true; //number is prime now
    }
}
2
Airan

これを試してみてください。非常に簡単に、すべての素数である印刷されている数を数えることができます。

  public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the number till which prime number to be printed: ");
        int limit = new Scanner(System.in).nextInt();

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        int count = 0;
        for(int number = 2; count<limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                count++;
                System.out.println(number);
            }
        }

    }
1

これを試してみてください。Java 8 !!!

public static void main(String[] args) {
        Integer maxVal = 100;
        IntStream.iterate(2, i -> ++i)
                .parallel()
                .filter(i -> !IntStream.rangeClosed(2, i/2).anyMatch(j ->  i%j == 0))
                .limit(maxVal)
                .forEach(System.out::println);
    }
0
Ankit Agarwal
public boolean isPrime(long pNo) {
    if(pNo > 9) {
        long unitDigit = pNo % 10;
        if(unitDigit == 0 || unitDigit%2 == 0 || unitDigit == 5) {
            return false;
        } else {
            for (long i=3; i < pNo/2; i=i+2) {
                if(pNo%i == 0) {
                    return false;
                }
            }
            return true;
        }
    } else if(pNo < 0) {
        return false;
    }
    else {
        return pNo==2 || pNo==3|| pNo==5 || pNo==7;
    }
}
public int getPrimeNumberCount(long min, long max) {
    int count = 0;
    if(min == max) {
        System.out.println("Invalid range, min and max are equal");
    } else if(max < min || min < 0 || max < 0) {
        System.out.println("Invalid range");
    } else {
        for (long i = min; i <= max; i++) {
            if (isPrime(i) && i > 0) {
                // System.out.println(i);
                count++;
            }
        }
    }
    return count;
}
0
user2170172