C ++从编程任务书 [英] C++ Prime number task from the book
本文介绍了C ++从编程任务书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个C ++初学者;)
下面的代码作为找到2-1000之间的所有素数的方法有多好:
I'm a C++ beginner ;) How good is the code below as a way of finding all prime numbers between 2-1000:
int i, j;
for (i=2; i<1000; i++) {
for (j=2; j<=(i/j); j++) {
if (! (i%j))
break;
if (j > (i/j))
cout << i << " is prime\n";
}
}
推荐答案
对我们在这里发布的整个文本的一个简单的答案是:试验区!
如果有人提到这个任务所基于的数学基础,我们可以节省大量的时间;)
The one simple answer to the whole bunch of text we posted up here is : Trial division! If someone mentioned mathematical basis that this task was based on, we'd save plenty of time ;)
这篇关于C ++从编程任务书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文