Jump to content

Prime Number C++

Go to solution Solved by itsmyjobtoknow,
Just now, Kagami Tsukino said:

I try adding bunch of bracket and I still do not get it. still having same problem. 

  Hide contents

void primeNumber(int num)
{
    ofstream primeNumbersInput;
    primeNumbersInput.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
    {
        for (int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                break;
            }
            else if (i == j + 1)
            {
                cout << i << " " << endl;
            }
            primeNumbersInput << i << endl;
        }
    }
    primeNumbersInput.close();
}

 

Well....you only needed to put one set of brackets in there.

Another Hint: look at your nested for loop, something is misplaced by a single line which is causing all the issues.

I need help for my project. I just need help with my .txt file it keep repeating the number multiple time, but with the cmd it only repeat it once. Can someone help me how to I make my text file write it once

Spoiler

#include <iostream>
#include <fstream>

using namespace std;
void primeNumber(int);

int main()
{

    int num;
    
    cout << "Would you please enter an integer number that should be less than 3001." << endl;
    cin >> num;
    if (num < 3001)
    {}
    else
    {
        do 
        {
            cout << "Your input should be less than 3001. Try again." << endl
                << "Please enter an integer number that should be less than 3001." << endl;
            cin >> num;
        } while (num > 3001);
    }
    
    primeNumber(num);

    return 0;
}

void primeNumber(int num)
{
    ofstream primeNumber;
    primeNumber.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
        for (int j = 2; j<i; j++)
        {
            if (i % j == 0)
                break;
            else if (i == j + 1)
                cout << i << " "<< endl;
            primeNumber << i << endl;
        }
    
    primeNumber.close();
}

 

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/
Share on other sites

Link to post
Share on other sites

8 minutes ago, itsmyjobtoknow said:

Look at your primeNumber function.

sorry, but what do you mean if i change the primeNumber function. Still an armature with programming. 

 

the text file will be like this if i just type 10 as input.

Spoiler

3
5
5
5
7
7
7
7
7
9

 

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8874993
Share on other sites

Link to post
Share on other sites

What happens if multiple numbers between i and j divide num?

 

Also, your algorithm doesn't work. Just because i == j + 1 does not mean it's prime.

 

EDIT: I think I've misunderstood what you algorithm is trying to do, are you trying to find all primes up to a max?

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875011
Share on other sites

Link to post
Share on other sites

3 minutes ago, Blade of Grass said:

What happens if multiple numbers between i and j divide num?

 

Also, your algorithm doesn't work. Just because i == j + 1 does not mean it's prime.

But I did run the program and it did find me prime. All I need to know is why my test file writing multiple of the same number while the cout only show once.

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875026
Share on other sites

Link to post
Share on other sites

8 minutes ago, Kagami Tsukino said:

sorry, but what do you mean if i change the primeNumber function. Still an armature with programming. 

 

the text file will be like this if i just type 10 as input.

  Hide contents

3
5
5
5
7
7
7
7
7
9

 

Oh, misunderstood what you meant...but still, the source of the problem is still in that function. ...hint: you're missing brackets

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875032
Share on other sites

Link to post
Share on other sites

5 minutes ago, itsmyjobtoknow said:

Oh, misunderstood what you meant...but still, the source of the problem is still in that function. ...hint: you're missing brackets

I try adding bunch of bracket and I still do not get it. still having same problem. 

Spoiler

void primeNumber(int num)
{
    ofstream primeNumbersInput;
    primeNumbersInput.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
    {
        for (int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                break;
            }
            else if (i == j + 1)
            {
                cout << i << " " << endl;
            }
            primeNumbersInput << i << endl;
        }
    }
    primeNumbersInput.close();
}

 

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875056
Share on other sites

Link to post
Share on other sites

Just now, Kagami Tsukino said:

I try adding bunch of bracket and I still do not get it. still having same problem. 

  Hide contents

void primeNumber(int num)
{
    ofstream primeNumbersInput;
    primeNumbersInput.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
    {
        for (int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                break;
            }
            else if (i == j + 1)
            {
                cout << i << " " << endl;
            }
            primeNumbersInput << i << endl;
        }
    }
    primeNumbersInput.close();
}

 

Well....you only needed to put one set of brackets in there.

Another Hint: look at your nested for loop, something is misplaced by a single line which is causing all the issues.

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875066
Share on other sites

Link to post
Share on other sites

14 minutes ago, Blade of Grass said:

What happens if multiple numbers between i and j divide num?

 

Also, your algorithm doesn't work. Just because i == j + 1 does not mean it's prime.

 

EDIT: I think I've misunderstood what you algorithm is trying to do, are you trying to find all primes up to a max?

yes I am trying to find all prime number and display it. At the same time should also save it but only once in text file.

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875068
Share on other sites

Link to post
Share on other sites

3 minutes ago, Kagami Tsukino said:

I try adding bunch of bracket and I still do not get it. still having same problem. 

  Reveal hidden contents

void primeNumber(int num)
{
    ofstream primeNumbersInput;
    primeNumbersInput.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
    {
        for (int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                break;
            }
            else if (i == j + 1)
            {
                cout << i << " " << endl;
            }
            primeNumbersInput << i << endl;
        }
    }
    primeNumbersInput.close();
}

 

Sidenote: I already know the solution, already tested it on my end, just want to see if you realize how simple of a mistake it is.

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875074
Share on other sites

Link to post
Share on other sites

@Kagami Tsukino have you tried tracing your code? I mean try it by hand on paper, line by line.

 

Also, your code misses the prime 2

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875082
Share on other sites

Link to post
Share on other sites

4 minutes ago, itsmyjobtoknow said:

Sidenote: I already know the solution, already tested it on my end, just want to see if you realize how simple of a mistake it is.

Spoiler

void primeNumber(int num)
{
    ofstream primeNumbersInput;
    primeNumbersInput.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
    {
        for (int j = 2; j < i; j++)

            if (i % j == 0)

                break;
            else if (i == j + 1)
                cout << i << " " << endl;
        primeNumbersInput << i << endl;

    }
    primeNumbersInput.close();
}

Finally found it. XP. Now i got to figure out why 2 is missing while text file have 2.

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875107
Share on other sites

Link to post
Share on other sites

1 minute ago, Kagami Tsukino said:
  Hide contents

void primeNumber(int num)
{
    ofstream primeNumbersInput;
    primeNumbersInput.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
    {
        for (int j = 2; j < i; j++)

            if (i % j == 0)

                break;
            else if (i == j + 1)
                cout << i << " " << endl;
        primeNumbersInput << i << endl;

    }
    primeNumbersInput.close();
}

Finally found it. XP. Now i got to figure out why 2 is missing while text file have 2.

If your text file has two I was just saying it's missing based off what you posted as the output. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875117
Share on other sites

Link to post
Share on other sites

4 minutes ago, Kagami Tsukino said:
  Hide contents

void primeNumber(int num)
{
    ofstream primeNumbersInput;
    primeNumbersInput.open("user_number_input.txt");

    for (int i = 2; i < num; i++)
    {
        for (int j = 2; j < i; j++)

            if (i % j == 0)

                break;
            else if (i == j + 1)
                cout << i << " " << endl;
        primeNumbersInput << i << endl;

    }
    primeNumbersInput.close();
}

Finally found it. XP. Now i got to figure out why 2 is missing while text file have 2.

The code you put in the hidden content looks the same as the one you posted before. 

Link to comment
https://linustechtips.com/topic/691983-prime-number-c/#findComment-8875130
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×