Question

In: Computer Science

Given a number n, what is the largest gap between successive primes which are less than...

Given a number n, what is the largest gap between successive primes which are less than number n?

For example, if n = 12, the prime numbers less than 12 are: [2, 3, 5, 7, 11]. The largest gap between any two prime numbers in this list is 4, so the function would return '4'.

>>>print(largestGap(12))
4

Write Python code to solve this problem, and include the following 3 test cases in your answer.

>>>print(largestGap(100))
8
>>>print(largestGap(1000))
20
>>>print(largestGap(10000))
36

In PyCharm

Solutions

Expert Solution

def checkIfNumberIsPrime(num):
    if num >1:
        for i in range(2,num):
            if num%i ==0:
                return 0
        return 1
    else:
        return 0
        
#target find the max difference between 2 consecutive primes
previousPrime=1 #assuming first prime is stored in previousPrime and as first prime is 1 so we initialize the value to 1
maxDifference=0 # initialize the maxDifference with default value 0
number=int(input("Enter a number")) #ask the user a number upto which we want to find the max difference
for i in range(2,number): #as first number is initialized to 1 so we start iteration from 2 and repeat it till the user entered number
    x=checkIfNumberIsPrime(i) # checks if the number in current iteration is prime or not
    if(x==1): #this block gets executed if the number is prime
        if i-previousPrime > maxDifference: #now check if the difference between previousPrime and current prime i.e number in current iteration is greater than maxDifferent if yes then we update the maxDifferent to the difference between previousPrime and current prime number and as we will have to find difference between cosecutive prime numbers so we update the previousPrime with currentPrime
            maxDifference=i-previousPrime
        previousPrime=i

print("Max difference between primes:",maxDifference)

Related Solutions

Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. You may want to modify your solution from Problem 1 on Assignment 4, or you can use this starter...
If n>=2, prove the number of prime factors of n is less than 2ln n.
If n>=2, prove the number of prime factors of n is less than 2ln n.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output:...
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output: 2,3,5,7 n=16, output: 2,3,5,7,11,13 In Java please
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7,...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7, output: 2,3,5,7,11,13,17. In Java please
Guidelines for grouping scores suggest you use _____________ groups. the number of less than 15 between...
Guidelines for grouping scores suggest you use _____________ groups. the number of less than 15 between 5 and 15 over 20 between 0 and 5 Flag this Question Question 322 pts In a frequency histogram, the horizontal dimension is called the _____________, and the vertical dimension is called the _____________. ordinate; abscissa y-axis; x-axis ordinate; ascendent abscissa; ordinate Flag this Question Question 332 pts In a scatterplot, each data point represents _____________ measurement(s) from ______________ individual(s). one; two multiple; multiple...
The main process by which a recessionary gap is eliminated is a(n)
The main process by which a recessionary gap is eliminated is a(n) increase in wages that shifts the aggregate supply curve inward. drop in wages that shifts the aggregate demand curve inward. increase in wages that shifts the aggregate demand curve outward. drop in wages that shifts the aggregate supply curve outward
Design a Turing machine that, given a positive binary number n greater than or equal to...
Design a Turing machine that, given a positive binary number n greater than or equal to 2, subtracts 2 from this number. Test it, step-by-step, on the example of n = 2.
prove that 2/pi is less than or equal to (sinx)/x which is less than or equal...
prove that 2/pi is less than or equal to (sinx)/x which is less than or equal to 1. for x is in (0,pi/2]
The number of minutes between successive bus arrivals at Mehmet's bus stop is exponentially distributed with...
The number of minutes between successive bus arrivals at Mehmet's bus stop is exponentially distributed with unknown parameter . Mehmet's prior information regarding  (real valued, i.e., a continuous random variable) can be summarized as a continuous uniform PDF in the range from 0 to 0.57. Mehmet has recorded his bus waiting times for three days. These are 26, 11, and 25 minutes. Assume that Mehmet's bus waiting times in different days are independent. Compute the MAP (maximum a posteriori probability) rule...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT