Question

Using the Fermat primality test, calculate how many Fermat witnesses AND how many Fermat liars there...

Using the Fermat primality test, calculate how many Fermat witnesses AND how many Fermat liars there are for 341 using computer programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As men-ticned ke queshan, useused ie e cale is Compi // 忄nclude the necel@l headers // lap til Me ecndiHan sa frer η check. adition イ+ (expo 1,2 5 リ Il cempote 17 Retton value // rnehad怬nnat (丿 bool feamct L // Declarand in帤aliyettfaWachecis coneifen Retan oietoon false // ch ck condi4f0า Mou petono 기etan falge e men Melt nm CRead fopot u chect ondatfe else lI Pause}

Sample Output K cluserslamudhankandasameldocumentslvisu... a Enter an input to test prinality: 341 Nunber of Fermat lairs: 1 341 is not prine

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;
}

Add a comment
Know the answer?
Add Answer to:
Using the Fermat primality test, calculate how many Fermat witnesses AND how many Fermat liars there...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT