0

I have this code:

#include <iostream>
#include <cmath>

using namespace std;

int n, liczba;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> liczba;
        if (liczba < 2) {
            cout << "NIE" << endl;
        } else if (liczba == 2) {
            cout << "TAK" << endl;
        }

        for (int i = 2; i < liczba; i++) {
            if (liczba % i == 0) {
                cout << "NIE" << endl;
                break;
            } else if (liczba % i != 0) {
                cout << "TAK" << endl;
                break;
            }
        }
    }
    return 0;
}

This program is supposed to write yes "TAK" or no "NIE" whether the number you input is prime or isn't. Variable n is the number of numbers you want to input into program, and liczba is the number you want to check if it's prime or not. It seems to work fine expect one significant thing. If I input number 9 it says yes "TAK" instead of no "NIE".. I discovered that this happens to numbers: 9,27,45,63,81 and so on.. if I add 18 starting from 9 it will happen every time.

What's wrong with my code?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Konradek
  • 33
  • 3

3 Answers3

3

You're breaking on BOTH sides of your if() test. Effectly you'll only ever test one divisor:

e.g. liczba = 9

1. if (liczba % 2 == 0) -> if (9 % 2 == 0) -> if (1 == 0) -> false
2. ...jump to else
3. if (liczba % 2 != 0) -> if (9 % 2 != 0) -> if (1 != 0) -> TRUE
4. spit out 'tak' and break out of the loop

You cannot break out of the loop "early" if you get a remainder. That means the divisor you tested is NOT a factor of the number. You can only break early if you DO get a remainder of 0, which means the number's not prime - it's composite.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks for help, to all of You guys! :) It is brighter to me now ;) How to start my adventure with C++? I have book but I would rather do something interactive like Codecademy? I know that there is no course on Codecademy but I just gave an example. I know it may sound rash or reckless but I've read couple of pages of this book (about 120 I guess) and I would code something myself rather than only acquire knowledge without practical use. What do You think? – Konradek Jul 28 '16 at 21:06
3

Hint:

All prime numbers (except 2 and 3) can be expressed in the form 6k+1 or 6k-1, where k is a positive whole number.

Therefore, This should work:

bool IsPrime( int number )
{

 if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
      return (false);

 for( int k = 1; 36*k*k-12*k < number;++k)
     if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
         return (false);
     return true;
}

Taken from Determining if a number is prime.

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
1

You have not used a flag and thus it says NIE after first check. Also, you are missing an else statement. Try this instead:

#include <iostream>
#include <cmath>
using namespace std;
int n,liczba;
int main()
{
    cin >> n; //
    for(int i=0;i<n;i++)
        {
            cin>>liczba;
            if (liczba < 2)
                {
                cout << "NIE" << endl;
                }
            else if (liczba == 2)
            {
                cout << "TAK" << endl;
            }
            else
            {
                bool isPrime = true;
                for (int i=2;i<liczba;i++)
                {

                    if (liczba % i == 0)
                        {
                            isPrime = false;
                            break;
                        } 
                }
                if(isPrime)
                    cout<<"TAK";
                else
                    cout<<"NIE";
            }

        }
    return 0;
}

Currently, if you input 7, It will check if 7 % 2 == 0. Since it is not, it will print "NIE" and break out of the loop.

Vaibhav Bajaj
  • 1,934
  • 16
  • 29