函数作业问题

以下作业题,请各位帮忙指点,因刚学c++,请各位老师在程序尽量多些注释说明。谢谢!

正整数n是素数,如果它只能由1和本身整除。
(1)写一个名为ISPrime的C++函数,它取正整数参数,如果参数为Prime,则返回true,否则返回false。假设参数大于1
(2)写一个C++函数,名为SUFFPrimes,取正整数参数n,并返回第一N素数的和。例如,SUFFPrimes(6)必须返回41,这是前6个素数的总和:
2+3+5+7+11+13=41。您必须使用在部分(1)中定义的ISPrime函数来定义函数SUFFPROMES。

原题:A positive integer number n is prime if it is divisible by ONLY 1 and
itself.
a. Write a C++ function named isPrime that takes a positive
integer argument and returns true if the argument is prime and returns
false otherwis。 Assume the argument is greater than 1.

        b.Write a C++ function named sumOfPrimes that takes a positive integer

argument n and returns the sum of the first n prime numbers. For example,
sumOfPrimes(6) must return 41 , which is the sum of the first 6 prime numbers:
2+3+5+7+11+13 = 41. You must use the isPrime function you defined in part (a) to define the function sumOfPrimes.


哔哔one
浏览 744回答 1
1回答

哈士奇WWW

第一题&nbsp;#include&nbsp;<stdio.h> #include&nbsp;<iostream> using&nbsp;namespace&nbsp;std; bool&nbsp;ISPrime(const&nbsp;int&nbsp;&&nbsp;num) { &nbsp;&nbsp;if(num&nbsp;<=1) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;} &nbsp;&nbsp;int&nbsp;i&nbsp;=&nbsp;2; //素数就是指&nbsp;因子只有自己和1 &nbsp;&nbsp;//i&nbsp;从二开始 &nbsp;&nbsp;//用num除以i,若是可以除尽,且i小于num&nbsp;就说明num&nbsp;存在别的因子 &nbsp;&nbsp;//所以num就不是素数 &nbsp;&nbsp;for(i&nbsp;=&nbsp;2;&nbsp;i&nbsp;<&nbsp;num;&nbsp;++i) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if(num&nbsp;%&nbsp;i&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//若可以除尽,就跳出循环 &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp;if(i&nbsp;==&nbsp;num) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;//如果出循环且i值已经等于num&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//说明是素数 &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;//否则不是素数 } int&nbsp;main() { &nbsp;&nbsp;int&nbsp;n&nbsp;=&nbsp;19; &nbsp;&nbsp;bool&nbsp;ret&nbsp;=&nbsp;ISPrime(n); &nbsp;&nbsp;if(ret) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;printf("Is&nbsp;Prime\n"); &nbsp;&nbsp;} &nbsp;&nbsp;else &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;printf("Not&nbsp;Prime\n"); &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;0; }第二题&nbsp;#include&nbsp;<stdio.h> #include&nbsp;<iostream> using&nbsp;namespace&nbsp;std; int&nbsp;SUFFPrime(const&nbsp;int&nbsp;&&nbsp;n) { &nbsp;&nbsp;int&nbsp;sum&nbsp;=&nbsp;0; &nbsp;&nbsp;if(n&nbsp;<=&nbsp;0) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sum; &nbsp;&nbsp;} &nbsp;&nbsp;int&nbsp;i&nbsp;=&nbsp;2; &nbsp;&nbsp;int&nbsp;count&nbsp;=&nbsp;0; &nbsp;&nbsp;int&nbsp;num&nbsp;=&nbsp;2; //素数就是指&nbsp;因子只有自己和1 &nbsp;&nbsp;//i&nbsp;从二开始 &nbsp;&nbsp;//用num除以i,若是可以除尽,且i小于num&nbsp;就说明num&nbsp;存在别的因子 &nbsp;&nbsp;//所以num就不是素数 &nbsp;&nbsp;//外层循环用来控制个数,个数应该小于&nbsp;n &nbsp;&nbsp;//内层循环用来判断当前数字是不是素数 &nbsp;&nbsp;while(count&nbsp;<&nbsp;n) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;for(i&nbsp;=&nbsp;2;i&nbsp;<&nbsp;num&nbsp;;&nbsp;++i) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(num&nbsp;%&nbsp;i&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//若可以除尽,就跳出循环 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;if(i&nbsp;==&nbsp;num) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//如果出循环且i值已经等于num&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//说明是素数 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;+=&nbsp;num; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//否则不是素数 &nbsp;&nbsp;&nbsp;&nbsp;//就看下一个数是不是素数 &nbsp;&nbsp;&nbsp;&nbsp;num++; &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;sum; } int&nbsp;main() { &nbsp;&nbsp;int&nbsp;n&nbsp;=&nbsp;7; &nbsp;&nbsp;int&nbsp;sum&nbsp;=&nbsp;SUFFPrime(n); &nbsp;&nbsp;printf("sum:&nbsp;%d\n",sum); &nbsp;&nbsp;return&nbsp;0; }
打开App,查看更多内容
随时随地看视频慕课网APP