Notices
924/931/944/951/968 Forum Porsche 924, 924S, 931, 944, 944S, 944S2, 951, and 968 discussion, how-to guides, and technical help. (1976-1995)
Sponsored by:
Sponsored by:

WAY OT: programming in C

Thread Tools
 
Search this Thread
 
Old 02-13-2003 | 09:27 PM
  #1  
Enzo's Nightmare's Avatar
Enzo's Nightmare
Thread Starter
Burning Brakes
 
Joined: Sep 2002
Posts: 1,117
Likes: 0
From: St. Louis
Post WAY OT: programming in C

I have noticed that some people on the Rennlist are computer programmers or have programming experience. I am taking a computer science course and I am stuck on writing a program (in C) that involves printing all the prime numbers less than a number that is entered. I know this is probably a real basic program, but I have been working on it for a few hours and I can't get it right. I have the program that indicates if a number is prime, but that has not been much help yet. All I need is a step in the right direction. Any help will be appreciated. Thank you.
Old 02-13-2003 | 09:36 PM
  #2  
jabbadeznuts's Avatar
jabbadeznuts
Race Car
 
Joined: Sep 2002
Posts: 4,844
Likes: 0
From: Salem, OR (this place is a sh!t hole)
Post

I'm taking Java at the moment, which is similar to C. What does your logic look like? Ire you having trouble with syntax or the logic part? I might be able to help.
Old 02-13-2003 | 09:46 PM
  #3  
Enzo's Nightmare's Avatar
Enzo's Nightmare
Thread Starter
Burning Brakes
 
Joined: Sep 2002
Posts: 1,117
Likes: 0
From: St. Louis
Post

It is not allowing me to post what I have. It is referring to my code as HTML tag.
Old 02-13-2003 | 09:46 PM
  #4  
trebor_quitman's Avatar
trebor_quitman
Burning Brakes
 
Joined: Jun 2001
Posts: 1,111
Likes: 0
From: AZ
Post

Post your code or send it out in an E-mail. It's been awhile, but I think I might be able to remember enough to help. Not sure if I'll have the time before your deadline, but perhaps someone will.
Old 02-13-2003 | 09:47 PM
  #5  
Bri Bro's Avatar
Bri Bro
Addict
Rennlist Member

 
Joined: Feb 2002
Posts: 5,384
Likes: 1
Post

Here is an example on calculating prime numbers.

<a href="http://www.syix.com/wpsjr1/primes.html" target="_blank">http://www.syix.com/wpsjr1/primes.html</a>

You just calculate all the numbers up to the value entered, have fun.
Old 02-13-2003 | 09:48 PM
  #6  
bf95cab's Avatar
bf95cab
Instructor
 
Joined: Feb 2002
Posts: 113
Likes: 0
From: New Jersey
Post

cut and paste the code into the message.
Old 02-13-2003 | 09:52 PM
  #7  
Enzo's Nightmare's Avatar
Enzo's Nightmare
Thread Starter
Burning Brakes
 
Joined: Sep 2002
Posts: 1,117
Likes: 0
From: St. Louis
Post

Sorry, we do not permit this HTML tag:Parenthesis in HTML tag
This is the message I am getting when trying to post my code. Let me work with it a little more. Thanks for everyone's response.
Old 02-13-2003 | 10:18 PM
  #8  
Tabor's Avatar
Tabor
Drifting
 
Joined: May 2001
Posts: 2,779
Likes: 0
From: Portland, OR
Post

You need to make a loop that tests every number between 0 and the number that was entered. When the number is prime, print it out. You can do this with either a for() loop or a while() loop.
Old 02-13-2003 | 10:55 PM
  #9  
led's Avatar
led
Three Wheelin'
 
Joined: Aug 2002
Posts: 1,598
Likes: 1
From: San Juan, PR
Post

</font><blockquote><font size="1" face="Verdana,Tahoma,Arial,Helvetica,Geneva">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#include &lt;stdio.h&gt;

void main()
{
int i,j,num;
printf(&quot;Enter the limit number: &quot;
scanf(&quot;%d&quot;, &amp;num);

for(i=3;i&lt;=num;i++)/*iterates through numbers from 3 to the number entered*/
{
for(j=i-1;j&gt;1;j--)/*iterates backwards from i-1 checking to see if i is divisible by every other number &quot;before&quot; it.*/
{
if(!(i%j)) /*if i%j==0, the number is divisible by another(not prime)*/
break;/*breaks out of the innermost for loop*/
}
if(j==1) /*if j==1 the for above finished without finding i%j == 0 therefore the number in i is prime*/
printf(&quot;%d &quot;,i);
}
printf(&quot;\n&quot;
}</pre><hr /></blockquote><font size="2" face="Verdana,Tahoma,Arial,Helvetica,Geneva"><img border="0" alt="[thumbsup]" title="" src="graemlins/bigok.gif" />
edit: I edited the comments a bit but couldn't get italics to work <img border="0" title="" alt="[Frown]" src="frown.gif" />
Old 02-13-2003 | 11:06 PM
  #10  
Enzo's Nightmare's Avatar
Enzo's Nightmare
Thread Starter
Burning Brakes
 
Joined: Sep 2002
Posts: 1,117
Likes: 0
From: St. Louis
Post

YOU ARE MY IDOL!!! I'll be sure to put your name on the assignment. I don't like stealing credit from people.
Old 02-13-2003 | 11:15 PM
  #11  
led's Avatar
led
Three Wheelin'
 
Joined: Aug 2002
Posts: 1,598
Likes: 1
From: San Juan, PR
Post

Finally, after about a dozen edits I think I'm happy with the look of the comments. <img border="0" alt="[ouch]" title="" src="graemlins/c.gif" />

p.s. I won't consider it stolen if you learn from it.
Old 02-14-2003 | 03:18 AM
  #12  
rcldesign's Avatar
rcldesign
Racer
 
Joined: Aug 2002
Posts: 457
Likes: 0
From: San Diego, CA
Post

Ok... that algorithim is quick and ditry, and it will take ages to determine larger primes. I therefore suggest the following method using a dynamic algorithim

I didn't compile/test/etc, so for all I know there are bugs, typos, etc.; but the math is good

</font><blockquote><font size="1" face="Verdana,Tahoma,Arial,Helvetica,Geneva">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
/* This is the number that n cannot exceed, perhaps something
like 32767 or 65535 would be better */
#define MAX_PRIMES 10000

/* Global Variables */
/* Array to hold prime numbers */
long primes[MAX_PRIMES];

/* Current position in prime array index */
int iIndex;

/* Loop variable */
int i;

void Generate(long n)
{
/* The first prime is 1, put it in the array
This is needed as a starting point for the
algorithim */
iIndex = 0;
primes[iIndex] = 1;

for (i = 2; i &lt; n; i++)
{
if (IsPrime(i) == TRUE)
{
iIndex++;
primes[iIndex] = i;
}
}
}

bool IsPrime(long n)
{
long i;
long lRoot;

lRoot = sqrt(n);
for (i = 1; i &lt;= iIndex; i++)
{
if (primes[i] &lt;= lRoot)
{
if ((n % primes(iIndex)) == 0)
return FALSE;
} else {
return TRUE;
}
}
return TRUE;
}</pre><hr /></blockquote><font size="2" face="Verdana,Tahoma,Arial,Helvetica,Geneva">But if you're in a class where making a simple prime number generator is your assignment, then using an algorithim like above will probably get you suspected of cheating <img border="0" title="" alt="[Wink]" src="wink.gif" />
Old 02-14-2003 | 11:33 AM
  #13  
Tremelune's Avatar
Tremelune
Three Wheelin'
 
Joined: Mar 2002
Posts: 1,725
Likes: 24
From: Los Angeles
Post

For the future, arstechnica.com has an excellent forum, one if which is dedicated to coding. The folks around there are often very smrt.
Old 02-14-2003 | 03:50 PM
  #14  
Enzo's Nightmare's Avatar
Enzo's Nightmare
Thread Starter
Burning Brakes
 
Joined: Sep 2002
Posts: 1,117
Likes: 0
From: St. Louis
Post

Once again, thank you everyone for your great help.



Quick Reply: WAY OT: programming in C



All times are GMT -3. The time now is 03:02 AM.