C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 26th, 2005, 02:55 PM
J_Gra J_Gra is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 2 J_Gra User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 16 sec
Reputation Power: 0
Exclamation 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.

Reply With Quote
  #2  
Old August 26th, 2005, 02:59 PM
J_Gra J_Gra is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 2 J_Gra User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #3  
Old August 26th, 2005, 03:08 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
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

Reply With Quote
  #4  
Old August 26th, 2005, 03:25 PM
crownjewel82 crownjewel82 is offline
rebel with a cause
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,817 crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)  Folding Points: 53188 Folding Title: Beginner FolderFolding Points: 53188 Folding Title: Beginner FolderFolding Points: 53188 Folding Title: Beginner Folder
Time spent in forums: 3 Months 4 Days 12 h 16 m 39 sec
Reputation Power: 3460
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.

Reply With Quote
  #5  
Old August 26th, 2005, 04:55 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #6  
Old August 26th, 2005, 05:47 PM
rjett3345 rjett3345 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 rjett3345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old August 29th, 2005, 02:50 PM
CASSANOVA CASSANOVA is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: KOLKATA
Posts: 39 CASSANOVA User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.......

Reply With Quote
  #8  
Old August 29th, 2005, 03:34 PM
crownjewel82 crownjewel82 is offline
rebel with a cause
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,817 crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 37th Grade (Above 100000 Reputation Level)  Folding Points: 53188 Folding Title: Beginner FolderFolding Points: 53188 Folding Title: Beginner FolderFolding Points: 53188 Folding Title: Beginner Folder
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > finding if a number is prime and another ?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap