Prime numbers - fermat's theorem

Hi ;)
I wanted to write a program which would tell "YES" if an input number was prime, and "NO" if it wasn't.

Fermat’s Little Theorem: If n is a prime number, then for every a, 1 <= a < n,

a^(n-1) % n = 1

My program knows when a number isn't prime,but for some unknown reason when the input number is for example 7 the return is "NO".
This is how it works:
https://ibb.co/pdqqb4J

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
using namespace std;

bool ifprime(int n)
{
int result, prime;
    cin>>n;
    for(int a=1; a<n; a++)
    {
        result = pow(a, n-1);
        prime = result%n;
        cout << result <<"%"<<n<<" = "<< prime << endl;
        if(prime!=1)

    return false;
    }
    return true;
}
int main()
{
int n, times;
cout << "How many numbers do you want to check?" << endl;
cin >>times;
cout << endl;

    for(int k=0; k<times; k++)
    {
        bool check;
        check = ifprime(n);

        if(check == true)
        cout << "YES" << endl << endl;
        else cout << "NO" <<endl << endl;
    }
return 0;
}
??? What is the value of n when it gets to line 30?

1
2
3
4
5
6
7
8
9
int n, times;
cout << "How many numbers do you want to check?" << endl;
cin >>times;
cout << endl;

    for(int k=0; k<times; k++)
    {
        bool check;
        check = ifprime(n);
Last edited on
Well, I hoped the value was as in the input in function, but now i see it looks not so good.
Changed it to this below, but it still doesn't work as it should: (n value is input in main, not in function)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
using namespace std;

bool ifprime(long int &n)
{
long int result, prime;
    for(int a=1; a<n; a++)
    {
        result = pow(a, n-1);
        prime = result%n;
        cout << result <<"%"<<n<<" = "<< prime << endl;
        if(prime!=1)

    return false;
    }
    return true;
}
int main()
{
long int n, times;
cout << "How many numbers do you want to check?" << endl;
cin >>times;
cout << endl;

    for(int k=0; k<times; k++)
    {
        cin >> n;
        bool check;
        check = ifprime(n);

        if(check == true)
        cout << "YES" << endl << endl;
        else cout << "NO" <<endl << endl;
    }
return 0;
}


Last edited on
but it still doesn't work as it should:


What output are you expecting for what input? (Please don't direct people to external pages for this.)

Please also put prompt lines in whenever you want an input. You may understand how your program works, but others may not (especially when your indentation is very inconsistent).

Are you sure that Fermat's Little Theorem is an "equivalence" statement; i.e. does
prime => property
necessarily mean that
property => prime
?
Now I'm not so sure that this theorem works backwards, thanks.
Maybe for now, I'm just curious why when the input number = 7, the program tells that

How many numbers do you want to check?
1

7
1%7 = 1    (1 power 6)
64%7 = 1  (2 power 6)
729%7 = 1 (3 power 6)
4096%7 = 1 (4 power 6)
15624%7 = 0     (5 power 6) <=== ??? 
NO


Why 15624, not 15625? In this line, the output should be 5 power 6, but it's missing one...
This is the output on cpp shell - looks OK

How many numbers do you want to check?
1

7
1%7 = 1
64%7 = 1
729%7 = 1
4096%7 = 1
15625%7 = 1
46656%7 = 1
YES


About the only thing I can suggest is that you are using pow() ... which naturally deals with floating point numbers, and so is subject to all those floating-point rounding difficulties.
result = pow(a, n-1);
If this works out as, say, 15624.99999999
then it will truncate down to 15624

To get powers of integers ... just use repeated multiply, NOT pow()




Last edited on
In your example, when n=66, you must compute 65^65. Which has about 117 digits. That's far more than can fit in a 64 bit integer.

The bottom line is that you should use a different method of determining if a number is prime.
Another advantage of using "repeated multiply ... modulo p" over the pow() function, is that successive multiplications modulo p will prevent blow-up. You will never end up with a number bigger than the original prime.
Topic archived. No new replies allowed.