-4

I have an array of prime numbers. The length of this array is 1,000,000. This means that my last element primes[999999] should have the 1 millionth prime, and actually it should have the 1,000,001th prime since I left 2 out of the beginning.

When I run my program and have it spit out the last element of the array it spits out the 999,999th prime number. I am in the process of learning C and this is a little homework project for a math class I am taking. I have no idea where to even start in my troubleshooting of this problem. Any help would be greatly appreciated.

Edit: Sorry, for some reason I thought it might be an easy answer. I posted the code below. I have printed out the array of primes under option 1 and all the primes are correct and the result is correct. Any other option and I get an erroneous output.

void createArray( int, unsigned long long * );
unsigned long long root(unsigned long long);
void print( unsigned long long *, int );

int main(void){

    unsigned long long *prime = (unsigned long long *)calloc(SIZE6, 
                                    sizeof(unsigned long long));
    unsigned long long *ptrEmpty = &prime[15];
    unsigned long long i;
    unsigned long long j;
    long int sizeComp;
    int flag = 0;
    unsigned long option = 0;
    int length = 0;

    system( "clear" );

    do{ 
        flag = 0;
        option = -1;

        printf( "Welcome to the prime number finder program. Below are a list "
            "of prime numbers\nyou can find as well as their average runni"
            "ng time on the Loki system of UNO.\n\nOption 1: Return the 10"
            "0th prime number. ( Running time: .006s )\nOption 2: Return t"
            "he 1000th prime number. ( Running time: .008s )\nOption 3: Re"
            "turn the 10000th prime number. ( Running time: .089s )\nOptio"
            "n 4: Return the 100000th prime number. ( Running time: 2.188s"
            " )\nOption 5: Return the 1000000th prime number. ( Running ti"
            "me: 57.156s )\nOption 6: Return the 10000000th prime number. "
            "( **CAUTION!** Running time: 35m51.3s )\n\nEnter the number o"
            "f the option you would like the program to\nperform (Enter 0 "
            "to exit):  " );

        scanf( "%lu", &option );

        switch( option ){

            case 0:
                flag = 1;
                break;
            case 1:
                createArray( SIZE1, prime );
                ptrEmpty = &prime[15];
                length = SIZE1;
                break;
            case 2:
                createArray( SIZE2, prime );
                ptrEmpty = &prime[15];
                length = SIZE2;
                break;
            case 3:
                createArray( SIZE3, prime );
                ptrEmpty = &prime[15];
                length = SIZE3;
                break;
            case 4:
                createArray( SIZE4, prime );
                ptrEmpty = &prime[15];
                length = SIZE4;
                break;
            case 5:
                createArray( SIZE5, prime );
                ptrEmpty = &prime[15];
                length = SIZE5;
                break;
            case 6:
                createArray( SIZE6, prime );
                ptrEmpty = &prime[15];
                length = SIZE6;
                break;
            default:
                printf( "Please enter one of the available options ( 1 - 7 ) "
                "or 0 to exit:  " );
            flag= 1;
            break;
        }

        if( flag != 1 ){
            for( i = START, sizeComp = 15; sizeComp <= length; i += 2 ){

                flag = 0;
                for( j = 0; prime[j] < root( i ); j++ ){

                    if( ( i % prime[j] ) == 0 ){
                        flag = 1;
                        break;
                    }
                }

                if( flag == 0 ){
                    *ptrEmpty = i;
                    ++sizeComp;
                    ptrEmpty++;
                }
            }

            printf( "\nThe %dth prime number is %llu.\n\n", length, 
                    prime[ length - 2 ]);

//            print( prime, length );
            printf( "****************************************"
                    "****************************************\n\n" );

    }

    }while( option != 0 );

    free( prime );

    return 0;
}

void print( unsigned long long *prime, int length ){

    int i;
    printf( "\n%4d", 2 );
    for( i = 0; i < length; i++){
        printf( "%4llu ", prime[i] );
    }
    printf( "\n" );
}

void createArray( int length, unsigned long long *prime ){

    unsigned long long *pHolder = NULL;

    pHolder = (unsigned long long *)realloc( prime, length * 
                                        sizeof( unsigned long long ) );

    if(pHolder != NULL){
        prime = pHolder;
    }else{
        free( prime );
        printf( "Error reallocating memory.\n" );
        exit(1);
    }

    prime[0] = 3;
    prime[1] = 5;
    prime[2] = 7;
    prime[3] = 11;
    prime[4] = 13;
    prime[5] = 17;
    prime[6] = 19;
    prime[7] = 23;
    prime[8] = 29;
    prime[9] = 31;
    prime[10] = 37;
    prime[11] = 41;
    prime[12] = 43;
    prime[13] = 47;
    prime[14] = 53;
}

unsigned long long root(unsigned long long a) {

    unsigned long long rem = 0;
    unsigned long long root = 0;
    int i;

    for (i = 0; i < 16; i++) {
        root <<= 1;
        rem <<= 2;
        rem += a >> 30;
        a <<= 2;

        if (root < rem) {
            root++;
            rem -= root;
            root++;
        }
    }

    return (root >> 1);
}

Edit: SIZE1 = 100, SIZE2 = 1000, SIZE3 = 10000, SIZE4 = 100000, SIZE5 = 1000000, SIZE6 = 10000000

4

1 回答 1

3

您可以通过将数组大小更改为 5 并仅查看前 5 个素数,打印出所有 5 个素数,然后查看它是否实际上是 2 3 5 7 11 (或者在您的情况下为 3 5 7 11 13 ) 或者如果你有一些错误。

您的错误可能会在那里存在,然后您可以针对更小的情况纠正它,它应该适用于更大的情况。

于 2013-06-07T18:53:38.187 回答