Author Topic: 1st Prime number Finder  (Read 5711 times)

0 Members and 1 Guest are viewing this topic.

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
1st Prime number Finder
« on: July 19, 2011, 09:31:19 pm »
Hey, so I've been learning my c++ and I've been trying to solve a problem.
So far I've learned about
while, for, loops
if,else statements
Functions
I guess, pretty much the basics.
So i was presented with a problem, the problem being
Quote
Write a program that lets the user enter any number n and then finds
the first prime number larger than n.
So my soloution was
Code: [Select]
#include "stdafx.h"
#include <iostream>
#include <math.h>

double square (double a) ; // square root finding function

int modulus(int a, int b , double c, int d) ; // function to find whether number is prime or not

using namespace std ;

int main ()
{
    int prime = 0 ;
    double n; // Number that prime number must be > than.
    double i ; //Number to test primeness on
   
    int l=2; //loop
    cout << "Please enter the value that the first prime number must be greater than! " << endl ;
    cin >> n ;
    i=n    ;
    i++; // set i greater than n

    while(i<n){
        modulus (i, l, n, prime) ;
            if ( prime=0){ // if modulus returns prime=0
                i++ ;    } // increase i, to run loop again, with greater number
            if (prime=1)// if modulus returns prime=1
                cout << "The number " << i << " is the first prime number after " << n << endl;
            break; // end loop as prime number is found
   
    }

    getchar();
    getchar();
    }


double square (double a)
{
    return sqrt(a);
}

int modulus (int a, int b, double c, int d) {
    for (b=2; b<=square(c) ; b++) {
        if (a%b==0){ // This would mean the number is not prime
            d=0 ;
            return d=0 ;
        }
        if (b>=square(c)) { // if b becomes equal to or greater than the number, the number would be prime
            return d=1;
        }
       
    } 
}

I've looked at this code a million times and I just cant figure out why it doesnt work?
It builds succesfully, (just gives me a warning on conversion from double to int, which is fine)
When i run it however, i get to the first step, entering the number which the prime number must be > than
BUT then the program just asks me to press any key to continue and exits!
It totally skips my loop, although the condition is already met!
Can someone help me out here???

Offline ElectricNoodle

  • Serf
  • *
  • Posts: 38
  • Cookies: 6
    • View Profile
Re: 1st Prime number Finder
« Reply #1 on: July 19, 2011, 09:40:23 pm »
I think I see where the error is... I haven't tried it though,

after your:

Code: [Select]
cin>>n;
i=n;
i++;

you say:
Code: [Select]
while(i<n)
"i" is never going to be less than "n" because you first made it equal to "n", then incremented it by one? Therefore your code inside the while statement is never being used. :) Hope that helps!!

Edit:

Also in your Modulus function, there is no need to say:
Code: [Select]
return d=0;if you want it to return the value of d then this will do:
Code: [Select]
return d;
You also seem to be doing a few things that aren't really needed.. :P
If I wasn't off on my way to work I'd stick it into a compiler and have a better look! But if your still stuck when I get in in the morning then I'll load it up and try help :P
« Last Edit: July 19, 2011, 09:53:11 pm by ElectricNoodle »

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: 1st Prime number Finder
« Reply #2 on: July 19, 2011, 10:43:09 pm »
Hey, i removed d=0 and put it as just
Code: [Select]
return d;but still no luck : (


iMorg

  • Guest
Re: 1st Prime number Finder
« Reply #3 on: July 19, 2011, 11:30:44 pm »
Well, your square function does nothing but wrap the sqrt function so it is not needed.

Now the real problem(why it is skipping the loop) electricnoodle answered.

in this code:
Code: [Select]
    cin >> n ;
    i=n    ;
    i++; // set i greater than n

    while(i<n){

remove the i++ statement and change "while(i<n)" to "while(i<=n)"

There is probably more than that but im tired right now.

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: 1st Prime number Finder
« Reply #4 on: July 20, 2011, 12:11:14 am »
Well, your square function does nothing but wrap the sqrt function so it is not needed.

Now the real problem(why it is skipping the loop) electricnoodle answered.

in this code:
Code: [Select]
    cin >> n ;
    i=n    ;
    i++; // set i greater than n

    while(i<n){

remove the i++ statement and change "while(i<n)" to "while(i<=n)"

There is probably more than that but im tired right now.
Okay, i did that and now when i type in a number for the first time, then i get the next number after that as a result!
And im not typing in a prime number.
Quote
the number 25 is the first prime number after 24
« Last Edit: July 20, 2011, 12:14:03 am by Lionofgod »

Offline petermlm

  • Knight
  • **
  • Posts: 226
  • Cookies: 7
  • Information is Power
    • View Profile
    • Security Check
Re: 1st Prime number Finder
« Reply #5 on: July 20, 2011, 12:27:57 am »
Okay, i did that and now when i type in a number for the first time, then i get the next number after that as a result!
And im not typing in a prime number.

Can you post the last version of the code?

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: 1st Prime number Finder
« Reply #6 on: July 20, 2011, 01:29:15 am »
Can you post the last version of the code?
yep :D
Code: [Select]
#include "stdafx.h"
#include <iostream>
#include <math.h>

double square (double a) ; // square root finding function

int modulus(int a, int b , double c, int d) ; // function to find whether number is prime or not

using namespace std ;

int main ()
{
    int prime = 0 ;
    double n; // Number that prime number must be > than.
    double i ; //Number to test primeness on
   
    int l=2; //loop
    cout << "Please enter the value that the first prime number must be greater than! " << endl ;
    cin >> n ;
    i=n    ;
   
    while(i>=n){
        modulus (i, l, n, prime) ;
            if ( prime=0){ // if modulus returns prime=0
                i++ ;    } // increase i, to run loop again, with greater number
            if (prime=1)// if modulus returns prime=1
                cout << "The number " << i << " is the first prime number after " << n << endl;
            break; // end loop as prime number is found
   
    }

    getchar();
    getchar();
    }


double square (double a)
{
    return sqrt(a);
}

int modulus (int a, int b, double c, int d) {
    for (b=2; b<=square(c) ; b++) {
        if (a%b==0){ // This would mean the number is not prime
            d=0 ;
            return d ;
        }
        if (b>=square(c)) { // if b becomes equal to or greater than the number, the number would be prime
            return d=1;
        }
       
    } 
}

Offline petermlm

  • Knight
  • **
  • Posts: 226
  • Cookies: 7
  • Information is Power
    • View Profile
    • Security Check
Re: 1st Prime number Finder
« Reply #7 on: July 20, 2011, 01:55:19 am »
I have not yet seen the rest of the code, but I can tell you this. If you want to know prime numbers and work with prime numbers in this, use only integer variables. Even if the input in a double, make a cast to int like this:

Code: [Select]
int a;
double b = 10.23;
a = (int) b; //a = 10

 
Code: [Select]
int a;
 double b = 10.99;
 a = (int) b; //a = 10

(Note this is not rounding the number. If it were then in the second code a would be equal to 11)

Offline petermlm

  • Knight
  • **
  • Posts: 226
  • Cookies: 7
  • Information is Power
    • View Profile
    • Security Check
Re: 1st Prime number Finder
« Reply #8 on: July 20, 2011, 02:14:55 am »
(I am sorry for the double post. But I really felt it was necessary.)

Ok. So a few thinks. I solved your problem, the solutions is below. But please note this:

First you code is a little "missed shaped". What I mean is, the place where you put the {} signs, and some other symbols are not always the same. Even if this is not confusing for many people it is still a good programming practice to adopt an indentation style. See this page for details: http://en.wikipedia.org/wiki/Indent_style.

Another think. I noticed that you had an argument in the modulus (modulus(i, l, n, prime)) function called prime. And then you had, inside that function something saying return d = 0, and then outside modulus you had an if testing if prime was equal to 0 or 1. This is a bit of a logical error. If you send prime to the function, inside modulus d will take it's value. But prime won't be altered. I hope by the alteration I made you can check the difference.

Also, I had to alter the name of the function modulus to modulus2 because modulus already exists in the c++ std namespace.

Happy Hacking!

Code: [Select]
#include <iostream>
#include <stdio.h>
#include <math.h>

int square (int a); // square root finding function

int modulus2(int a, int c); // function to find whether number is prime or not

using namespace std;

int main ()
{
    double input;
    int n; // Number that prime number must be > than.
    int i ; //Number to test primeness on
   
    cout << "Please enter the value that the first prime number must be greater than! " << endl ;
    cin >> input;
    n = (int)input;
    i= n;
   
    while(1)
    {
            if(modulus2(i, n) == 0) // if modulus returns prime=0
                i++;
               
            else if(modulus2(i, n) == 1) // if modulus returns prime=1
            {
                cout << "The number " << i << " is the first prime number after " << n << endl;
                break; // end loop as prime number is found
            }
    }

    getchar();
    getchar();
}


int square (int a)
{
    return sqrt((double)a);
}

int modulus2(int a, int c)
{
    for (int b=2; b<=square(c) ; b++)
        if (a%b==0) // This would mean the number is not prime
            return 0;
    return 1;
}
« Last Edit: July 20, 2011, 02:26:38 am by petermlm »

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: 1st Prime number Finder
« Reply #9 on: July 20, 2011, 02:53:02 am »
(I am sorry for the double post. But I really felt it was necessary.)

Ok. So a few thinks. I solved your problem, the solutions is below. But please note this:

First you code is a little "missed shaped". What I mean is, the place where you put the {} signs, and some other symbols are not always the same. Even if this is not confusing for many people it is still a good programming practice to adopt an indentation style. See this page for details: http://en.wikipedia.org/wiki/Indent_style.

Another think. I noticed that you had an argument in the modulus (modulus(i, l, n, prime)) function called prime. And then you had, inside that function something saying return d = 0, and then outside modulus you had an if testing if prime was equal to 0 or 1. This is a bit of a logical error. If you send prime to the function, inside modulus d will take it's value. But prime won't be altered. I hope by the alteration I made you can check the difference.

Also, I had to alter the name of the function modulus to modulus2 because modulus already exists in the c++ std namespace.

Happy Hacking!

Code: [Select]
#include <iostream>
#include <stdio.h>
#include <math.h>

int square (int a); // square root finding function

int modulus2(int a, int c); // function to find whether number is prime or not

using namespace std;

int main ()
{
    double input;
    int n; // Number that prime number must be > than.
    int i ; //Number to test primeness on
   
    cout << "Please enter the value that the first prime number must be greater than! " << endl ;
    cin >> input;
    n = (int)input;
    i= n;
   
    while(1)
    {
            if(modulus2(i, n) == 0) // if modulus returns prime=0
                i++;
               
            else if(modulus2(i, n) == 1) // if modulus returns prime=1
            {
                cout << "The number " << i << " is the first prime number after " << n << endl;
                break; // end loop as prime number is found
            }
    }

    getchar();
    getchar();
}


int square (int a)
{
    return sqrt((double)a);
}

int modulus2(int a, int c)
{
    for (int b=2; b<=square(c) ; b++)
        if (a%b==0) // This would mean the number is not prime
            return 0;
    return 1;
}
Thank you very much this will help me learn a lot :D
I will also take a look at the indentation style you mentioned.
Code: [Select]
karma ++ ;

« Last Edit: July 20, 2011, 02:53:35 am by Lionofgod »

Offline petermlm

  • Knight
  • **
  • Posts: 226
  • Cookies: 7
  • Information is Power
    • View Profile
    • Security Check
Re: 1st Prime number Finder
« Reply #10 on: July 20, 2011, 11:29:08 am »
Code: [Select]
karma ++ ;

Ah! Thank you very must!!