Techgig Solution 2021|Prime Game Question solution for python

Techgig Solution 2021|Prime Game Question Solution




Prime Game:

Game:Rax will randomly provide you a range [ L , R ] (both inclusive) and you have to tell him the maximum difference between the prime numbers in the given range. There are three answers possible for the given range.


1.There are two distinct prime numbers in the given range so the maximum difference can be found.


2.There is only one distinct prime number in the given range. The maximum difference in this case would be 0.


3.There are no prime numbers in the given range. The output for this case would be -1

To win the game the participant should answer the prime number  difference correctly for the given range.

Example:Range[1,11]

The maximum difference between the prime numbers in the given range is 5.


The maximum difference between the prime numbers in the given range is 9

Difference =11-2=9


Range:

There is only one distinct prime number



EXPLAINATION :

Testcase1:[5-3]=2

Testcase2:[11-7]=4

Testcase3:No Prime number in the given range.Output:-1

Testcase4:[37-23]=14

Testcase5:[93-53]=40

Ans.


Steps to follow:

1.Let understand the program and we have to input all the prime numbers between start to ending and then we have to find difference between first and last prime number

2.So first we will take empty list so that we can do some operations.

3.Then now we will input starting and ending number,then we will apply for prime number logic to find prime number between starting to end number.

4.So if we have found the prime number between start to ending,then we will add all prime number in the empty list

5.Now our last step that we will take for loop to excess every elements,so now we will subtract zero index value and ending index value of the list.


Now if you are not understanding the program and it's logic, then read all three parts of the program. By reading all three parts you will understand the program. Part-1 In the whole program we have to just find difference between start and last prime number that you will find. In first part first you will take input of start and ending number using input() function. Then we use prime number logic to find prime number between start to end. If you don't know how to find find number then click in below link. Part-2 In the second part now we have got the prime numbers. So we will take empty list or we can take in the beginning of the program. Then we will use append() function to add every prime number in the list. After adding every prime number in the list . Now use for loop to access every elements of the list. Part-3 In the last part we are accessing the every elements, so we will find difference between starting and ending number in the list. Here you can stop your program. But to check the difference is zero, negative or positive use if-elif-else structure and use print() to print the difference is zero or negative or positive.

You can copy it by selecting code 



Flow of program-

First 

start=1

Ending=10

i=1

Here flag=0

Then , i=2

Flag=1

Then L=[2]

Now  program goes to another for loop and now value j=2 and 2%2=0

Again loop run

Now, j=3

L=[2, 3]

j=4

4%4=0

Again loop run, 

 j=5

5 is completely divisible by 2 , 3 and 4.

Then, L=[2, 3,5]

j=6

It is divisible by all numbers. 

Then, j=7

L=[2, 3,5,7]

j=8

It is also divisible by all numbers. So it is also not prime. 

j=9

It also divisible by 1,3,9 , so it also not prime. 

Because for prime number max it can be divided by only 2 numbers I. E 1 and itself. 

L=[2, 3,5,7]

Now difference=2-7=-5

It 


lst=[ ]
#Here we have to find out prime number between start to ending
start=int(input("Enter a first number:")) #Starts mean first number

#Now input ending
end=int(input("Enter a last number:"))  #end means last number

#Now take loop to get all prime numbers between start to ending number
for i in range(start,end+1):
    flag=1   # assume number is prime
#Now test the condition
    if i==1:
        flag=0  #Assume number is not prime
    if i ==2:
        
        flag=1
    #If the condition is false then go to else part
    #And take another loop to check the number is prime or not this loop willgo upto 2 to ending befpre one number

    else:
        for j in range(2,i):
            if i%j==0:
    #If the value of i will  divide by any value of j then loop will break 
                flag=0
                break
    #Now again test condition if number prime then print and append in empty list i.e L
    if flag==1:
        lst.append(i)
print(lst)
n=len(lst)

diff=0    #diff means difference is 0
if n>=1:
    
    for i in range(0,len(lst)):
        diff=lst[0]-lst[len(lst)-1]
    #take diffrence between first and last number if there is two or more than two prime number
    #using print() print
    print("There are  more than two prime number and their differnce is :",diff)

elif n==0:
    print("There are only one prime number and difference is 0.")
elif n==-1:
    print("There is no prime number and difference is -1.")
        
else: 
print("there are more than one prime nber. ")
    
    
        






            

No comments:

Powered by Blogger.