Date created: 07/10/11 19:14:48. Last modified: 12/13/12 11:41:20

Problem 7

// Project Euler - Problem 7
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[]) {

        int i = 2;
        int j = 2;
        int primecount = 0;
        int primetop = atoi(argv[1]);

        while (1) {
                while (j<=i) {
                        if (i % j != 0) {
                                j++;
                        } else if (i % j == 0) {
                                if (j == i) {
                                        primecount++;
                                        printf("%d is a prime number %d\n", i, primecount);
                                        if (primecount==primetop) {
                                                exit (EXIT_SUCCESS);               
                                        }
                                }
                                j = i + 1;
                        }
                }
                j=2;
                i++;
        }
        return EXIT_SUCCESS;
}

Previous page: Problem 6
Next page: Problem 8