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

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
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
# RQ3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>>...
# RQ3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>> largest_factor(15) # factors are 1, 3, 5 5 >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40 40 """ "*** YOUR CODE HERE ***" # RQ4 # Write functions c, t, and f such that calling the with_if_statement and # calling the with_if_function do different things. # In particular, write the functions (c,t,f) so that calling with_if_statement function returns...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT