0
long long int fun2(int a, int b, int m)
{
    long long int res = 1;
    long long int c = a % m;

    for (int i = 1; i <= b; i <<= 1)
    {
        c = c % m;
        if ((b & i) != 0)
        {
            res = res * c;
            res = res % m;
        }
        c = c * c;
    }

    return res;
}

int fun(int num, int k)
{
    srand((unsigned)time(NULL));

    if (num <= 1)
    {
        return num * 10;
    }

    if (num == 2 || num == 3 || num == 5)
    {
        return num * 10 + 1;
    }

    if (num % 2 == 0)
    {
        return num * 10;
    }

    if (num % 3 == 0)
    {
        return num * 10;
    }

    if(num % 5 == 0)
    {
        return num * 10;
    }

    int s = 0;
    int s_pow = 1;

    while ((s_pow & (num - 1)) == 0)
    {
        s  = s + 1;
        s_pow = s_pow << 1;
    }

    int d = num / s_pow;

    for (int i = 0; i < k; i++)
    {
        int a = (int)((num - 1) * rand() / (RAND_MAX + 1.0)) + 1;

        if (fun2(a, d, num) != 1)
        {
            is_prime = false;
            for (int r = 0; r <= s - 1; r++)
            {
                if (fun2(a, (1 << r) * d, num) == num - 1)
                {
                    is_prime = true;
                    break;
                }
            }
            if (!is_prime)
            {
                return num * 10;
            }
        }
    }

    return num * 10 + 1;
}

Where is a problem, maybe these long long int with int compares doesnt work correctly. Compilation for windows and linus is without any warnings. It works but gives bad results for linux, for windows is ok. Please help.

@EDIT I deleted code with INT_MIN and INT_MAX I just tried to fix the problem with this. (Sorry, should have delete it)

harper
  • 13,345
  • 8
  • 56
  • 105
  • 5
    It'd be helpful to give a specific example - for a specific input, what do you expect to get vs what is the actual result. – ysap Jan 13 '14 at 20:42
  • You say "no warnings", but did you try compiling with `-Wall` on Linux? – JAB Jan 13 '14 at 20:43
  • Linux and Windows have different definitions of types... see http://stackoverflow.com/questions/15908789/cross-platform-definition-of-64-bit-integers-in-c-for-windows-and-linux – cubitouch Jan 13 '14 at 20:46
  • 3
    Inputs with expected and received outputs might be nice. Since the functions are called `fun` and `fun2` with no documentation. –  Jan 13 '14 at 20:51
  • Technically `INT_MAX` and `INT_MIN` are implementation-defined. Although I don't think that would be the cause of the error. –  Jan 13 '14 at 20:52
  • 3
    An aside that is probably unrelated to your question: a test such as `if (a > INT_MAX)` where `a` is of type `int` will never be true, since an `int` cannot have a value larger than `INT_MAX`. – Michael Burr Jan 13 '14 at 20:53
  • 1
    I see no difference in output. [Coliru](http://coliru.stacked-crooked.com/a/f1f86330d98c0bbd) and [Rextester](http://rextester.com/TDNLMZ57442). –  Jan 13 '14 at 20:59
  • Yes I added -Wall on linux, INT_MAX and INT_MIN are not a problem I added it to solve problem, but it doesnt help, It shouldnt be there. This is test for prime numbers it looks like all numbers are prime beyond these numbers which can be devide by 2,3,5 with rest 0. So it looks like the middle of fuction fun() doesnt work correctly. – user3144540 Jan 13 '14 at 21:04
  • Unrelated: the `INT_MAX` checking you put int, `a > INT_MAX`, is pointless. `a` is `int` and as such, it cannot be greater than `INT_MAX`. Likewise with `a < INT_MIN`. It would probably help if you reduced the unrelated clutter and had *just* the problem at-hand exposed. – WhozCraig Jan 13 '14 at 21:07
  • Any ideas what is wrong? :/ – user3144540 Jan 13 '14 at 21:41
  • or just maybe this algoritm is bad – user3144540 Jan 13 '14 at 22:03
  • You should only call `srand` once at the beginning of your program, rather than every time you call `fun`. Also, please define "bad result" for Linux; what are you expecting to see, and what are you actually getting? What, exactly, is this algorithm supposed to be doing? – John Bode Jan 13 '14 at 22:03
  • You haven't declared `is_prime`. You're missing at least three required `#include` directives. And you can't expect any help until you tell us exactly what "bad results" means. We can't guess the intended behavior from the names `fun2` and `fun`. – Keith Thompson Jan 13 '14 at 22:16
  • Problem SOLVED by myself !!!! Imagine that problem was in random a. I exchange this int a = (int)((num - 1) * rand() / (RAND_MAX + 1.0)) + 1; with this int a = (int)(rand()%(num-1)) + 1; and everything works perfect – user3144540 Jan 13 '14 at 23:33

1 Answers1

0

Problem SOLVED by myself !!!! Imagine that problem was in random a. I exchange this

int a = (int)((num - 1) * rand() / (RAND_MAX + 1.0)) + 1;

with this

int a = (int)(rand()%(num-1)) + 1;

and everything works perfect – user3144540

Armali
  • 18,255
  • 14
  • 57
  • 171