Question

In: Computer Science

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 code that has the sieve() algorithm we studied to calculate all the prime numbers up to n.

Some example test cases (include these test cases in your program):

>>> print(smallest_gap(5, 12)) 2

# The primes between 1000 and 1020 are 1009, 1013, 1019 so the smallest gap

# is 4 as 1013 – 1009 = 4

>>> print(smallest_gap(1000,1020))

4

in python

Solutions

Expert Solution

Python code and screenshort of output shown below

import array
def SieveOfPrime(start_num,end_num):
# array of type boolean with True values in it
s=[]# empty array names s
prime = [True for i in range(end_num+1)]
p = 2
while (p * p <= end_num):
# If it remain unchanged it is prime
if (prime[p] == True):
# updating all the multiples
for i in range(p * p, end_num+1, p):
prime[i] = False
  
p += 1
  
c=0
print ('Prime numbers(',start_num,',',end_num,')are:')
for p in range(start_num,end_num+1):
if prime[p]:
s.insert(c,p)#store only prime numbers in array s
c=c+1
print (p,end=" ")
if c>=2:#count of prime numbers greater than
val=s[1]-s[0]#assign difference of first two numbers as a smallest gap for comaprison
else:#one prime number
return s[0]
for i in range(0,c-1):
diff=s[i+1]-s[i]
if val>diff:#comparing for finding smallest gap between consecutive prime numbers
val=diff

return val#return smallest gap
# main
if __name__=='__main__':
n = 33
print('\nThe smallest gap is:',SieveOfPrime(1000,1020))

screenshort of output


Related Solutions

Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
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...
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 an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
Find the biggest “*prime gap” (see definition) from the prime numbers between 1 and 1,000,000. *Prime...
Find the biggest “*prime gap” (see definition) from the prime numbers between 1 and 1,000,000. *Prime gap = the difference between two consecutive primes. The exercise can be completed manually or with a computer program. Whichever seems easiest.
1. Write a python function that receives two positive numbers and displays the prime numbers between...
1. Write a python function that receives two positive numbers and displays the prime numbers between them.Note: A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself. 2. Using either Whileor Foriteration loops, write a python code that prints the first Nnumbers in Fibonacci Sequence, where N is inputted by the user. Now, revise this code to instead print a) the Nthnumber in Fibonacci Sequence.b) the...
C# Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will...
C# Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will produce the original number. Consider the following example: Prime factors of 4 are: 2 x 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 Prime factors of 50 are: 2 x 5 x 5 Create a console application with a method named PrimeFactors that,...
Use "do...while" loops to write a program that finds all prime numbers less than a specified...
Use "do...while" loops to write a program that finds all prime numbers less than a specified value.
Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers...
Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers on request. Think of the object as being similar to the digital tasbeeh counter. It has a reset button which makes the counter return to 0. There is also a button to get the next prime number. On the tasbeeh, pressing this button increments the current value of the counter. In your object, pressing this button would generate the next prime number for display....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT