如何确定所有小于给定输入值的素数?

时间:2019-03-26 09:42:25

标签: java algorithm math

任何人都可以使用带有java8的扫描器来帮助我确定所有小于给定输入值的素数。

  

输入:N个整数> 0

     

输出:带有质数的表。

     

示例:对于N = 10,输出为:2 3 5 7

这是我到目前为止的工作

class Main {
public static void main(String[] args) {
    int N;
    int[] result = null;

    try (Scanner scanner = new Scanner(new File(args[0]))) {
        N = Integer.parseInt(scanner.nextLine());

          for (int i = 0; i < (N/2)+1; i++) {
            if (N%i==0)
                result[i]=i;
        for (int j = 0; j < result.length; j++) {
            System.out.print(result[j]);
            if (j < result.length - 1) {
                System.out.print(" ");
            }
        }
        }
        System.out.println();
    }
    catch (FileNotFoundException ex) {
        throw new RuntimeException(ex);
    }
 }
}

3 个答案:

答案 0 :(得分:2)

您的代码问题是int i = 00和下一行if (N%i==0)开头,因此不可能10/0抛出诸如java.lang.ArithmeticException: / by zero之类的错误< / p>

,然后遍历result.length,需要遍历i的父循环,并将条件放入if (N%i==0)内,您需要进行许多更改以查看我的以下答案,并调试出乎意料的地方输出并关注。

蛮力

public static void main(String[] args) {
        int N = 50;
        List<Integer> result = new ArrayList<>();
        for (int i = 1; i < N; i++) {
            boolean isPrime = true;
            for (int j = 2; j < i - 1; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                result.add(i);
            }
        }
        result.forEach(System.out::println);

    }

使用Math.sqrt reason

优化一个
public static void main(String[] args) {
        int N = 101;
        List<Integer> result = new ArrayList<>();
        for (int i = 1; i <= N; i++) {
            boolean isPrime = true;
            for (int j = 2; j < Math.sqrt(i - 1); j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                result.add(i);
            }
        }
        result.forEach(System.out::println);
}

使用BigInteger.isProbablePrime see

public static void main(String[] args) {
        int N = 101;
        List<Integer> result = new ArrayList<>();

        for (long i = 1; i <= N; i++) {
            BigInteger integer = BigInteger.valueOf(i);
            if (integer.isProbablePrime(1)) {
                result.add((int) i);
            }
        }
        result.forEach(System.out::println);

    }

已更新1 :-您想要的东西

try (Scanner scanner = new Scanner(new File(args[0]))) {
            int N = Integer.parseInt(scanner.nextLine());
            int[] result = new int[N];
            int resultIncreamenter = 0;
            // here for loop logic can be replaced with above 3 logic
            for (int i = 1; i <= N; i++) {
                boolean isPrime = true;
                for (int j = 2; j < Math.sqrt(i - 1); j++) {
                    if (i % j == 0) {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime) {
                    result[resultIncreamenter++] = i;
                }
            }
            for (int j = 0; j < result.length; j++) {
                System.out.print(result[j]);
                if (j < result.length - 1) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        } catch (FileNotFoundException ex) {
            throw new RuntimeException(ex);
        }

答案 1 :(得分:0)

使用here中的Eratosthenes筛网的算法:

private static int findNumberOfPrimes(int length) {
    int numberOfPrimes = 1;
    if (length == 2) {
        return 1;
    }

    int[] arr = new int[length];
    //creating an array of numbers less than 'length'
    for (int i = 0; i < arr.length; i++) {
        arr[i] = i + 1;
    }
    //starting with first prime number 2, all the numbers divisible by 2(and upcoming) is replaced with -1
    for (int i = 2; i < arr.length && arr[i] != -1; i++) {

        for (int j = i; j < arr.length; j++) {
            if (arr[j] % arr[i] == 0) {
                arr[j] = -1;
                numberOfPrimes += 1;
            }
        }
    }
    return numberOfPrimes;
}

更新

这就是您列出它们的方式:

package primelessthanN;

public class Main {

    public static void main(String[] args) {
        int j;
        int n;
        n = 10;

        for (j = 2; j <=n; j++) {
            if (isPrime(j))
                System.out.println(j);
        }
    }

    private static boolean isPrime(int m) {
        for (int i = 2; i <= sqrt(m); i++) {
            if (m % i == 0)
                return false;
        }
        return true;
    }
}

答案 2 :(得分:0)

尽管可以使用所有功能,但您丢失了概述。

public static boolean isPrime(int n) {
    for (int i = 0; i < (n/2)+1; i++) {
        if (n%i == 0) {
            return false;
        }
    }
    return true;
}

public static void main(String[] args) {
    try (Scanner scanner = new Scanner(new File(args[0]))) {
        int N = Integer.parseInt(scanner.nextLine());

        boolean printed = false;
        for (int j = 2; j < N; j++) {
            if (isPrime(j)) {
                if (printed) {
                    System.out.print(" ");
                }
                System.out.print(j);
                printed = true;
            }
        }
        System.out.println();
    } catch (FileNotFoundException ex) {
        throw new RuntimeException(ex);
    }
}

仅使用isPrime之类的抽象。

现在需要改进(您的results数组):在测试中使用已经找到的素数代替所有数字:

public static boolean isPrime(int n, int[] priorPrimes, int primesCount) {
    for (int p : priorPrimes) {
        if (n%p == 0) {
            return false;
        }
    }
    return true;
}


public static void main(String[] args) {
    try (Scanner scanner = new Scanner(new File(args[0]))) {
        int N = Integer.parseInt(scanner.nextLine());

        int[] primes = new int[N];
        int primesCount = 0;
        boolean printed = false;
        for (int j = 2; j < N; j++) {
            if (isPrime(j)) {
                primes[primesCount] = j;
                ++primesCount;
                if (printed) {
                    System.out.print(" ");
                }
                System.out.print(j);
                printed = true;
            }
        }
        System.out.println();
    } catch (FileNotFoundException ex) {
        throw new RuntimeException(ex);
    }
}