I am not sure what this is supposed to mean. i followed all instructions clearly.
also, does anyone review these codes? or is it automated?
I am not sure what this is supposed to mean. i followed all instructions clearly.
also, does anyone review these codes? or is it automated?
Hello @gunjanarora, you wrote the code wrong. We don’t need to write the input line that you have written. Moreover, the code is also not correct. Pls tell me the logic that you understood after that we will correct that out. And it is automatically judged but if you are sure your code is right and it is not showing expected outcome then you can reach out to us (but this is the rare case
)
I used the logic that if a number is divisible by itself and one only, then it is prime. I then proceeded to use a for loop from 1 to a (the number given by the user), checking divisibility by each number from 1 to a. If a is divisible by any of them, I added 1 to a variable b (which shows number of factors)
Since a number is obviously divisible by 1 and itself, the minimum value of b is 2. But if it has more than 2 factors, then the value of b is greater than 2, which means it is not prime.
Please review as this is the only logic that came to my mind. And it gave the correct output too.
ok ok, we don’t need to do that. In these questions, there is one concept of time complexity which signifies how much expected time it will take to run and for this question, your solution is taking too much time in this case. So we need to use O(root n) method probably you know. Pls try that method and if you don’t know about that method then pls kindly ask. And also take care of the indentation of your code.
Actually I dont know what that is. And I don’t remember this being taught as well.
Also, could you please tell me what’s wrong with the indentation? I’m not sure where I went wrong.
print("Enter number between 2 and 1000000000")
a = int(input())
b = 0
for i in range(1,a+1):
if a % i == 0:
b = b + 1
if b == 2:
print("Prime")
else:
print("Not Prime")
This is your code right, here we are running this loop from 1 to a and this is too long to go and calculating the factors. And we don’t need to print the starting line this print(“Enter number between 2 and 1000000000”)
It is never asked or mentioned in the question.
See the below approach we are taking a number first of all and I am start dividing the no. with 2 because it is obviously true that the number is divisible by 1. And if we get any no. that divides the n except 1 and that no. itself that means it is not a prime no. And I will break the loop. Pls go through the code and pls clarify all your doubts or confusion if there is any.
n = int(input())
i = 2
isPrime = True
while i*i <= n:
if n%i == 0:
isPrime = False
break
i = i+1
if n < 2:
isPrime = False
if isPrime:
print('Prime')
else:
print('Not Prime')
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.