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 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...
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.
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Write a program that prints the count of all prime numbers between A and B (inclusive),...
Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7,...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT