Logic: I am trying to loop from 3 to 100 and put all prime numbers in that range in an array.
(逻辑:我试图从3到100循环,并将该范围内的所有素数放入数组中。)
I do this by first putting 2 manually into the array.(我首先将2手动放入数组中。)
After that, I loop over all the numbers and if a number is not divisible by all the prime numbers I have added to the array, it is a prime.(之后,我遍历所有数字,如果一个数字不能被我添加到数组中的所有质数所除,则它是质数。)
The logic itself is not really valid in my opinion but this is a homework problem and I need to do it the way the professor wants it to be done.(在我看来,逻辑本身并不是真的有效,但这是一个作业问题,我需要按照教授希望的方式进行。)
So I try to loop and it gets to a point and the program just crashes.(因此,我尝试循环执行,直至达到临界点,程序便崩溃了。)
What am I doing wrong here?(我在这里做错了什么?)
int primeNums_lessThan100[25] = {2}; //25 is the size here because there are only 25 prime numbers under 100.
int primeNums_lessThan100_length = 1;
for(int counter = 3 ; counter < 100 ; counter++)
{
printf("Entered here!
");
for(int array_idx = 0; array_idx < primeNums_lessThan100_length ; array_idx++)
{
if(counter % primeNums_lessThan100[array_idx] != 0)
{
printf("Entered here, TOO!
");
primeNums_lessThan100[array_idx+1] = counter;
primeNums_lessThan100_length++;
}
else
{
continue;
}
}
}
ask by new_learner translate from so