On Tue, Apr 1, 2014 at 11:02 PM, Marko Rauhamaa <ma...@pacujo.net> wrote: > ======================================================================== > #include<stdio.h> > > int main() > { > int n, i = 3, count, c; > > printf("Enter the number of prime numbers required\n"); > scanf("%d",&n); > > if ( n >= 1 ) > { > printf("First %d prime numbers are :\n",n); > printf("2\n"); > } > > for ( count = 2 ; count <= n ; ) > { > for ( c = 2 ; c <= i - 1 ; c++ ) > { > if ( i%c == 0 ) > break; > } > if ( c == i ) > { > printf("%d\n",i); > count++; > } > i++; > } > > return 0; > } > ======================================================================== > (<URL: http://www.programmingsimplified.com/c/source-code/ > c-program-for-prime-number>)
Here's my tweaked version of that: ======================================================================== #include <stdio.h> int main() { int i = 3, count, factor; printf("Enter the number of prime numbers required\n"); scanf("%d",&count); if ( count >= 1 ) { printf("First %d prime numbers are:\n",count); printf("2\n"); } while (count > 1) { /* This is a pretty stupid algorithm */ for ( factor = 2 ; factor <= i - 1 ; factor++ ) if ( i%factor == 0 ) break; if ( factor == i ) { printf("%d\n",i); count--; } i++; } return 0; } ======================================================================== Doesn't change the parenthesis count (other than that I dropped an unnecessary pair of braces; some people would prefer to keep them, but I find they're quite superfluous), but improves readability. (Why use a for loop when you could use a simple while?) As to the question of whether this is more or less readable than the Scheme version... I guess that partly depends on the reader's relative familiarity with C and Scheme, but it's crystal clear to me what the C version is doing - and that it's doing something stupid. I don't find it more readable to cast something as recursive; compare these two tight loops: (let find-divisor ((c 2)) (cond ((= c i) (format #t "~S\n" i) (display-primes (1+ count) (1+ i))) ((= (remainder i c) 0) (display-primes count (1+ i))) (else (find-divisor (1+ c))))))))) for ( factor = 2 ; factor <= i - 1 ; factor++ ) if ( i%factor == 0 ) break; if ( factor == i ) { printf("%d\n",i); count--; } In the first one, you start doing something, and if you don't have a termination point, you recurse - which means you have to name this loop as a function. In the second, you simply iterate, and then at the end, decide whether you have the termination condition or not. It's easy to see what the loop condition is; it's easy to see that it will always end by one or other termination rule, and then it acts based on that. Actually, if you switch the conditions, it would look a bit more like the Scheme version: for ( factor = 2 ; i%factor ; factor++ ) { if ( factor == i ) { printf("%d\n",i); count--; break; } } I wouldn't say this makes the code notably more readable, and it doesn't change the parenthesis count (apart from making more people want to put the outer braces in - they're still technically optional, and that was only an optional reduction in the first place), but it's a closer equivalent and that might make comparison easier. My view is definitely that the C version is WAY more readable than the Scheme one. ChrisA -- https://mail.python.org/mailman/listinfo/python-list