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-25 14:27

>>1
back to MySQL Connection Failure, please

Name: Anonymous 2008-09-25 14:28

Hmm, why would you write this program in C? It doesn't look like a device driver.

Name: Anonymous 2008-09-25 14:30

read "programming newfag"
what better place to start?

Name: Anonymous 2008-09-25 14:31

You should just give up. You are horrible.

Name: Anonymous 2008-09-25 14:32

>>5
second

Name: Anonymous 2008-09-25 14:32

You want if (divisor > number/2) to check if no divisor was found. The last divisor++ will make it greater than number/2. At the moment, only the number 4 will be counted as prime (it's divisible by two, and 2 == 4/2 satisfies the condition).

Name: Anonymous 2008-09-25 14:36

Thanks, i'll try now

Name: Anonymous 2008-09-25 14:40

Thanks for the effort, I understand what you mean, but it not working. I used some printf's and found that the program never reach the else to increment the divisor. I still don't know why?

Name: Anonymous 2008-09-25 14:48

>>9
You're not using scanf right.

Name: Anonymous 2008-09-25 14:52

#include <stdio.h>

int main(void) {
    printf("やめて おしり いたい\n");
    return 0;
}

Name: Anonymous 2008-09-25 14:56

>>9
*facepalm*
Thank you, fixed it right up

Name: Anonymous 2008-09-25 14:57

>>12
err, meant to link to >>10

Name: Anonymous 2008-09-25 15:00

also while we're at it, read up on the sieve of eratosthenes.  you don't need to go to n/2, you've reached the end at n^(1/2), and also you can get rid of many of the divisors as nonprime while going through them.

Name: Anonymous 2008-09-25 15:43

This is much better, thanks for the n^(1/2) tip, makes it a bit faster. The program works now, although only for numbers up to 2^32. Heres the code now. The if statement at the end seems kind of messy, but it works.

#include<stdio.h>

int main(void)
{

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

    if ((divisor == number^(1/2) && number != 4)
        || number == 2 || number == 3) {
        printf("Number is prime\n");
    }
    else {
        printf("Number is not prime\n");
    }
    return 0;
}

Name: Anonymous 2008-09-25 16:53

thanks for the n^(1/2) tip

*facepalm*

Name: Anonymous 2008-09-25 17:17

>>15
>_<

^ doesn't do what you think it does, nor does /[code].

Also, taking the square root of N every iteration would hurt performance. Try testing [code]divisor * divisor < number
instead.

HIBTC?

Name: Anonymous 2008-09-25 17:22

Question : does void main in C have to be of the IO type?

Name: Anonymous 2008-09-25 18:27

>>18
Yes, along with everything else in C

Name: Anonymous 2008-09-25 18:32

>>1,>>15
Beautiful. 10/10 for both

Name: Anonymous 2008-09-25 18:41

#include <iostream>
#include <math>

int main() {
  long n;

  std::cout << "Enter a number: " << std::endl;
  cin >> n;

  long m = sqrt(n);
  try {
     for(long i=2; i < m; i++)
        if(n % i == 0)
           throw "Number is not prime";
  } catch (char * no) {
    std::cout << no << std::endl;
    return;
  }
 
  cout << "Number is prime" << std::endl;

}

Name: Anonymous 2008-09-25 19:24

>>21
You know, between C++ and >>1 , I think I prefer the trollOP.

Name: Anonymous 2008-09-25 19:29

>>18,19
Having a void main is inadvisable as the symbol would clash with the main function.

Name: Anonymous 2008-09-25 23:21

isPrime   :: Integer -> Bool
isPrime x | x <= 2    = True
          | otherwise = isPrime' 2 x

isPrime'     :: Integer -> Integer -> Bool
isPrime' y x | x `mod` y == 0 = False
             | y*y < x        = True && isPrime' (y+1) x
             | otherwise      = True

main = do print $ zip [0..100] (map isPrime [0..100])

Name: Anonymous 2008-09-26 1:13

>>24
isPrime x | x <= 2    = True
Never sacrifice correctness for conciseness.

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.

Name: Anonymous 2008-09-26 1:16

Now that I think of it, yo /prog/ what is the fastest primality test currently?

Name: Anonymous 2008-09-26 1:19

>>27
Lookup table.

Name: Anonymous 2008-09-26 1:25

Name: Anonymous 2008-09-26 1:34

Now that I think of it, yo /prog/ what was the GDP of Australia last year?

Name: Anonymous 2008-09-26 1:55

>>26
    long int a;
            printf("%ld might be prime.\n", a);
what.

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

bool is_prime(mpz_t n){
 bool ret = TRUE;
 mpz_t r, i, t;
 mpz_init(r);
 mpz_init(i);
 mpz_init(t);
 for(mpz_sqrt(r, n); mpz_cmp(r, i) > 0; mpz_add(i, i, 1))
  if(mpz_remove(t, n, i)){
   ret = FALSE;
   break;
  }
 mpz_clear(r);
 mpz_clear(i);
 mpz_clear(t);
 return ret;
}

int main(){
 mpz_t n;
 mpz_init(n);
 puts("Input number for primality testing");
 mpz_inp_str(n, stdin, 0);
 mpz_out_str(n, stdout, 0);
 puts(is_prime(n) ? "is prime." : "is not prime.");
 mpz_clear(n);
 return 0;
}

Name: Anonymous 2008-09-26 1:59

>>30
$908.8 billion. That's USD, not Australian.

Name: Anonymous 2008-09-26 2:05

>>31
s/"i/" i/g

Name: Anonymous 2008-09-26 2:07

>>31
it's because mpz_probab_prime_p is dqn

Name: Anonymous 2008-09-26 2:14

>>34
no, it's because mpz_probab_prime_p isn't what you should use if you really want to know if a number is prime.
what's you're excuse for ignoring mpz_inp_str/mpz_out_str and limiting the input to what can fit in a long int?

Name: Anonymous 2008-09-26 6:48

>>31
ANONIX quality

Name: Anonymous 2008-09-26 10:34

>>36
ANONIX
gmp.h
GNU

Does not compute.

Name: Anonymous 2008-09-26 10:42

>>37
Anonix has always been obsessed with GNU.

Name: Anonymous 2008-09-26 10:58

Suggest rename to ANG (ANG's Not GNU)

Name: Anonymous 2008-09-26 12:14

Suggest rename to Fagix

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