Reply
Fri 3 Dec, 2004 07:18 am
How to creat a program of prime nos. in C Language
1. Get good book about C language.
2. learn what a prime number is.
3. Study Sieve Of Erastosthenes.
4. Write code.
Well thanks a lot for the tip & i'm able to complete 3 steps but unable to do the 4th one. need help..........
Why don't you start, then we can help you when you get bogged down.
The first thing you need to do is to design how the program will work. A program of prime numbers doesn't tell me much about what it will look like. Some options...
1) I could type in a number and the program could tell me if it is prime or not.
2) The program could spit out a bunch of prime numbers.
3) The program could go number, by number and tell me the prime factors of each one.
The second thing you need to do is to come up with a series of tasks that the program will do to fulfill its purpose. You can write these steps in plain english (or whateverr language). As example the first task could be:
"1. Display the message "Enter a number" on the screen."
"2. Accept a number from the user and put it into a variable."
Once you do these two things, you can start figuring out the C code.
I don't write C code without being paid for it. However, if you start the planning I would be happy to help with specific things when you get stuck.
I based my "helpful reply", above, on my feeling that that the first poster has been given a school or college programming assignment. You see this so often on forums like this. "Please write me a QBasic program to calculate average speeds" or some such.
If somebody really wants to write a C program to find prime numbers, they will do it by using the steps I listed. If they need to ask on the web just to get started, maybe they are not yet ready for C programming. Especially if they cannot think "Google search C source code examples prime numbers". Or am I being too harsh here?
The whole point of such assignments is that you learn by doing things. If a bunch of folks on the net write your code for you, you won't learn much at all, and if the teacher says, "Why did you put that line of code in that place?" you won't be able to answer, because you don't know!!
However, here is an example uncommented. If you can write the comments for this, I guess you will have proved that you understand the stuff.
#include <stdio.h>
#include <stdlib.h>
#define DO(P,R,I,M,E,S,i0,v0,i1,v1,i2,v2,i3,v3,i4,v4,i5,v5,i6,v6,i7,v7) k=P;\
if (!(sieve[n] & (1<<R)))\
{ printf(" %ld",30*n + bits[R]);\
e = eos - I*n - M;\
for (m = sieve + (30*n + E) * n + S; m < e; m += i0)\
{ *m |= (1<<v0); *(m += i1) |= (1<<v1);\
*(m +=i2) |= (1<<v2); *(m += i3) |= (1<<v3);\
*(m +=i4) |= (1<<v4); *(m += i5) |= (1<<v5);\
*(m +=i6) |= (1<<v6); *(m += i7) |= (1<<v7);\
}\
if (m < eos) { *m|=(1<<v0);\
if ((m += i1) < eos) { *m |= (1<<v1);\
if ((m += i2) < eos) { *m |= (1<<v2);\
if ((m += i3) < eos) { *m |= (1<<v3);\
if ((m += i4) < eos) { *m |= (1<<v4);\
if ((m += i5) < eos) { *m |= (1<<v5);\
if ((m += i6) < eos) *m |= (1<<v6);\
} } } } } } }
char bits[] = {1,7,11,13,17,19,23,29} ;
int main(int argc, char *argv[])
{
unsigned long p,q,r,k=0,n,s;
char *m,*e,*eos,*sieve;
long bytes,atol();
if (argc!=2) printf("usage: %s <bytes_used or -maxprime>\n",*argv), exit(0);
if ((bytes=atol(argv[1])) < 0) bytes = 1 + (-bytes)/30;
if (!(sieve = malloc(bytes))) printf("Out of memory.\n"), exit(0);
if (bytes > 30) for (k = r = (bytes-1)/30; (q = r/k) < k; k >>= 1) k += q;
eos = sieve + bytes; s = k + 1; *sieve = 1; printf("2 3 5");
for (n = p = q = r = 0; n < s; n++)
{ DO(p++,0,28, 0, 2, 0,p,0,r,1,q,2,k,3,q,4,k,5,q,6,r,7); r++;
DO(q++,1,24, 6,14, 1,r,5,q,4,p,0,k,7,p,3,q,2,r,6,p,1); r++; q++;
DO(p-1,2,26, 9,22, 4,q,0,k,6,q,1,k,7,q,3,r,5,p,2,r,4); r++;
DO(q-1,3,28,12,26, 5,p,5,q,2,p,1,k,7,r,4,p,3,r,0,k,6);
DO(q+1,4,26,15,34, 9,q,5,p,6,k,0,r,3,p,4,r,7,k,1,p,2); r++;
DO(p+1,5,28,17,38,12,k,0,q,4,r,2,p,5,r,3,q,7,k,1,q,6); r++; q++;
DO(q++,6,26,20,46,17,k,5,r,1,p,6,r,2,k,3,p,7,q,0,p,4); r++;
DO(p++,7,24,23,58,28,r,0,k,7,r,6,q,5,p,4,q,3,p,2,q,1);
}
printf(" ...");
for (p = bytes - s; p < bytes; p++)
for (k = 0; k < 8; k++)
if (!(sieve[p] & (1<<k))) printf(" %ld",30 * p + bits[k]);
for (p = 0, n=3; p < bytes; p++)
for (k = 0; k < 8; k++) n += !(sieve[p] & (1<<k));
printf("\n%ld primes found\n", n);
exit(0);
}
Yuck!
Is that from the famed "obfuscated C" contest?
ebrown_p wrote:Yuck!
Is that from the famed "obfuscated C" contest?
I dunno, I found it by the Google method which I outlined above.
lol.
Was perusing the Hacker's Dictionary this morning.
Two entries come to mind on reading this thread:
RTFM (Read the f***ing manual)
and
STFW (Search the f***ing web)
that is the ugliest piece of source code I have ever seen so help me god
How's this then:-
(Found on the web)
The following example prints out all prime numbers between 1 and MAX_INT.
The program checks a number for primality by dividing it by all smaller integers up to half its size.
#include <stdio.h>
#define MAX_INT 500
#define TRUE 1
#define FALSE 0
int main ()
{
int poss_prime;
for (poss_prime = 2; poss_prime <= MAX_INT; poss_prime++)
{
if (prime(poss_prime))
{
printf ("%d ", poss_prime);
}
}
printf("\n\n");
return 0;
}
prime (int poss_prime) /* check whether poss_prime is prime */
{
int poss_factor;
for (poss_factor = 2; poss_factor <= poss_prime/2; poss_factor++)
{
if (poss_prime % poss_factor == 0)
{
return (FALSE);
}
}
return (TRUE);
}
The program should print the following sequence of integers:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499