d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Best Way In C++ Get 2 Input Positive Number > Return All Prime Numbers Between Two Num
Add Reply New Topic New Poll
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Sep 3 2013 01:17am
what would be the best way to do this?
like a general flowchart

would u use recursive? or no need?

just do like

input number , input number - 1
then divide the input num by -input number -1 and if remainder = 0 , prime = true

then when it hits 2 then set to false?
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Sep 3 2013 02:08am
Quote (Haxs @ Sep 3 2013 12:17am)
what would be the best way to do this?
like a general flowchart

would u use recursive? or no need?

just do like

input number , input number - 1
then divide the input num by -input number -1  and if remainder = 0 , prime = true

then when it hits 2  then set to false?

That is pretty much it. There are small things you can to do to improve on this, but they are minor.

for all integers greater than 1:

either are prime, or composite
composite numbers can always be represented as the product of 2 or more primes

idea 1 (this one is solid and you should consider using it):
Therefore if the number x is composite, it will have a prime factor that is equal to or smaller than sqrt(x) (round up)
So to find out if X is prime you only need to test if it can be factored by any number in the range 2 to sqrt(x)

idea 2 (this one has scaling problems with very large numbers):
Furthermore if the numbers are small enough, you could create/store a list of known primes (equal too or smaller than the square root of the largest value you plan on testing) and reuse them. For example:
Lets say you want to know what numbers are prime in the range 50 to 60. The square root of 60 is about 7.7 so we will use 8. Simply test if 2 - 8 are prime, then test those primes to see if they are factors of 50 to 60.

This post was edited by Azrad on Sep 3 2013 02:14am
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 3 2013 07:43am
Is this some sort of college assignment where you have to code it yourself or is it fine to use a library? GMP as a few functions to find primes.
Member
Posts: 1,732
Joined: Sep 3 2007
Gold: 5,991.00
Sep 4 2013 04:39pm
Return or print?

Because if print something like this would work:

#include <iostream>
using namespace std;

void primes(int x, int y){
if(x<1) x = 1; // 1, 0, and negatives cannot be prime so it makes no sense to look below that
if(y<1) y = 1;
if(y>x){
int temp = x;
x = y;
y = x;
}
for(int c = x+1; c < y; c++){
int factors = 0;
for(int d = 1; d <= c; d++){
if(c%d==0) factors++; // bit process intensive, here we look for all factors of the number c between 1 and itself
}
if(factors==2) cout << c << endl; // Prime numbers have two factors, 1 and itself, 1 is not a prime
}
}

int main(){
int x, y;
cout << "Please enter 2 positive integers, between which we will look for primes: ";
cin >> x >> y;
primes(x, y);
return 0;
}

This post was edited by NeX_1337 on Sep 4 2013 04:48pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll