#Trying to make a program to return only prime numbers and I'm lost at else if (x % x == 0)

84 messages · Page 1 of 1 (latest)

kind garden
#

`#include <iostream>

using namespace std;

int main() {

//Variables

int min_value, max_value;

//Ask user for min and max value and check

cout << "Please enter a minimum value bigger than 1: " << endl;
cin >> min_value;

while (min_value <= 1){
    cout << "Please enter a minimum value bigger than 1: ";
    cin >> min_value;
}

cout << "Please enter a maximum value: " << endl;
cin >> max_value;

while (min_value == max_value || min_value > max_value){
    cout << "Please enter a valid maximum value: ";
    cin >> max_value;
}

//Out for loop

for (int x : {min_value, max_value}){ //Declare range

    if (x == 2 ) {
        cout << x;
        x++;
    }
    else if (x == 3){
        cout << x;
        x++;
    }
    else if (x % x == 0){
        cout << 6*(x) - 1 << endl;
        x++;
    }
}

return 0;

}`

azure badgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

kind garden
#

Theres a 2 formulas to find prime numbers and its 6x-1 and 6x+1

#

Issue is, when I inserted a min value of 2 and a max value of 15 the output should be: 2,3,5,7,11,13. But it only outputs 2 and for some reason 89 and only those two numbers.

#

So now my question is, how do I first start the counter to increase by an increment of one, and second make it only stay in the range of the specified max value

final eagle
# kind garden Theres a 2 formulas to find prime numbers and its 6x-1 and 6x+1

Those formulas are not sufficient for finding primes. All primes greater than 3 can be expressed with one of those two formulas, but it also works with some non-primes. Consider for example when x = 6. That yields 6 * 6 - 1 = 35, which is not prime.

It's still possible to use those formulas to quickly eliminate some candidates for prime, but those by themselves are not sufficient for finding primes.

Your code is also quite bizarre, but I'm not sure if it's worth the effort to try to fix it when the underlying algorithm won't work.

kind garden
final eagle
kind garden
#

Was the first for loop alright though

#

The for (int x : {min,max})

final eagle
# kind garden Was the first for loop alright though

No. That is not how a normal for loop is written. The code does run, but it iterates only twice. Once for the min_value and a second time for the max_value. The normal code would be:

for(int x=min_value; x<=max_value; x++){
  //loop code body    
}

Also note that it is not normal to modify the iterator inside the loop. This is legal, but it makes reasoning about the code much harder and can cause many bugs that are difficult to understand without using a debugger.

azure badgeBOT
#

@kind garden Has your question been resolved? If so, run !solved :)

kind garden
#

for (int x = min_value; x <= max_value; x++){ for (int num = 2; num <= max_value; num++) if (x % num == 0) cout << x << endl;

#

@final eagle this is what i have for the function currently but I don't know what I'm doing wrong as it still outputs 2-15 but multiple of them

#

i believe im going in the right path but have the if statement wrong

final eagle
#

It's technically possible for the inner loop to iterate up to max_value, but it's not only inefficient. You will need to exclude the comparison when x is equal to num, because a number is always divisible by itself. It would be simpler to not iterate as high as x.

worldly urchin
#

In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prim...

#

take a look at this fast method of determining primes

#

this is what i would use

#

you can store the primes in a std::vector and index them when you need them

#

if there are not enough primes in std::vector, then generate more

kind garden
final eagle
final eagle
kind garden
#

I may not be able to do that

kind garden
#

what am I doing wrong?

#

I've tried a lot of combinations and I either get no ouput at all or get every number repeated like 10 times

final eagle
#

Also, your if and else if conditions are identical.

kind garden
#

So if I only do one if (x % num == 0) and then that cout

#

and then another if statememt?

kind garden
final eagle
kind garden
#

Are you saying don't cout?

#

Since its too early of a decision

final eagle
#

The decision of prime can only happen after all iterations of the inner loop.

kind garden
#

So if I did something

#

that

#

went through all the numbers

#

and checked if it was and wasn't

#

if it wasn't, it would append it to some sort of list

#

and then cout it

#

sorry messed up wording

final eagle
kind garden
#

I don't know

final eagle
#

This exercise is actually a logic puzzle.

kind garden
#

Hm okay

#

So rn I should focus on iterating through the loop and decide if its not prime

#

and see from there

kind garden
#

Okay

#

Also is there a reason

#

why when I originally had an output

#

it would output a bizzare amount of numbers

#

instead of just a singular number

#

for example

#

if i inputted 2 - 15

#

it would output 2 - 15 but like 20-30 times

final eagle
kind garden
#

okay

#

thank you

azure badgeBOT
#

@kind garden Has your question been resolved? If so, run !solved :)

kind garden
#

and did this

#

`//Out for loop
for (int x = min_value; x <= max_value; x++){

    //Assume that it is a prime number from the get go and try to falisfy it

    bool prime = true;

    for (int num = 2; num <= x; num++)

        if (x % num == 0){
            prime = false;
            break;
        }

    if (prime)
        cout << x << " ";
}`
#

i think this is the correct answer but i dont know why it still outputs nothing

#

my teacher is saying it has something to do with the square root of soemthing in the instructions

#

but im not too sure what that means at all

final eagle
#

The usual solution iterates to 1/2 x.

kind garden
#

Okay okay

#

I’ll try it when I’m done eating

kind garden
#

Thanks for all the help man

#

!solved