two integers X and K. Varsha needs to determine whether there is an integer A such that it has exactly X positive integer divisors and exactly K of them are prime numbers.
Answers
Program to find a number within range that has exactly X positive integer divisors and exactly K of them are prime numbers.
Language used : Python Programming
Program :
n=int(input("Enter the range of numbers in which you need to check : "))
X=int(input("Enter no.of positive integers divisors the number need to have : "))
K=int(input("Enter no.of positive prime integers divisors the number need to have : "))
count1=0
count2=0
flag=0
l=[]
finallist=[]
st=""
for num in range(0,n):
if num>1:
for i in range(2,num):
if (num % i) == 0:
break
else:
l.append(num)
for i in range(1,n):
for j in range(1,i+1):
if i%j==0:
count1+=1
if j in l:
count2=count2+1
if count1==X and count2==K:
finallist.append(i)
count1=0
count2=0
if len(finallist)==0:
st="None"
else:
for i in finallist:
st=st+str(i)+" ,"
st=st[:-2]
print("Numbers within range which have exactly X, i.e. ",X," positive integer divisors and exactly K i.e., ",K," of them are prime numbers is/are :", st,".")
Inputs and outputs : (Various Testcases)
Input 1 :
Enter the range of numbers in which you need to check : 30
Enter no.of positive integers divisors the number need to have : 8
Enter no.of positive prime integers divisors the number need to have : 2
Output 1 :
Numbers within range which have exactly X, i.e. 8 positive integer divisors and exactly K i.e., 2 of them are prime numbers is/are : 24 .
Input 2 :
Enter the range of numbers in which you need to check : 20
Enter no.of positive integers divisors the number need to have : 3
Enter no.of positive prime integers divisors the number need to have : 1
Output 2 :
Numbers within range which have exactly X, i.e. 3 positive integer divisors and exactly K i.e., 1 of them are prime numbers is/are : 4 ,9 .
Input 3 :
Enter the range of numbers in which you need to check : 50
Enter no.of positive integers divisors the number need to have : 8
Enter no.of positive prime integers divisors the number need to have : 8
Output 3 :
Numbers within range which have exactly X, i.e. 8 positive integer divisors and exactly K i.e., 8 of them are prime numbers is/are : None .
Explanation :
- First take the range n, X and K values as input and prepare a list of prime till range n.
- Then, write a loop that iterates till n, and with iterator variable i, and inner loop till the range i, such that i modulo jth value to get the divisiors count.
- If you find i to be the divisor or then check if the divisor is a prime or not using the list prepared. If it is a prime, increment prime count.
- If it is a prime, increment prime count. After the completion of one value, check X value with divisor count and K value with prime count. If they are equal, push them into a list of final values.
- After the completion of the computations on every value within the range, print the values that satisfies the users input.
If you go through the test cases given,
When n=30, X=8, Y=2 is taken, it resulted 24 as the answer, as the number 24 has 8 divisors [1, 2, 3, 4, 6, 8 , 12, 24] in which 2 are prime divisors [2, 3]. 24 is the only number that satisfies the given data in the range.
Similarly, the other two cases too work.
Note : I have added the attachments of the interpretation and the execution of programs with their outputs. Take a look!
Quick Tip : In python, you must and should follow Indentation. Following the indentation property in any programming language will help you in quickly analysing the code, if an error occurs.
Learn More :
- If you are new to python, and wants to try out for loop as a beginner, here we have a small gift for you.
https://brainly.in/question/8287051
- Want to check if a number is prime or not? Have a look at this beginner friendly code.
https://brainly.in/question/15723722
Hope it helps you. Cheers!