2013-07-02 58 views
1

我不知道我是否應該問這裏或程序員,但我一直在試圖找出爲什麼這個程序不會工作,雖然我已經發現了一些錯誤,但它仍然會返回「x不是素數」 ,即使是這樣。素數的C++程序

#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++(和一般的編程)

這是簡單地旨在是的功能和控制測試。

+0

給我們一些什麼時候不起作用的例子。 –

+0

爲什麼不這樣做......在for循環外面添加一個return(1)語句並刪除else if語句,這樣,如果循環完成並且函數未返回,則該數字將成爲素數。 – Sai

+0

一些正確的答案已經發布在下面,但我想補充一些一般的東西。您的回報價值附近的parentesises是多餘的。返回後的值總是在返回之前被計算。 ((a%i!= 0)&&(i == a -1))在兩個檢查周圍的parentesises也是reduntant,雖然這些並不必然是壞的,因爲它們可能會使它更容易閱讀。由於你是一名初級程序員,我希望你知道這一點。接下來,您不檢查提供的輸入是否實際是一個數字,所以如果輸入其他內容,這可能會崩潰。 –

回答

5

i永遠不可能等於a - 1 - 您只能達到b - 1ba/2,這是永遠不會導致匹配。

這意味着你的循環結束條件,將返回1是不正確的。

在一個素數的情況下,跑出循環的結束。這會導致未定義的行爲,因爲您在那裏沒有return語句。鏘給了一個警告,沒有任何特殊的標誌:

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 

總體而言,您的首要檢查迴路要複雜得多它需要。假設你只在乎大於或等於a2

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;

1

你的邏輯是不正確。你正在使用這個表達式(i == a -1)),正如卡爾所說,這絕不會是真的。

例如: -

If a = 11 

b = a/2 = 5 (Fractional part truncated) 

所以,你正在運行的循環,直到i<5。所以i永遠不能等於a-1,因爲在這種情況下i的最大值將是4,並且a-1的值將是10

1

您可以通過檢查直到平方根來完成此操作。但下面是對你的代碼進行一些修改以使其工作。

#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; 

}

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; 

     } 
} 
} 
} 
0
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(); 
    } 
} 
0

下面是查找素數,直到任何用戶輸入的號碼的完整解決方案。

#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(); 
} 
0

一種方法是使用篩分算法,如sieve of 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)); 
} 
+0

它僅適用於<121個號碼 –