Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Program

Name: Anonymous 2008-09-25 14:26

Programming newfag here. I'm trying to make a c program that determines whether a number is prime. It isn't working properly though. It only indicate that the number is not prime, regardless of the number, so something wrong. Anybody can help?

#include<stdio.h>

int main(void)
{

    int number;
    int count;
    int divisor = 2;
   
    printf("Input number for primality testing\n");
    scanf("d",number);
   
    while (divisor < number/2) {   
        if (number % divisor == 0) {
            break;   
        }
        else
            divisor++;
    }

    if (divisor == number/2) {
        printf("Number is prime\n");
    }
    else {
        printf("Number is not prime\n");
    }
    return 0;
}

Name: Anonymous 2008-09-26 1:15

#include <stdio.h>
#include <gmp.h>

#define IS_PRIME 2
#define MAYBE_PRIME 1
#define NOT_PRIME 0

int main (int argc, char ** argv) {
    long int a;
    mpz_t b;

    printf("Input number for primality testing\n");
    scanf("%ld", &a);

    mpz_init_set_si(b, a);

    switch (mpz_probab_prime_p(b, 8)) {
        case IS_PRIME:
            printf("%ld is prime.\n", a);
            break;
        case MAYBE_PRIME:
            printf("%ld might be prime.\n", a);
            break;
        case NOT_PRIME:
            printf("%ld is not prime.\n", a);
            break;
        default:
            printf("What.\n");
    }

    mpz_clear(b);
    return 0;
}


implementing primality tests is boring, let's go shopping.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List