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.

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

Postby zeeshan Hussain » Sat Dec 02, 2006 3:19 am

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.
zeeshan Hussain
Lance Naik
 
Posts: 28
Joined: Tue Sep 02, 2003 1:16 am
WLM: zeeshanhussain@pakistanmail.com
Yahoo Messenger: zeesh_tas@yahoo.co.in
Location: Karachi

Re:

Postby LinuxFreaK » Sat Dec 02, 2006 10:09 am

Dear zeeshan Hussain,
Salam,

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

Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Correct Code

Postby zeeshan Hussain » Sat Dec 02, 2006 1:56 pm

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
}
}

}
zeeshan Hussain
Lance Naik
 
Posts: 28
Joined: Tue Sep 02, 2003 1:16 am
WLM: zeeshanhussain@pakistanmail.com
Yahoo Messenger: zeesh_tas@yahoo.co.in
Location: Karachi

Postby lambda » Sat Dec 02, 2006 2:01 pm

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
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby lambda » Sat Dec 02, 2006 2:06 pm

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.
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Many thanx, Lambda

Postby zeeshan Hussain » Sat Dec 02, 2006 4:20 pm

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
zeeshan Hussain
Lance Naik
 
Posts: 28
Joined: Tue Sep 02, 2003 1:16 am
WLM: zeeshanhussain@pakistanmail.com
Yahoo Messenger: zeesh_tas@yahoo.co.in
Location: Karachi

Postby nomankhn » Sat Dec 02, 2006 4:59 pm

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.
nomankhn
Colonel
 
Posts: 714
Joined: Wed Aug 07, 2002 8:00 pm

Re: Many thanx, Lambda

Postby lambda » Sat Dec 02, 2006 11:22 pm

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.
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron