Question

In: Computer Science

A number is prime if it can be divided exactly only by 1 and itself. Here...

A number is prime if it can be divided exactly only by 1 and itself. Here is an algorithm for checking if an input number is prime:

function out = isprime(m)

            for j:=2 to m-1

            if mod(m,j) ==0 then

            return “input is not a prime”

            endif

            endfor

            return ”input is a prime”

Let n denote the size of the input m, i.e. the number of bits in the binary expression for m. What is the running time of the algorithm as a function of n? (that is, give the running time in the form O(f(n)) for the appropriate function f). Can you suggest an asymptotically faster algorithm?

Solutions

Expert Solution

Solution for the problem is given below, please comment if any doubts:

Complexity of the given algorithm:

Here the prime number checking algorithm takes the input ‘m’ and check for prime number.

The “for” loop needs to runs from whole “2” to “m-1” in worst case, that is if the number is prime the algorithm needs to check each and every number from “2” to “m-1”, here “n” denotes the input size in binary. For “m” number the number of bits, n= floor(log2(m))+1

(n-1)= floor(log2(m))

m = (2n-1), in average case

Now the complexity is in order of O(m), in the function of n, it is O(2n-1)

Asymptotically faster algorithm:

  • In the given algorithm the for loop checks from “2” to “m-1”, but theories said that we need to check only up to square root of (m).
  • Thus the given algorithm can be modified by replacing “for j:=2 to m-1” with “for j:=2 to square_root(m)”.
  • The complexity will be reduced from O(m) to O(m).
  • In terms of n, O(2(n-1)/2)

Related Solutions

5. A prime number is a number that is only evenly divisible by itself and 1....
5. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, how‐ ever, is not prime because it can be divided evenly by 1, 2, 3, and 6.   Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise....
isPrime Function. A prime number is a number that is only evenly divisible by itself and...
isPrime Function. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. The...
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume...
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume that we are given a list of all the prime numbers from 1 to 10,000, in sorted order in a text file called primes.txt: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a java program which reads a list of N integers and prints the number of prime numbers in the list. Input: The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output:...
A prime number is an integer greater than 1 that is evenlydivisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate...
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. Java
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. b) Modify the...
Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
A prime number (or prime) is a natural number greater than 1 that has no posítive...
A prime number (or prime) is a natural number greater than 1 that has no posítive divisors other than 1 and itself. Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT