-1

Can someone please tell me why my programme is outputting all primes up to inputted n and also just the number n+2 (whether it is prime or not)? And also sometimes n+1 if it is prime, e.g. for inputting n=10, output is 2,3,5,7,11,12.

Here is my code:

/*Write a program that reads in a number n between 1 and 100,000 and then lists all the prime numbers between 1 and n inclusive.*/

#include

#define LIMIT 100000 /*size of integers array*/
#define PRIMES 100000 /*size of primes array*/

int main(){
    int i,j,numbers[LIMIT];
    int primes[PRIMES];
    int n, count;
    count = 0;

    /*Read in an upper limit, n*/
    printf("Please enter a value for n: ");
    if (scanf("%d", &n) !=1) {
        printf("Sorry, cannot read n.\n");
        return 1;
    }

    /*fill the array with natural numbers*/
    for (i=0;i<=n;i++){
        numbers[i]=i+2;
    }

    /*sieve the non-primes*/
    for (i=0;i<=n;i++){
        if (numbers[i]!=-1){
            for (j=2*numbers[i]-2;j<n;j+=numbers[i])
                numbers[j]=-1;
        }
    }

    /*transfer the primes to their own array*/
    j = 0;
    for (i=0;i<=n&&j<PRIMES;i++)
        if (numbers[i]!=-1) {
            primes[j++] = numbers[i];
            count = count + 1; }

    /*print*/
    for (i=0;i<count;i++)
        printf("%d\n",primes[i]);

    return 0;
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • 3
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Jan 26 '17 at 19:25
  • It has bias (`numbers[i]=i+2;`). – BLUEPIXY Jan 26 '17 at 19:51

1 Answers1

0

You always put n+1 and n+2 in your numbers array,

for (i=0;i<=n;i++){
    numbers[i]=i+2;
}

For i in {n-1, n}.

But you don't filter them out, Because this loop:

for (i=0;i<=n;i++){
    if (numbers[i]!=-1){
        for (j=2*numbers[i]-2;j<n;j+=numbers[i])
            numbers[j]=-1;
    }
}

Ignores the numbers which are greater than n.

A working version:

#define MAX_SIZE 100000 /*size of integers array*/

int main() {
    unsigned int i; 
    unsigned int j;
    unsigned int max_filled_index;
    unsigned int numbers[MAX_SIZE];
    unsigned int primes[MAX_SIZE];
    unsigned int num_to_sieve;
    unsigned int prime_count = 0;

    /*Read in an upper limit, n*/
    printf("Please enter a value for n: ");
    if (scanf("%d", &num_to_sieve) != 1) {
        printf("Sorry, cannot read n.\n");
        return 1;
    }

    max_filled_index = num_to_sieve - 2;
    /*fill the array with natural numbers*/
    for (i = 0; i <= max_filled_index; i++) {
        numbers[i] = i + 2;
    }


    /*sieve the non-primes*/
    for (i = 0; i <= max_filled_index; i++) {
        if (numbers[i] != 0) {
            for (j = 2 * numbers[i] - 2; j <= max_filled_index; j += numbers[i]) {
                numbers[j] = 0;
            }
        }
    }

    /*transfer the primes to their own array*/
    for (i = 0; i <= max_filled_index && prime_count < MAX_SIZE; i++)
        if (numbers[i] != 0) {
            primes[prime_count] = numbers[i];
            prime_count++;
        }

    /*print*/
    for (i = 0; i < prime_count; i++)
        printf("%d\n", primes[i]);

    return 0;
}
Ynon
  • 334
  • 2
  • 7