Is this the correct implementation of Fermat's Primality Test?

  • Thread starter user366312
  • Start date
  • Tags
    Test
In summary: Thanks for pointing that out. My other comment about the size limitations on 64-bit integers is still applicable, though.
  • #1
user366312
Gold Member
89
3
Homework Statement
I need to implement Fermat's primality tester in Python. I don't know Python very well. So, I am implementing it in C#. If I succeed, I will convet it to Python.
Relevant Equations
https://en.wikipedia.org/wiki/Fermat_primality_test
Fermat's Primality Tester:
class PrimeTest
{
    public static bool IsPrime(long n, int iteration = 5)
    {
        Random r = new Random();
        long a = 0;
        long aPow = 0;

        for (int i = 0; i < iteration; i++)
        {
            a = r.Next(1, Convert.ToInt32(n - 1));

            double x = Convert.ToDouble(a);
            double y = Convert.ToDouble(n - 1);
            double p = Math.Pow(x, y);

            aPow = Convert.ToInt64(p);//<==== this line is giving an exception.

            if (1 % n == aPow % n)
            {
                return true;
            }
        }

        return false;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("{0}", PrimeTest.IsPrime(33));
        Console.ReadLine();
    }
}

Output:
An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll

Additional information: Arithmetic operation resulted in an overflow.

I have two questions:

  1. Is this implementation correct? If not, why?
  2. How to get rid of this overflow exception?
 
Physics news on Phys.org
  • #2
user366312 said:
Homework Statement:: I need to implement Fermat's primality tester in Python. I don't know Python very well. So, I am implementing it in C#. If I succeed, I will convet it to Python.
Relevant Equations:: https://en.wikipedia.org/wiki/Fermat_primality_test

Fermat's Primality Tester's Primality Tester:
class PrimeTest
{
    public static bool IsPrime(long n, int iteration = 5)
    {
        Random r = new Random();
        long a = 0;
        long aPow = 0;

        for (int i = 0; i < iteration; i++)
        {
            a = r.Next(1, Convert.ToInt32(n - 1));

            double x = Convert.ToDouble(a);
            double y = Convert.ToDouble(n - 1);
            double p = Math.Pow(x, y);

            aPow = Convert.ToInt64(p);//<==== this line is giving an exception.

            if (1 % n == aPow % n)
            {
                return true;
            }
        }

        return false;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("{0}", PrimeTest.IsPrime(33));
        Console.ReadLine();
    }
}

Output:
An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll

Additional information: Arithmetic operation resulted in an overflow.

I have two questions:

  1. Is this implementation correct? If not, why?
  2. How to get rid of this overflow exception?
1. Is the implementation correct? No. As noted in the wiki article, this primality test is probabilistic. It will determine that a number is composite, or that it probably is prime. In your code, you have declared a Random object, but you never use it. Since you want to determine that 33 is prime or not, your code should loop through the numbers in the range 2 through 31.
2. You can't avoid the overflow exception as long as you are limited to using the Int64 type for your numbers. In running your program, in the first iteration, it attempts to convert a number that is about ##8.34 \times 10^{40}##, which is larger than ##2^{123}##. This is way too large to fit into 64 bits -- the largest unsigned 64-bit integer is ##2^{64} - 1##.
You can however, use a different type, the BigInteger struct (https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=netframework-4.8), which allows you to work with arbitrarily large integer values, similar to what Python offers. The BigInteger struct also provides a set of methods for arithmetic, including a method called ModPow (https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.modpow?view=netframework-4.8) that would be useful for what you're trying to do.
 
  • Like
Likes sysprog
  • #3
Mark44 said:
In your code, you have declared a Random object, but you never use it.
That is not true. I used that. see closely kindly.
 
  • #4
user366312 said:
That is not true. I used that. see closely kindly.
I looked and saw it defined but not used; can you please tell us on what line you called or used your 'Random' object?
 
  • #5
sysprog said:
I looked and saw it defined but not used; can you please tell us on what line you called or used your 'Random' object?
see line#11.
 
  • Like
Likes sysprog
  • #6
user366312 said:
sysprog said:
I looked and saw it defined but not used; can you please tell us on what line you called or used your 'Random' object?
see line#11.
It looks to me to be the case that line 11:a = r.Next(1, Convert.ToInt32(n - 1)); is inside the 'Random' object and does not call it --
 
  • #7
sysprog said:
It looks to me to be the case that line 11:a = r.Next(1, Convert.ToInt32(n - 1)); is inside the 'Random' object and does not call it --
lol
 
  • #8
sysprog said:
It looks to me to be the case that line 11:a = r.Next(1, Convert.ToInt32(n - 1)); is inside the 'Random' object and does not call it --
No, the Random object r is being used here. The Next() method is one of three overloaded methods on the Random class. The variable a is being set to a random integer between 1 and 32 here.

It's easy to overlook variables with single-letter names. I didn't pick up on the fact that the Random object was being used, and it appears that you (@sysprog) missed it as well.

My other comment about the size limitations on 64-bit integers is still applicable, though.
 
  • Like
Likes sysprog
  • #9

FAQ: Is this the correct implementation of Fermat's Primality Test?

What is Fermat's Primality Test?

Fermat's Primality Test is a probabilistic algorithm used to determine whether a given number is prime or not. It is based on Fermat's Little Theorem, which states that if a number p is prime, then ap-1 ≡ 1 (mod p) for any integer a.

How does Fermat's Primality Test work?

Fermat's Primality Test works by randomly choosing a number a between 2 and n-1, where n is the number being tested for primality. It then checks whether an-1 ≡ 1 (mod n). If the result is not equal to 1, then n is definitely not prime. However, if the result is 1, then there is a high probability that n is prime, but it is not guaranteed.

Is Fermat's Primality Test always accurate?

No, Fermat's Primality Test is not always accurate. It can sometimes incorrectly identify a composite number as prime. This is because there are numbers known as "Carmichael numbers" that satisfy Fermat's Little Theorem for all possible values of a, even though they are not prime. However, the chances of this happening are very low, and the test is accurate for most numbers.

How is Fermat's Primality Test different from other primality tests?

Fermat's Primality Test is a probabilistic test, meaning that it does not always give a definite answer. Other primality tests, such as the Sieve of Eratosthenes or the AKS Primality Test, are deterministic and will always give a correct answer. However, these tests are more computationally intensive and may not be practical for large numbers.

Is Fermat's Primality Test used in real-world applications?

Yes, Fermat's Primality Test is used in various real-world applications, such as cryptography and number theory research. It is also commonly used as a preliminary test before using more computationally intensive primality tests. However, it should not be used as the sole method for determining primality, as it is not always accurate.

Similar threads

Replies
9
Views
2K
Replies
2
Views
1K
Replies
3
Views
3K
Replies
2
Views
4K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
4
Views
2K
Back
Top