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

编程入门 行业动态 更新时间:2024-10-24 22:27:52
本文介绍了如何确定所有小于给定输入值的素数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任何人都可以使用带有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); } } }

解决方案

您的代码问题是int i = 0以0开头,并下一行if (N%i==0),因此10/0不可能抛出类似java.lang.ArithmeticException: / by zero的错误不可能

然后循环遍历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 原因

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 请参见

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); }

Can anyone help me to determine all prime numbers smaller than a given input value using scanner with java8 .

Input: N integer> 0

Output: the table with prime numbers.

Example: for N = 10 the Output is : 2 3 5 7

this is my work so far:

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); } } }

解决方案

your code problem is int i = 0 start with 0 and next line if (N%i==0) so 10/0 is not possible throw a error something like java.lang.ArithmeticException: / by zero is not possible

and you loop through result.length, you need to loop through i your parent loop and put condition inside if (N%i==0) and you need many changes saw my below answer and debug where you get unexpected output and follow.

brute Force

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); }

optimize one using 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); }

using 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); }

Updated 1:- that something you want

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); }

更多推荐

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

本文发布于:2023-11-29 23:06:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647903.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:素数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!