Java 欧拉项目。3索引自动边界异常
我正在尝试使用埃拉托什尼筛法来寻找一个大数的最大素因子(欧拉计划中的问题3) 我的语法似乎是正确的,我使用的是Long(不是int),但我得到了以下错误消息:Java 欧拉项目。3索引自动边界异常,java,math,indexoutofboundsexception,Java,Math,Indexoutofboundsexception,我正在尝试使用埃拉托什尼筛法来寻找一个大数的最大素因子(欧拉计划中的问题3) 我的语法似乎是正确的,我使用的是Long(不是int),但我得到了以下错误消息: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unk
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at problem3.ProblemThree.Factor(ProblemThree.java:49)
at problem3.ProblemThree.Recursion(ProblemThree.java:37)
at problem3.ProblemThree.main(ProblemThree.java:83)
我不知道为什么会这样。谁能告诉我我做错了什么
package problem3;
import java.util.List;
import java.util.ArrayList;
public class ProblemThree
{
//initializing variables and lists
long factorNo;
long nowTesting;
int i;
List<Long> allPrimeList = new ArrayList<Long>();
List<Long> ourPrimes = new ArrayList<Long>();
ProblemThree(long x) //constructor; the input "x" is the number whose highest prime factor is being sought
{
factorNo = x;
}
void initialize() //use the workaround initialization (add 2 to the allPrimesList, set nowTesting to 3).
//If the factorNo is even, add 2 to the primes list
//TODO: need more elegant solution
{
allPrimeList.add((long) 2);
nowTesting=3;
if(factorNo % 2 == 0) ourPrimes.add((long) 2);
i = 0;
}
void recursion() //keep factoring the next nowTesting until the next nowTesting is greater than half of the factorNo
{
while (nowTesting <= (factorNo/2))
{
nowTesting = factor(nowTesting);
}
System.out.println(ourPrimes);
}
long factor(long t) //The factorization algorithm. Lists all the factors of long t
{
nowTesting = t;
// Line 49:
if ((nowTesting % allPrimeList.get(i)) == 0)
{
i = 0;
return (nowTesting + 2);
}
else
if(i <= allPrimeList.size()) //if we have not yet reached the end of ourPrimeList
{
i++;
return nowTesting;
}
else //if the end of ourPrimeList has been reached without a single modulus==0, this number is a prime
{
allPrimeList.add(nowTesting);
if(factorNo%nowTesting==0) //if the nowTesting is a prime factor of factorNo, it will be perfectly divisible
{
ourPrimes.add(nowTesting);
}
i=0;
return (nowTesting+2);
}
}
public static void main (String[] args)
{
ProblemThree pt = new ProblemThree(600851475143L);
pt.initialize();
pt.recursion();
}
}
包装问题3;
导入java.util.List;
导入java.util.ArrayList;
公共课问题三
{
//初始化变量和列表
长因子诺;
长期测试;
int i;
List allPrimeList=new ArrayList();
List ourPrimes=new ArrayList();
ProblemThree(长x)//构造函数;输入“x”是正在查找其最高素数因子的数字
{
系数no=x;
}
void initialize()//使用变通方法初始化(将2添加到allPrimesList中,将nowTesting设置为3)。
//如果factorNo为偶数,则在素数列表中添加2
//TODO:需要更优雅的解决方案吗
{
添加((长)2);
nowTesting=3;
如果(factorNo%2==0)ourPrimes.add((long)2);
i=0;
}
void recursion()//继续分解next nowTesting,直到next nowTesting大于factorNo的一半
{
while(nowTesting一个有趣的方法。当然,没有人应该解决您的Euler挑战。但是您知道第二次输入'factor'nowTesting是3吗
// The factorization algorithm. Lists all the factors of long t
long factor (final long nowTesting)
{
System.out.println ("entering factor: " + nowTesting);
小点子:
allPrimeList.add ((long) 2);
可以这样写:
allPrimeList.add (2L);
你居然认出了“决赛”在factor中的'long'参数前面?如果您将所有未更改的内容标记为final,则有助于对代码进行推理。在实践中,结果是,您的Java代码中充斥着'final'修饰符,但事实就是如此。这是好代码的标志-可能不是好设计。final可能是默认值。一个有趣的ap方法。当然,没有人应该解决你的Euler挑战。但是你知道第二次,你输入的“因子”现在测试是3吗
// The factorization algorithm. Lists all the factors of long t
long factor (final long nowTesting)
{
System.out.println ("entering factor: " + nowTesting);
小点子:
allPrimeList.add ((long) 2);
可以这样写:
allPrimeList.add (2L);
你居然认出了“决赛”在factor中的'long'参数前面?如果您将所有未更改的内容标记为final,则有助于对代码进行推理。在实践中,结果是,您的Java代码中充斥着'final'修饰符,但事实就是这样。这是好代码的标志-可能不是好设计。final可能是默认值。在第49行,shoull你不是在检查nowTesting是否可以被i整除,而不是所有素数的第i个元素吗?在第49行,你不是应该检查nowTesting是否可以被i整除,而不是所有素数的第i个元素吗?谢谢大家耐心地阅读我的代码,我意识到这一定非常痛苦:)
我刚刚解决了这个问题。回想起来,我以前的方法似乎非常复杂。这是我使用的最终解决方案,非常优雅,但仍有改进的余地:
//second attempt from the ground up!
package problem3;
public class BiggestPrime
{
long lInput;
long factorTest;
long currentHeight;
boolean divided;
public BiggestPrime(long n)
{
factorTest = 2;
currentHeight = n;
System.out.println("The prime factors of " + n + " are:");
while (factorTest<currentHeight)
{
if (divided == true) {factorTest = 2; divided = false;}
if (factorTest > currentHeight) {System.out.println("factorTest is greater than currentHeight; breaking"); break;}
if (currentHeight%factorTest==0)
{
System.out.println(factorTest);
currentHeight /= factorTest;
divided = true;
}
else { factorTest = factorTest + 1L; divided = false;}
}
if (factorTest == currentHeight)
{
System.out.println(factorTest);
}
System.out.println("The end");
}
public static void main (String[] args)
{
BiggestPrime bp = new BiggestPrime(600851475143L);
}
}
//从头开始第二次尝试!
包装问题3;
公共类最大素数
{
长林普特;
长因子试验;
长高度;
布尔除法;
公共最大素数(长n)
{
factorTest=2;
当前高度=n;
System.out.println(“+n+的素因子为:”);
while(factorTest currentHeight){System.out.println(“factorTest大于currentHeight;break”);break;}
如果(当前高度%factorTest==0)
{
系统输出打印项次(系数测试);
当前高度/=系数测试;
除以=真;
}
else{factorTest=factorTest+1L;divided=false;}
}
如果(factorTest==当前高度)
{
系统输出打印项次(系数测试);
}
System.out.println(“结束”);
}
公共静态void main(字符串[]args)
{
BiggestPrime bp=新的BiggestPrime(600851475143L);
}
}
感谢大家耐心地阅读我的代码,我意识到这一定非常痛苦:)
我刚刚解决了这个问题。回想起来,我以前的方法似乎非常复杂。这是我使用的最终解决方案,非常优雅,但仍有改进的余地:
//second attempt from the ground up!
package problem3;
public class BiggestPrime
{
long lInput;
long factorTest;
long currentHeight;
boolean divided;
public BiggestPrime(long n)
{
factorTest = 2;
currentHeight = n;
System.out.println("The prime factors of " + n + " are:");
while (factorTest<currentHeight)
{
if (divided == true) {factorTest = 2; divided = false;}
if (factorTest > currentHeight) {System.out.println("factorTest is greater than currentHeight; breaking"); break;}
if (currentHeight%factorTest==0)
{
System.out.println(factorTest);
currentHeight /= factorTest;
divided = true;
}
else { factorTest = factorTest + 1L; divided = false;}
}
if (factorTest == currentHeight)
{
System.out.println(factorTest);
}
System.out.println("The end");
}
public static void main (String[] args)
{
BiggestPrime bp = new BiggestPrime(600851475143L);
}
}
//从头开始第二次尝试!
包装问题3;
公共类最大素数
{
长林普特;
长因子试验;
长高度;
布尔除法;
公共最大素数(长n)
{
factorTest=2;
当前高度=n;
System.out.println(“+n+的素因子为:”);
while(factorTest currentHeight){System.out.println(“factorTest大于currentHeight;break”);break;}
如果(当前高度%factorTest==0)
{
系统输出打印项次(系数测试);
当前高度/=系数测试;
除以=真;
}
else{factorTest=factorTest+1L;divided=false;}
}
如果(factorTest==当前高度)
{
系统输出打印项次(系数测试);
}
System.out.println(“结束”);
}
公共静态void main(字符串[]args)
{
BiggestPrime bp=新的BiggestPrime(600851475143L);
}
}
您是否尝试查找这些错误消息的含义?只是想让您知道,方法名通常使用camelBack
,类通常使用大写DWORDS
。当方法名称看起来像类时,解析代码确实很困难。根据错误消息,在第49行,您从allPrimeList
数组中获取索引1处的项,而该数组中只有1项(索引0处)。那是个错误。因此,您需要回顾一下您的逻辑,并确定为什么您要尝试访问一个索引,该索引超出了