Question

In: Computer Science

Using the Fermat primality test, find the number of Fermat witness and number of Fermat liars...

Using the Fermat primality test, find the number of Fermat witness and number of Fermat liars for 341 via computer programming (ex;c++).

Solutions

Expert Solution

Copyable Code

//Include the needed headers
#include <cstring>
#include <iostream>
#include <cstdlib>
#define longlong long long
using namespace std;

//Method modulo()
longlong modulo(longlong base, longlong expo, longlong mod)
{
   // Declare and initialize variables
   longlong c = 1;
   longlong d = base;

   //Loop till the condition satisfies
   while (expo > 0)
   {
       //Check condition
       if (expo % 2 == 1)

           //Compute c
           c = (c * d) % mod;

       //Compute d
       d = (d * d) % mod;

       //Compute exponent
       expo = expo / 2;
   }

   //Return value
   return c % mod;
}

//Method Fermat()
bool Fermat(longlong p, int noOfIterations)
{
   // Declare and initialize variables
   int fLiars = 0;
   int fWitness = 0;

   //Check condition
   if (p == 1)
   {
       //Return
       return false;
   }

   //Loop through noOfIterations
   for (int i = 0; i < noOfIterations; i++)
   {
       //Declare and initialize a
       longlong a = rand() % (p - 1) + 1;

       //Check condition
       if (modulo(a, p - 1, p) != 1)
       {
           //Increment fermat liars
           fLiars++;

           //Display fermat liar
           cout << " Number of Fermat lairs: " << fLiars;
          
           //Return
           return false;
       }
   }

   //Increment fermat witness
   fWitness++;

   //Display fermat witness
   cout << " Number of Fermat witness: " << fLiars;

   //Return
   return true;
}

//Method main()
int main()
{
   //Declare and initialize iteration
   int iteration = 50;

   //Declare a variable for input
   longlong input;

   //Prompt for input
   cout << " Enter an input to test primality: ";

   //Read input
   cin >> input;

   //Check condition
   if (Fermat(input, iteration))

       //Display result
       cout << " " << input << " is prime" << endl;

   //Otherwise
   else
       //Display result
       cout << " " << input << " is not prime" << endl;

   //Pause
   getchar(); getchar();

   //Return
   return 0;
}


Related Solutions

Using Fermat't Little Theorem for primality test. Answer the following. Show work for credit. (a) Test...
Using Fermat't Little Theorem for primality test. Answer the following. Show work for credit. (a) Test whether each of the following numbers is primes: 101, 341, and 1105. Try at least two bases if needed, and state if the number is pseudoprime to any base you try. You may use a claculator to compute large powers. (MS Excel can be used) (b) Find a composit number that is pseudoprime to base 3 and 7 but not pseudoprime to base 2...
Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer...
Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer is a prime. The input of the algorithm is a large positive integer. The output is “the number *** is a prime” or “the number *** is not a prime”. The error probability of the algorithm should be no more than 1 256 . Use this program to test some big integers. In Java, there is a class BigInteger. You can use methods of...
Under the Neil v. Biggers test for witness identification, what are the constitutional requirements that must...
Under the Neil v. Biggers test for witness identification, what are the constitutional requirements that must be met in order to allow the admissibility of evidence of a victim’s or witness’s pretrial identification of the defendant from photographs or a lineup?
Find the most powerful test using neyman pearson lemma.
Find the most powerful test using neyman pearson lemma.
Find the difference between the two groups when using a dependent t-test then find the independent two-sample t-test
data = X1= 14, 8, 6, 14, 20, 13, 9, 12 x2= 13, 4, 1, 8, 10, 12, 8, 6 1. Find the difference between the two groups when using a dependent t-test then find the independent two-sample t-test, and compare the results with those from the dependent t-test
(In this problem first find the probability by using SPSS and then calculate the number of...
(In this problem first find the probability by using SPSS and then calculate the number of trees manually by using the probabilities.) A certain variety of pine tree has a mean trunk diameter of μ=150 cm, and a standard deviation of σ=30 cm which is normally distributed. A certain section of a forest has 500 of these trees. Find Approximately 1. how many of these trees have a diameter smaller than 120 2. how many of these trees have a...
Using the following data, test the question that an equal number of Democrats, Republicans, and the...
Using the following data, test the question that an equal number of Democrats, Republicans, and the Independents voted during the most recent election. Test this hypothesis at the .05 level of significance. Political Affiliation Republican Democrat Independent 800 700 900
Using Ellman’s reagent, find the number of cysteines in a protein that has a concentration of...
Using Ellman’s reagent, find the number of cysteines in a protein that has a concentration of 30 mg/ml solution with a molecular weight of 15,000 Da. Data-- 0.1 ml of the protein was reacted with the reagent and diluted to 10 ml and the absorbance at 412 nm was 1.4 A. Hint, first find the moles of protein used for the experiment, then calculate the moles of Ellman’s reagent that reacted
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 2 Use any sorting algorithm to sort list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT