Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
August 26th, 2005, 02:55 PM
 J_Gra
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.

#2
August 26th, 2005, 02:59 PM
 J_Gra
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

#3
August 26th, 2005, 03:08 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
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

#4
August 26th, 2005, 03:25 PM
 crownjewel82
rebel with a cause

Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,817
Time spent in forums: 3 Months 4 Days 12 h 16 m 39 sec
Reputation Power: 3460
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.

#5
August 26th, 2005, 04:55 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,141
Time spent in forums: 2 Months 2 Weeks 4 Days 5 m 55 sec
Reputation Power: 1974
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.

#6
August 26th, 2005, 05:47 PM
 rjett3345
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)
{
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.

#7
August 29th, 2005, 02:50 PM
 CASSANOVA
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.......

#8
August 29th, 2005, 03:34 PM
 crownjewel82
rebel with a cause

Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,817
Time spent in forums: 3 Months 4 Days 12 h 16 m 39 sec
Reputation Power: 3460
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

 Viewing: Dev Shed Forums > Programming Languages > C Programming > finding if a number is prime and another ?