[英]Prime number C++ program
我不確定是否應該在這里詢問程序員,還是我應該問程序員,但是我一直在試圖弄清為什么該程序無法運行,盡管我發現了一些錯誤,但即使返回x也不是素數。
#include <iostream>
using namespace std;
bool primetest(int a) {
int i;
//Halve the user input to find where to stop dividing to (it will remove decimal point as it is an integer)
int b = a / 2;
//Loop through, for each division to test if it has a factor (it starts at 2, as 1 will always divide)
for (i = 2; i < b; i++) {
//If the user input has no remainder then it cannot be a prime and the loop can stop (break)
if (a % i == 0) {
return(0);
break;
}
//Other wise if the user input does have a remainder and is the last of the loop, return true (it is a prime)
else if ((a % i != 0) && (i == a -1)) {
return (1);
break;
}
}
}
int main(void) {
int user;
cout << "Enter a number to test if it is a prime or not: ";
cin >> user;
if (primetest(user)) {
cout << user << " is a prime number.";
}
else {
cout << user<< " is not a prime number.";
}
cout << "\n\nPress enter to exit...";
getchar();
getchar();
return 0;
}
抱歉,如果它太本地化(在這種情況下,您可以建議我在哪里問這樣的具體問題?)
我應該補充一點,我對C ++(和一般的編程)非常陌生
這僅僅是為了測試功能和控件。
i
永遠不會等於a - 1
你只會上升到b - 1
。 b
為a/2
,永遠不會導致匹配。
這意味着將返回1的循環結束條件永遠不會成立。
如果是質數,則循環結束。 這會導致不確定的行為,因為那里沒有return
語句。 Clang發出了警告,沒有任何特殊標志:
example.cpp:22:1: warning: control may reach end of non-void function
[-Wreturn-type]
}
^
1 warning generated.
如果您的編譯器沒有警告您,則需要打開更多警告標志。 例如,添加-Wall
會在使用GCC時發出警告:
example.cpp: In function ‘bool primetest(int)’:
example.cpp:22: warning: control reaches end of non-void function
總體而言,素數檢查循環要復雜得多。 假設你只關心的值a
大於或等於2
:
bool primetest(int a)
{
int b = sqrt(a); // only need to test up to the square root of the input
for (int i = 2; i <= b; i++)
{
if (a % i == 0)
return false;
}
// if the loop completed, a is prime
return true;
}
如果要處理所有int
值,則可以添加if (a < 2) return false;
一開始。
您的邏輯不正確。 您正在使用此表達式(i == a -1))
,就像卡爾所說的那樣,它永遠不可能是正確的。
例如:-
If a = 11
b = a/2 = 5 (Fractional part truncated)
因此,您正在循環運行直到i<5
。 所以i
永遠不能等於a-1
因為在這種情況下i的最大值將是4而a-1
值將是10
您只需檢查直到平方根即可。 但是下面是對代碼的一些修改,以使其正常工作。
#include <iostream>
using namespace std;
bool primetest(int a) {
int i;
//Halve the user input to find where to stop dividing to (it will remove decimal point as it is an integer)
int b = a / 2;
//Loop through, for each division to test if it has a factor (it starts at 2, as 1 will always divide)
for (i = 2; i <= b; i++) {
//If the user input has no remainder then it cannot be a prime and the loop can stop (break)
if (a % i == 0) {
return(0);
}
}
//this return invokes only when it doesn't has factor
return 1;
}
int main(void) {
int user;
cout << "Enter a number to test if it is a prime or not: ";
cin >> user;
if (primetest(user)) {
cout << user << " is a prime number.";
}
else {
cout << user<< " is not a prime number.";
}
return 0;
}
看一下這個:
//Prime Numbers generation in C++
//Using for loops and conditional structures
#include <iostream>
using namespace std;
int main()
{
int a = 2; //start from 2
long long int b = 1000; //ends at 1000
for (int i = a; i <= b; i++)
{
for (int j = 2; j <= i; j++)
{
if (!(i%j)&&(i!=j)) //Condition for not prime
{
break;
}
if (j==i) //condition for Prime Numbers
{
cout << i << endl;
}
}
}
}
main()
{
int i,j,x,box;
for (i=10;i<=99;i++)
{
box=0;
x=i/2;
for (j=2;j<=x;j++)
if (i%j==0) box++;
if (box==0) cout<<i<<" is a prime number";
else cout<<i<<" is a composite number";
cout<<"\n";
getch();
}
}
這是查找素數直到所有用戶輸入的數字的完整解決方案。
#include <iostream.h>
#include <conio.h>
using namespace std;
main()
{
int num, i, countFactors;
int a;
cout << "Enter number " << endl;
cin >> a;
for (num = 1; num <= a; num++)
{
countFactors = 0;
for (i = 2; i <= num; i++)
{
//if a factor exists from 2 up to the number, count Factors
if (num % i == 0)
{
countFactors++;
}
}
//a prime number has only itself as a factor
if (countFactors == 1)
{
cout << num << ", ";
}
}
getch();
}
一種方法是使用篩選算法,例如Eratosthenes的篩選器 。 這是一種非常有效的快速方法。
bool isPrime(int number){
if(number == 2 || number == 3 | number == 5 || number == 7) return true;
return ((number % 2) && (number % 3) && (number % 5) && (number % 7));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.