Question

Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following...

Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments):

1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random)

2. A method to compute modular exponentiation. It will require that you also implement the algorithm for modular addition and multiplication.

3. A method to test primality. Since the numbers you will be testing are randomly generated, use the first algorithm we discussed with a single test 3 N−1 ≡ 1(mod N)

Test your algorithm with randomly generated 16-bit numbers. Specifically, use it to generate 100 numbers (presumably prime), and check how many of them are primes by a brute-force approach of trying all divisors up to the square root of the number; you will be able to use regular integer arithmetic to do it). How many times did your algorithm come back with a wrong answer? Should that surprise you or is it consistent with what we discussed in class?

Use your algorithm to generate prime numbers with 16, 32, 64 and 128 bits. In each case, report the number of randomly generated numbers before the prime was found. What should it be in theory? Are your results consistent with the theory?

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

#include<iostream>

using namespace std;

bool isPrime(int n){
   for (int i=2; i< n; i++)
   {
       if (n % i == 0)
       {
           return false;
       }
   }
   return true;
}

int main(){
   int n;
   cout<<"Enter number:"<<endl;
   cin>>n;
   int i = 0;
   while( !isPrime(n+i)){
       i++;
   }
   cout<<n+i;
}

Add a comment
Know the answer?
Add Answer to:
Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following...
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
  • Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime...

    Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime numbers. To write your program, you will need to use the following programming features: The random library (module) A function A variable that will prompt the user to say how many random numbers the program should generate A for loop with a range function A randint function taken from the random library (module); the randint function takes two arguments – the beginning and the...

  • Problem 3.7. We wish to use the AKS algorithm to test whether 211 is a prime...

    Problem 3.7. We wish to use the AKS algorithm to test whether 211 is a prime number explanation of AKS. 24 a)211 See the wikipedia reference for a concise ex 1. Use a computer to compute if ( 211a (mod 211) for a suitable choice of a. Be sure to explain why your choice of a is allowed. Does this allow us to conclude -T211 that 211 is prime? 2. Compute our upper bound r < n using the AKS...

  • for matlab programming beginner level Q1) Write an algorithm for finding the prime numbers between 1...

    for matlab programming beginner level Q1) Write an algorithm for finding the prime numbers between 1 and 100 and displaying them. As you know; prime numbers are the ones that can only be divided by 1 and themselves, Example: 5 is a prime number. Write your algorithm in pseudocode and as flowchart. I

  • 3. (50%) Use a programming language you familiar with to implement the brute force method and...

    3. (50%) Use a programming language you familiar with to implement the brute force method and the branch and bound algorithm, both of which were already introduced in the class, for solving the traveling salesperson problem (35%). Compare their running time when the input size n is from 5 to 15 in steps of 1 (15%). Note that for each n, you should generate three problem instances and average the running time of solving these three instances. To verify the...

  • Problem 1. Simplify the following using modular exponentiation. This is the same kind of problem ...

    Problem 1. Simplify the following using modular exponentiation. This is the same kind of problem as one on the previous homework. But this time I want you to do them using your calculators, as I showed you how to do in class. The point is to If you don't notice a big improvement in how smoothly and quickly you work, then make up some more random problems like this and do them. The book also has some too. (a) 17463...

  • please complete exercises 10.4, 10.5, 10.6, 10.7 and 10.9, thank you so much! (I dont understand...

    please complete exercises 10.4, 10.5, 10.6, 10.7 and 10.9, thank you so much! (I dont understand your comment what is qs 3.6?) 10.4 Exercise. Show that the algorithm descrihed in Question 3.6 for com puting a (mod n) is a polynomial time algorithm in the number of digits in r In the next scrics of problems you will cxplore the usc of this opcration as a means of testing for primality by starting with a familiar theorem. Theorem (Fermat's Little...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Implement the PLA algorithm using Python (Only language I know). Generate linearly separable data using at...

    Implement the PLA algorithm using Python (Only language I know). Generate linearly separable data using at least 20 points with two features, it is not required that the points be evenly divided between the positive and negative class. Run and test your code. Please show working code with explanations to help me understand how to implement my own. P.S. You guys are awesome Regarding Comment: "No information providing regarding dataset, features" It is a data set of nothing in particular,...

  • java Write an application that input a number from the user and checks if all digits...

    java Write an application that input a number from the user and checks if all digits are prime numbers using method prime. The number entered can be of any size. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don’t use an if to check numbers from 1 – 9; you need to find an algorithm to check...

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