WAY OT: programming in C
#1
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.
#5
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.
<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.
#7
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.
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.
Trending Topics
#9
</font><blockquote><font size="1" face="Verdana,Tahoma,Arial,Helvetica,Geneva">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#include <stdio.h>
void main()
{
int i,j,num;
printf("Enter the limit number: "
scanf("%d", &num);
for(i=3;i<=num;i++)/*iterates through numbers from 3 to the number entered*/
{
for(j=i-1;j>1;j--)/*iterates backwards from i-1 checking to see if i is divisible by every other number "before" 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("%d ",i);
}
printf("\n"
}</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" />
void main()
{
int i,j,num;
printf("Enter the limit number: "
scanf("%d", &num);
for(i=3;i<=num;i++)/*iterates through numbers from 3 to the number entered*/
{
for(j=i-1;j>1;j--)/*iterates backwards from i-1 checking to see if i is divisible by every other number "before" 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("%d ",i);
}
printf("\n"
}</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" />
#11
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.
p.s. I won't consider it stolen if you learn from it.
#12
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 < 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 <= iIndex; i++)
{
if (primes[i] <= 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" />
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 < 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 <= iIndex; i++)
{
if (primes[i] <= 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" />