制作程序打印素数 [英] Make program to print prime number

查看:133
本文介绍了制作程序打印素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>

 int main()
      {
   int prime[10000];
       int i,j;
      for(i=1;i<10000;i++)
    {
   prime[i]=1;
        }
         prime[1]=0;
        for(i=2;i<=100;i++)
       {
       if (prime[i]==1){
        for (j=i;i*j<10000;j++)
         {prime[i*j]=0;}
           }
         }
     for(i=1;i<10000;i++)
            {
        if( prime[i]==1 )
         {
       printf("%4d",i  );
      }

  }
   printf("\n");
   return 0;
}





首先程序将数组1设置为10000到值1然后设置prime [1]到0

然后这段代码:



first the program set the array 1 until 10000 to value 1 then set prime[1] to 0
then this code :

for(i=2;i<=100;i++)
 {
     if (prime[i]==1){
       for (j=i;i*j<10000;j++) {prime[i*j]=0;}
     }





应该消除并使所有的多个数字

例如2 * 2 = 4并且使得[数组中的4]变为0

但为什么素数[6]也变为0?



为什么我第一次调试它为什么突然所有数组的值变为1表示奇数,0表示偶数?虽然已经把它设置为1 ??是什么让这成为101010阵列?



我尝试过:



为什么当我第一次调试它为什么突然所有数组的值变为1为奇数而0为偶数?虽然已经把它设置为1 ??是什么让它成为数组中的101010?



should eliminate and make all the of multiple number
for example 2*2 = 4 and make prime[4] in array become 0
but why the prime[6] also become 0?

why when i debug it for the first time why suddenly all the value of the array become 1 for odd and 0 for even number?? though already set it to 1?? what makes this become 101010 in array?

What I have tried:

why when i debug it for the first time why suddenly all the value of the array become 1 for odd and 0 for even number?? though already set it to 1?? what makes this become 101010 in array?

推荐答案

因为你从索引1开始 - 它为零,你就这样设置了。这意味着它不是素数所以你忽略它。

当你进入循环时,你的指数是2 - 而且它仍然是1,所以它是素数。

你输入内部循环,它将索引的每个倍数设置为零,以指示它们不是素数。因为2是第一个偶数(根据定义),当您设置 prime [i * j] = 0时,从筛子中删除所有偶数;

因此在围绕外循环的第一次迭代之后,1不是素数,2是素数,3是素数,4不是,5是,依此类推。你看到的是哪种模式。





我在第一次迭代中不理解内循环它使所有偶数变成零?对于(j = i; i * j <10000; j ++){prime [i * j] = 0}当进入循环时索引i = 2 I = j = 2 2 * 2 = 4< 10000 J ++这意味着开始j = 2然后乘以j = 2 * 2如果它在10000以下然后用1增加j,所以在它是3 * 3 = 9(?)后我不明白这部分!!



当你为循环写一个时,它有四个部分:

Because you start with index 1 - and it's zero, you set it that way. That means it's "not prime" so you ignore it.
When you enter loop, your index is 2 - and that's still one, so it's prime.
You enter the inner loop, which sets every multiple of the index to zero to indicate they are not prime. Because 2 is the first even number (by definition) that removes all even numbers from your sieve when you set prime[i*j]=0;
So after the first iteration round your outer loop, 1 is not prime, 2 is prime, 3 is prime, 4 isn't, 5 is, and so on. Which is the pattern you are seeing.


I dont understand the inner loop here in first iteration it make all even number become zero? For(j=i;i*j<10000;j++) {prime[i*j]=0} Index i=2 when enter the loop I=j=2 2*2=4<10000 J++ This mean start j=2 then multiply it j=2*2 if it under 10000 then increment j with 1, so after it is 3*3=9(?) i dont understand this part!!

When you write a for loop, it has four parts:
for (initialization; condition; modification)
   execution statement

执行语句很明显 - 这是每次循环时执行的代码。在你的情况下,就是这样:

The execution statement is obvious - it's the code that is executed each time you go round the loop. In your case, it's this:

{prime[i*j]=0;}



初始化可让您设置循环的起始值:


The initialization lets you set up starting values for the loop:

j = 1



条件如果每次要进入循环时进行测试,如果是,则循环继续。如果为false,则循环终止而不再执行执行语句


The condition if tested each time you are about to enter the loop, and if it is true, the loop continues. If it is false, the loop terminates without executing the execution statement again.

i*j<10000

修改如果在每次执行执行语句结束时执行,则设置循环为下一次迭代:

The modification if executed at the end of each execution of the execution statement to set the loop up for the next iteration:

j++



所以你先设置 j 到i的值,并重复循环,直到 i * j 超过(或等于)10000.



有意义吗?





啊得到它!!!!这意味着i = 2,因为初始化和j = 2,因为条件为真4 <10000它将执行执行语句然后j将增加到3所以i * j <10000 - > 6< 10000这就是为什么数组中偶数的所有值都变为0然后对于i = j = 3它将使得多个3的值在数组中变为0,依此类推?



Dat's de bunny! :thumbsup:





非常感谢!!!!!!!!!!!!!!!!! !!!!!!!!详细解释!!!!!!!!!!!!!!希望每次都能找到像你这样的人,我很困惑!!! lol

欢迎你!


So you start by setting j to the value of i, and repeat the loop until i * j exceeds (or equals) 10000.

Make sense?


ah got it!!!! it means i=2 because the initialization and j =2 because the condition is true 4<10000 it will execute the execution statement then j will be incremented to 3 so i*j<10000 -> 6 <10000 that is why all the value of even number in array become 0 then for i=j=3 it will make the value of multiple 3 become 0 in array and so on?

Dat's de bunny! :thumbsup:


thankyou so much!!!!!!!!!!!!!!!!!!!!!!!!! for the detailed explanation!!!!!!!!!!!!!! hope to find someone like you again everytime im confuse!!!lol
You're welcome!


首先,正确缩进你的代码,它有助于阅读。

First of all, indent your code properly, it help to read.
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int prime[10000];
	int i,j;
	for(i=1;i<10000;i++)
	{
		prime[i]=1;
	}
	prime[1]=0;
	for(i=2;i<=100;i++)
	{
		if (prime[i]==1){
			for (j=i;i*j<10000;j++)
			{
				prime[i*j]=0;
			}
		}
	}
	for(i=1;i<10000;i++)
	{
		if( prime[i]==1 )
		{
			printf("%4d",i  );
		}

	}
	printf("\n");
	return 0;
}




引用:

为什么我第一次调试它为什么突然所有数组的值变为1为奇数而0为偶数?虽然已经把它设置为1 ??是什么让它成为数组中的101010?

why when i debug it for the first time why suddenly all the value of the array become 1 for odd and 0 for even number?? though already set it to 1?? what makes this become 101010 in array?



我不知道你所谓的'debug',但使用调试器会显示你的代码是如何做你所看到的。

看起来当你从素数中删除多个时,你也会删除那个素数。



当你不明白你的代码在做什么或者它为什么会这样做,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


I don't know what you call 'debug', but using the debugger would show how your code is doing what you see.
it look like when you remove multiples from the of primes, you also remove that prime.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于制作程序打印素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆