The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
finding if a number is prime and another ?
Discuss finding if a number is prime and another ? in the C Programming forum on Dev Shed. finding if a number is prime and another ? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 26th, 2005, 02:55 PM
|
|
Registered User
|
|
Join Date: Aug 2005
Posts: 2
Time spent in forums: 8 m 16 sec
Reputation Power: 0
|
|
finding if a number is prime and another ?
I am doing this independent study on programming and i am stuck. How would u figure out if a number is prime? Is there a function u can call?
My next question is how would u figure out if a number is a multiple of 7, 11, or 13.
thanks in advance.
|

August 26th, 2005, 02:59 PM
|
|
Registered User
|
|
Join Date: Aug 2005
Posts: 2
Time spent in forums: 8 m 16 sec
Reputation Power: 0
|
|
|
this is the c programming language
i am doing a class on c programming not c++... thanks again
|

August 26th, 2005, 03:08 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Quote: | Originally Posted by J_Gra I am doing this independent study on programming and i am stuck. How would u figure out if a number is prime? Is there a function u can call?
My next question is how would u figure out if a number is a multiple of 7, 11, or 13.
thanks in advance. |
There isn't a predefined function you can call. You have to write your own, which is the point of the exercise.
As for finding if a number is divisible by 7, 11 or 13, that's what the / operator is for.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

August 26th, 2005, 03:25 PM
|
|
rebel with a cause
|
|
Join Date: May 2004
Location: The Batsh!t Crazy State.
|
|
Google is your friend:
prime numbers
you might want the % operator instead of /
__________________
Dear God. What is it like in your funny little brains? It must be so boring.
|

August 26th, 2005, 04:55 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
You might want to research "Sieve of Eratosthenes". I remember it used to be used extensively for benchmarking.
Hey, guys, I'm not doing his work for him. Just pointing him in the right direction.
Turns out that years ago I had recreated the Sieve, though not completely in its classical form, in solving a different problem: taking an arbitrary number of data items and storing them in a 2-D array that was as square as I could make it. That was about 20 years ago, so I forget now why I needed to do that.
Last edited by dwise1_aol : August 26th, 2005 at 04:59 PM.
|

August 26th, 2005, 05:47 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 21
Time spent in forums: 6 h 2 m 54 sec
Reputation Power: 0
|
|
dont wanna do ur work for you but, theres an absolute simple way to do this. with a for statement.
IF YOU DONT WANT TO SEE THAT ANSWER DONT LOOK AT THE CODE.
Code:
primeNum = True;
for (i = 2; i < num; i++)
{
if ((a % i) == 0)
{
primeNum = False;
break;
}
}
if (primNum)
cout << "prime number\n";
else
cout << "Not prime number\n";
this is in c++ since i dont use c, but you get the idea. If you think about things enough some things can be solved very simply . This simple code goes from 2 to the number -1 and if its evenly divisible by anything its not a prime. Very simple.
|

August 29th, 2005, 02:50 PM
|
|
Contributing User
|
|
Join Date: Jul 2005
Location: KOLKATA
Posts: 39
Time spent in forums: 11 h 41 m 50 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by rjett3345 dont wanna do ur work for you but, theres an absolute simple way to do this. with a for statement.
IF YOU DONT WANT TO SEE THAT ANSWER DONT LOOK AT THE CODE.
Code:
primeNum = True;
for (i = 2; i < num; i++)
{
if ((a % i) == 0)
{
primeNum = False;
break;
}
}
if (primNum)
cout << "prime number\n";
else
cout << "Not prime number\n";
this is in c++ since i dont use c, but you get the idea. If you think about things enough some things can be solved very simply . This simple code goes from 2 to the number -1 and if its evenly divisible by anything its not a prime. Very simple. |
We can simply move upto the square-root of the number !!!
No need to check until i becomes the number itself.......
|

August 29th, 2005, 03:34 PM
|
|
rebel with a cause
|
|
Join Date: May 2004
Location: The Batsh!t Crazy State.
|
|
|
while rjett3345's code will work, it is only efficient for small numbers. It is extremely inefficient for large numbers. Also only odd numbers need to be checked. if x isn't divisible by 2 then it's not going to be divisible by 4
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|