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

Solutions

Expert Solution

    
def largestGap(num):
    f_prime=0;"""to store the latest prime number"""
    s_prime=0;"""To store the previous prime number"""
    gap=0;"""to store the gap between two successive prime numbers"""
    for j in range(2,num):
        cnt=0;
        for i in range(2,j/2):
            if(j%i == 0):
                cnt=1;
                break;
        if(cnt == 0):
           s_prime=f_prime;
           f_prime=j;
           if(gap<f_prime-s_prime):
               gap=f_prime-s_prime;
    print(gap); 
    
largestGap(100);  

The j loop is to iterate from 2 till the number passed to the function.

i loop is to check if the number passed from j loop is prime or not.

it checks if the value of j is divisible by 2 till number/2.if divisible,breaks from the loop;

if not divisble by any number from 2 till number/2,its a prime.this value is assigned to f_prime and the previous number is assigned to s_prime.

then checks if gap is less than f_prime-s_prime. if yes gap is assigned this value.


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