Solovay - Strassen primality test

I am trying to write a program that tests an odd number if it is prime using the Solovay - Strassen primality test (http://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test). But it doesn't seem to work, no matter what number the input is the program always says it is composite.

Here is the loop that should do the test:
1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i=0; i<=k; i++)

    {

        int a = rand() % n;

        x = (a/n);

        if (x = 0 || pow(a,(n-1)/2) != (x % n))

        y = y + 1;

    }


I'd be gratefull if you could give me some hint on what I'm doing wrong.
Thanks in advance.
Last edited on
Hey,

You could try seeding rand().

Also,

if (x = 0 || pow(a,(n-1)/2) != (x % n))

Should be

if (x == 0 || (pow(a,(n-1)/2) != (x % n)))

EDIT: Shouldn't you be returning true of false depending on the result of the if statement? I was under the impression that if the statement is true then n is a composite, otherwise it's a prime. But you don't seem to be testing for this?
Last edited on
Topic archived. No new replies allowed.