生成前100个素数的问题 [英] Problem in Generating 1st 100 Primes

查看:99
本文介绍了生成前100个素数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在编写一个简单的代码来生成前100个素数。

除了程序的输出外,一切看起来都不错。它有什么问题?b $ b错了吗?我正在附加程序以及输出。如果有人可以通过 ba *******邮寄给我,我们会感激不尽*****@gmail.com


谢谢

AM Rahman


// -------------------------------------------------- -------------------------------------------------- --------------------------------------


#include< iostream.h>

#include< process.h>


int prime(int number);

int main(无效)

{


int counter = 0;


for(int n = 2; n < 1000; n ++)

{

if(prime(n)== 1)

{

cout<< n<< " " ;;

counter ++;

if(counter 99)

{

exit(0);

}


} //关闭如果

} //关闭


返回0;

} //关闭主要

int prime(int number)

{


for(int i = 2; i< = number -1; i ++)

{

if(number%i!= 0)

{

返回1;

}


其他

{

返回0;

}

} //关闭

}


---------------------------------------------- -------------------------------------------------- ----------------------------------- //


输出


2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

51 53 55

57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101

103 105

107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 13 7 139 141

143 145

147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181

183 185

187 189 191 193 195 197 199

解决方案



baltimoredude1写道:


int prime(int number)

{


for(int i = 2;我< =数字-1; i ++)

{

if(number%i!= 0)

{

return 1;

}


其他

{

返回0;

}

} //关闭

}



当''number''mod某个数字时,这将返回true不是0. 15 mod 2

不为零(所以你的函数返回true),但15不是素数。

基本上这个素数函数没有正确执行。


-Brian


baltimoredude1写道:


我正在编写一个简单的代码来生成前100个素数。



你确定100吗?你似乎只打印出前99,没有?


除了程序的输出外,一切看起来都不错。



LOL ......所以,一切都好,只是它不起作用,是吗?这意味着

并非一切都好。


什么是'b $ b错误?



我立即看到的一些事情。下面的评论......


我附加程序以及输出。如果有人可以通过 ba *******邮寄给我,我们会感激不尽*****@gmail.com


谢谢

AM Rahman


// -------------------------------------------------- -------------------------------------------------- --------------------------------------


#包括< iostream.h>



没有这样的标准标题。只要我们有标准的b $ b,就没有了。你为什么还在编写非标准代码?应该是:


#include< iostream>


#include< process.h>



这是一个非标准的标题。实际上你在使用它吗?


>

int prime(int number);


int main(void)



" void"在括号之间没有多大意义,除非你写的是一个C程序。你不是。所以,它不是。虽然这是允许的,但是你需要习惯不使用它。


{

int counter = 0;


for(int n = 2; n< 1000; n ++)

{

if(prime(n)== 1)

{

cout<< n<< "英寸;



这应该是


std :: cout<< n<< " " ;;


counter ++;

if(counter 99)

{

退出(0);



这应该是简单的


休息;


}


} //关闭如果

} //关闭


返回0;

} //关闭主要


int prime(int number)

{


for (int i = 2; i< = number -1; i ++)

{

if(number%i!= 0)

{

返回1;



如果有一个师的剩余部分,你不应该继续尝试

来寻找另一个除数吗?


}


else

{

返回0;

}

} //关闭



一旦你检查了_all_数字,你又回来了什么?


}


------------------------- -------------------------------------------------- -------------------------------------------------- ------ //


输出


2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

51 53 55

57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101

103 105

107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139

141 143 145

147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179

181 183 185

187 189 191 193 195 197 199



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


baltimoredude1写道:





我正在编写一个简单的代码来生成第一个100个素数。

除了程序的输出外,一切看起来都不错。它有什么问题?b $ b错了吗?我正在附加程序以及输出。如果有人可以通过 ba *******邮寄给我,我们会感激不尽*****@gmail.com


谢谢

AM Rahman


// -------------------------------------------------- -------------------------------------------------- --------------------------------------


#包括< iostream.h>



#include< iostream>


#include< process.h>



不是标准标题。反正没用过。


int prime(int number);



我可以推荐


bool is_prime(无符号数);


>


int main(无效)

{


int counter = 0; < (b = 2; n< 1000; n ++)



if(prime(n)== 1)

{

cout<< n<< " " ;;

counter ++;

if(counter 99)

{

exit(0);

}


} //关闭如果

} //关闭


返回0;

} //关闭主要

int prime(int number)

{


for(int i = 2; i< = number -1; i ++)

{

if(number%i!= 0)

{

返回1;

}


其他

{

返回0;

}

} //关闭

}



注:


a)此函数永远不会返回number == 2的值。因此,您有

未定义的行为main()。


b)对于数字2,prime()函数在语义上被破坏。 for循环

永远不会进入第二次迭代,即i == 2是唯一出现
的情况:如果number不是2的倍数,则返回1,如果数字是

的2的倍数,则返回0.这与输出匹配。


--- -------------------------------------------------- -------------------------------------------------- ---------------------------- //


输出

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

51 53 55

57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101

103 105

107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141

143 145

147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181

183 185

187 189 191 193 195 197 199


Hi

I was writing a simple code to generate the first 100 prime numbers.
Everything looks fine to me except the output of the program. What''s
wrong with it? I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@gmail.com

Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostream.h>
#include<process.h>

int prime(int number);
int main(void)
{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";
counter ++;
if (counter 99)
{
exit(0);
}

} // close if
} // close for

return 0;
} // close main

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
}

else
{
return 0;
}
} // close for
}

-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141
143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181
183 185
187 189 191 193 195 197 199

解决方案


baltimoredude1 wrote:

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
}

else
{
return 0;
}
} // close for
}

This will return true when ''number'' mod some number is not 0. 15 mod 2
is not zero ( so your function returns true ), but 15 is not prime.
Basically this prime function isn''t implemented right.

-Brian


baltimoredude1 wrote:

I was writing a simple code to generate the first 100 prime numbers.

Are you sure about "100"? You seem to only print out first 99, no?

Everything looks fine to me except the output of the program.

LOL... So, everything is OK, only it doesn''t work, eh? That means
not everything is OK.

What''s
wrong with it?

A couple of things that I immediately see. Comments below...

I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@gmail.com

Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostream.h>

There is no such standard header. There hasn''t been for as long as we had
the Standard. Why are you still writing non-standard code? Should be:

#include <iostream>

#include<process.h>

This is a non-standard header. Are you using anything from it, actually?

>
int prime(int number);
int main(void)

"void" between parentheses doesn''t make much sense, unless you''re writing
a C program. You''re not. So, it doesn''t. And although it''s allowed, you
should take a habit of not using it.

{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";

This should be

std::cout << n << " ";

counter ++;
if (counter 99)
{
exit(0);

This should probably be simply

break;

}

} // close if
} // close for

return 0;
} // close main

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;

If there is a remainder from division, shouldn''t you be going on trying
to find another divisor?

}

else
{
return 0;
}
} // close for

And once you checked _all_ numbers, what do you return?

}

-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139
141 143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179
181 183 185
187 189 191 193 195 197 199

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


baltimoredude1 wrote:

Hi

I was writing a simple code to generate the first 100 prime numbers.
Everything looks fine to me except the output of the program. What''s
wrong with it? I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@gmail.com

Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostream.h>

#include <iostream>

#include<process.h>

Not a standard header. Not used anyway.

int prime(int number);

May I recommend

bool is_prime( unsigned number );

>

int main(void)
{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";
counter ++;
if (counter 99)
{
exit(0);
}

} // close if
} // close for

return 0;
} // close main

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
}

else
{
return 0;
}
} // close for
}

Note:

a) This function never returns a value for number == 2. Thus, you have
undefined behavior in main().

b) For number 2, the prime() function is semantically broken. The for loop
never enters the second iteration, i.e., i==2 is the only case that ever
occurs: if number is not a multiple of 2, prime returns 1, if number is a
multiple of 2, prime returns 0. This matches the output.

-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141
143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181
183 185
187 189 191 193 195 197 199


这篇关于生成前100个素数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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