Help ..C program for finding Prime numbers ...issues

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
zeeshan Hussain
Lance Naik
Posts: 28
Joined: Tue Sep 02, 2003 1:16 am
Location: Karachi
Contact:

Help ..C program for finding Prime numbers ...issues

Post by zeeshan Hussain »

hi friends, im a newbie at C,

im trying to code for a program which cud return all prime numbers in a specific range.
its returning the results with an error, which is , it also includes the products of two prime numbers in results,
for example 15=3*5 and 21=3*7

my code is
---------------------------------------------------------
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
for (int i=1;i<=300;i++)
{
for(int j=2;j<=i;j++)
if(i%j==0 )

break;
else
{
printf("\t%d ",i);
break;
}
}
getche();
}
--------------------------------------------------
Result:

3 5 7 9 11 13 15 17 19 21 23 .................

kindly guide me where the problem resides.
which is letting 15,21 etc in output.
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear zeeshan Hussain,
Salam,

FYI, http://fabrice.bellard.free.fr/mersenne.html

Best Regards.
Farrukh Ahmed
zeeshan Hussain
Lance Naik
Posts: 28
Joined: Tue Sep 02, 2003 1:16 am
Location: Karachi
Contact:

Correct Code

Post by zeeshan Hussain »

dear farrukh,

The code presented in the mentioned example is far far away from the understanding of even a regular C programmer.

extensive use of pointers and custom functions to compact the code is visible.
but anyhow, its appreciable that u tried to help in any sense.

i've fixed the issue with the program and its working perfect now.

--------------------------------------------------------------------
void main()
{

for ( int i=1;i<=32000;i++) // range to find prime numbers is 1-32000
{
int p=1; // starting with 1, taking it as prime,(factors, 1 & -1)

for(int j=2;j<=(i/2);j++)

if(i%j==0) //if the result is 0, its not prime

{
p=0; // so , we should halt here, and start again with next
break;
}

if(p) // if its prime
{
printf("\t%d ",i); // print it here
}
}

}
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

look at how the code works. that will help you understand the problem. here, use this:

Code: Select all

#include<stdio.h>

int main()
{
  int i;
  int j;

  for (i = 1; i <= 300; i++) {
    for (j = 2; i <= i; j++) {
      if (i % j == 0) {
	break;
      } else {
	printf("i %d j %d i %% j %d\n", i, j, i % j);
	break;
      }
    }
  }
}
output:

Code: Select all

i 1 j 2 i % j 1
i 3 j 2 i % j 1
i 5 j 2 i % j 1
i 7 j 2 i % j 1
i 9 j 2 i % j 1
i 11 j 2 i % j 1
i 13 j 2 i % j 1
i 15 j 2 i % j 1
i 17 j 2 i % j 1
i 19 j 2 i % j 1
i 21 j 2 i % j 1
Burn the libraries, for thier price is only one book....AL QURAN (UMER FAROOQ r.a)
please provide a factual reference for the above quote from muslim sources, or stop using it in your signature. someone who studied the issue wrote this article.

maybe he said it, maybe he didn't. show proof.
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

you posted your fixed code while i was writing my post. oh, well.

a good article that describes several techniques for finding primes is here.
zeeshan Hussain
Lance Naik
Posts: 28
Joined: Tue Sep 02, 2003 1:16 am
Location: Karachi
Contact:

Many thanx, Lambda

Post by zeeshan Hussain »

You were really helpful both with the program and the quote,

ist... the output presented was very easy for anyone to understand.
but anywayz i had figured that out earlier.

second..after reading above and few other articles i am dropping that quote off.
i read that in the Dictionary of Quotations, published by indians.

many thanx for correction
nomankhn
Colonel
Posts: 714
Joined: Wed Aug 07, 2002 8:00 pm

Post by nomankhn »

lambda wrote:look at how the code works. that will help you understand the problem. here, use this:

Code: Select all

#include<stdio.h>

int main()
{
  int i;
  int j;

  for (i = 1; i <= 300; i++) {
    for (j = 2; i <= i; j++) {
      if (i % j == 0) {
	break;
      } else {
	printf("i %d j %d i %% j %d\n", i, j, i % j);
	break;
      }
    }
  }
}
output:

Code: Select all

i 1 j 2 i % j 1
i 3 j 2 i % j 1
i 5 j 2 i % j 1
i 7 j 2 i % j 1
i 9 j 2 i % j 1
i 11 j 2 i % j 1
i 13 j 2 i % j 1
i 15 j 2 i % j 1
i 17 j 2 i % j 1
i 19 j 2 i % j 1
i 21 j 2 i % j 1
Burn the libraries, for thier price is only one book....AL QURAN (UMER FAROOQ r.a)
please provide a factual reference for the above quote from muslim sources, or stop using it in your signature. someone who studied the issue wrote this article.

maybe he said it, maybe he didn't. show proof.
Dear Lambda

Nice Program.

Regards
Noman Liaquat Khanzada Rajput
Linux means productivity and fun.
We all love Linux, but it's also a fact that some people might not be able to migrate.
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re: Many thanx, Lambda

Post by lambda »

zeeshan Hussain wrote: second..after reading above and few other articles i am dropping that quote off.
i read that in the Dictionary of Quotations, published by indians.
it really is irrelevant that the book of quotations was published by indians, or whoever. the legend has been around for several centuries, you'll find the story repeated everywhere. and, yes, you will find muslims who believe it's a factual account.

i'm not saying it's not true -- it may very well be true.
Post Reply