Java 素数搜寻者有自己的想法吗?
我正在做一个素数查找器,它将查找用户输入的给定数字的素数。我现在看到的似乎不是缺少素数,就是在ArrayList中添加了非素数。我的代码对我来说似乎是合乎逻辑的,我不明白为什么会发生这种情况。谁能告诉我我做错了什么?或者是一种更简单的方法(我觉得我太复杂了)?一些错误示例如下:输入21,只有3显示为素数。输入11000、25和55显示(显然不是质数)。提前谢谢Java 素数搜寻者有自己的想法吗?,java,numbers,Java,Numbers,我正在做一个素数查找器,它将查找用户输入的给定数字的素数。我现在看到的似乎不是缺少素数,就是在ArrayList中添加了非素数。我的代码对我来说似乎是合乎逻辑的,我不明白为什么会发生这种情况。谁能告诉我我做错了什么?或者是一种更简单的方法(我觉得我太复杂了)?一些错误示例如下:输入21,只有3显示为素数。输入11000、25和55显示(显然不是质数)。提前谢谢 import java.util.*; public class PrimeFactors { public static void
import java.util.*;
public class PrimeFactors {
public static void main(String args[]) {
long num;
Scanner in = new Scanner(System.in);
System.out.println("\n\n\nThis program finds the prime factors of a given number.\n");
System.out.print("Please enter the number: ");
num = in.nextLong();
System.out.println("\nThe prime factors are: " + primeFactor(num) + "\n");
}
public static ArrayList<Long> primeFactor(long n) {
long output = 0;
long guess = 2;
ArrayList<Long> primeFactors = new ArrayList<Long>();
while (guess <= n) {
long primes = 0;
long i = 2;
long x = 0;
long rt = 1;
long duplicate = 0;
output = n % guess;
// Finds the sqrt.
while (x <= n) {
x = rt * rt;
rt++;
}
// Finds odd factors.
if ((output == 0) && (guess % 2 != 0)) {
// This divides the odd factor by an incrementing number that is not 1 or the number itself.
while (i < rt) {
primes = primes + (guess % i);
// If the sum of the remainders to the division is not 0, then the number is prime.
// I used duplicate to make sure it didn't just go through once and count as prime.
if (primes != 0){
// There were duplicates, so I added them for the division later.
duplicate = duplicate + guess;
// This was used to wait for the while loop to finish, then find if the amount of times the guess went through was equal to its value - 1 and another 1 for the final number (primes are only divisible by one and itself).
if (i == (factors - 1)) {
if ((duplicate / guess) == (guess- 2)) {
primeFactors.add(guess);
}
}
}
i++;
}
}
guess++;
}
return primeFactors;
}
}
import java.util.*;
公共类因素{
公共静态void main(字符串参数[]){
长数;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“\n\n\n此程序查找给定数字的素数因子。\n”);
系统输出打印(“请输入号码:”);
num=in.nextLong();
System.out.println(“\n素因子为:“+primeFactor(num)+”\n”);
}
公共静态ArrayList素数因子(长n){
长输出=0;
长猜测=2;
ArrayList primeFactors=新的ArrayList();
而(猜测作为一个开始,而不是
long x = 0;
long z = 1;
while (x <= n) {
x = z * z;
z++;
}
while (j < z) {
长x=0;
长z=1;
虽然(xOK),但您的代码存在一些问题:
j、 x,j&n,命名不好的变量会使调试工作变得困难
您的System.out.println()
调用在哪里,以便查看代码中发生了什么
n的平方根是停止寻找n以下素数的最佳点
我可以建议你看看这个:寻找一种快速查找素数的方法。一个使代码更简单的建议。在你的素数方法中,首先找到它是一个因子,我相信你已经在做了,然后调用另一个方法来确定它是否是素数。如果是素数,请添加到列表中。我将为你提供一些关于素数的伪代码易于实现(效率不高)的算法:
the value to factorize is N
keep going until N is equal to one
start at 2 and find lowest number X that divides N evenly
X is one factor
N/X is your new N to factor
变量名不一致。因子
尤其糟糕,因为它只是对单个因子的猜测。请改称它为猜测
你用因子
、素数
和dup
做的算术也很奇怪。你为什么要添加到dup
或素数
?试着做你自己的计算机,对数字12执行你的算法;你会发现你根本没有正确的算法
通过在最后增加因子
,您已经排除了重复因子的可能性
你在这里做的数学和逻辑很奇怪,我不太明白发生了什么
为此,我将投票赞成+1使代码更简单。这可以通过两个简单的方法来实现。第一个方法将查找数字的因子并通过素数检查程序运行它们。如果它们是因子并通过素数检查,则它们将被添加到数组中
奖励点:通过只搜索每个因子检查器和素数检查器的下半部分来提高算法的速度。逻辑上,任何超过半个数字的值都不能是该数字的因子
更多的速度奖励积分,增加2点,跳过2的所有倍数,因为它们自动不是素数。祝你好运
import java.util.ArrayList;
import java.util.Scanner;
/***************************************************
*
* @file: PrimeFactors.java
* @date: Mar 17, 2013
* @author: AaronW
*/
/**
*
* @author AaronW
*/
public class PrimeFactors {
public PrimeFactors() {
}
/**
*
* @param args
*/
public static void main(String[] args) {
long num;
Scanner in = new Scanner(System.in);
System.out.println("\n\n\nThis program finds the prime factors of a given number.\n");
System.out.print("Please enter the number: ");
num = in.nextInt();
System.out.println("\nThe factors are: " + findFactors((double)num) + "\n");
}
public static ArrayList<Integer> findFactors(Double num) {
ArrayList<Integer> factors = new ArrayList<Integer>();
for (int x = 1; x <= num; x++) {
System.out.println("Testing " + num + " % " + x + " = " + num % x);
// First, let's see if a number is factor of your target number
if (num % x == 0) {
System.out.println(x + " is a factor");
// Now that we know it's a factor, let's test to see if it's prime
if (isPrime(x)) {
// If it's prime, add it to the ArrayList
System.out.println("And " + x + " is prime.");
factors.add(x);
} else {
System.out.println("But " + x + " is not prime.");
}
} else {
System.out.println(x + " is not a factor");
}
}
return factors;
}
public static boolean isPrime(double num) {
// Let's start by assuming everything is prime and try to prove that false
// If we fall through the loop without proving it false, we have a prime
boolean prime = true;
for (int x = 2; x < num; x++) {
// if our target number can be divided by any number between 1 and itself, it is not prime
if (num % x == 0) {
prime = false;
}
}
return prime;
}
}
import java.util.ArrayList;
导入java.util.Scanner;
/***************************************************
*
*@file:PrimeFactors.java
*@日期:2013年3月17日
*@作者:AaronW
*/
/**
*
*@作者AaronW
*/
公共类因素{
公共政策因素(){
}
/**
*
*@param args
*/
公共静态void main(字符串[]args){
长数;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“\n\n\n此程序查找给定数字的素数因子。\n”);
系统输出打印(“请输入号码:”);
num=in.nextInt();
System.out.println(“\n因子为:”+findFactors((double)num)+“\n”);
}
公共静态ArrayList findFactors(双数值){
ArrayList因子=新的ArrayList();
对于(int x=1;x)考虑使用调试器或“穷人的调试器”(许多调用<代码> St.out)。监视程序变量的状态,从而找出程序出现错误的原因。编辑:噢,顺便说一句,欢迎使用stackoverflow!我建议从调试代码开始。您可以通过添加System.out.println()来完成此操作方法在整个代码中调用或使用调试器进行调用。使用这两种方法中的任何一种方法检查每一行代码的行为是否符合预期。最有可能的情况是,您会发现一行代码不符合您的要求,如果您不立即了解如何修复它,您将对问题有更清晰的了解,并可以询问另一行更具体的问题这里是ic问题。非常有用,谢谢!我以前有过它,所以如果余数每次都是0,它会添加一个数字,所以如果不是1或数字本身,我有(dup/factors)=(factors-2)来解决这个问题。使用系统打印表明它应该可以工作(例如,当输入21时,7将显示5次,因此它应该是素数-但从未显示在ArrayList中),但事实并非如此。为我笨拙的思维方式感到抱歉,哈哈!它似乎做了远远超出必要的工作。有时候,征求第二个意见,对问题有一个新的观点,然后重新开始是很有帮助的。祝你好运!更新了帖子,并发表评论,试图解释我疯狂的数学(就像我说的,我是个新手,所以我不知道是否有更简单的方法来做事情。)编辑:看起来我没有发现我的错误。我当场想到的变量(应该在粘贴到这里之前更改它们-这只是我的第一篇帖子).至于println调用,我已经测试过了,但是在粘贴到这里之前已经删除了。如果那样有帮助的话,很抱歉。谢谢您的输入!
import java.util.ArrayList;
import java.util.Scanner;
/***************************************************
*
* @file: PrimeFactors.java
* @date: Mar 17, 2013
* @author: AaronW
*/
/**
*
* @author AaronW
*/
public class PrimeFactors {
public PrimeFactors() {
}
/**
*
* @param args
*/
public static void main(String[] args) {
long num;
Scanner in = new Scanner(System.in);
System.out.println("\n\n\nThis program finds the prime factors of a given number.\n");
System.out.print("Please enter the number: ");
num = in.nextInt();
System.out.println("\nThe factors are: " + findFactors((double)num) + "\n");
}
public static ArrayList<Integer> findFactors(Double num) {
ArrayList<Integer> factors = new ArrayList<Integer>();
for (int x = 1; x <= num; x++) {
System.out.println("Testing " + num + " % " + x + " = " + num % x);
// First, let's see if a number is factor of your target number
if (num % x == 0) {
System.out.println(x + " is a factor");
// Now that we know it's a factor, let's test to see if it's prime
if (isPrime(x)) {
// If it's prime, add it to the ArrayList
System.out.println("And " + x + " is prime.");
factors.add(x);
} else {
System.out.println("But " + x + " is not prime.");
}
} else {
System.out.println(x + " is not a factor");
}
}
return factors;
}
public static boolean isPrime(double num) {
// Let's start by assuming everything is prime and try to prove that false
// If we fall through the loop without proving it false, we have a prime
boolean prime = true;
for (int x = 2; x < num; x++) {
// if our target number can be divided by any number between 1 and itself, it is not prime
if (num % x == 0) {
prime = false;
}
}
return prime;
}
}