+ 1

Can anyone point out my mistake? why i am not having the desired result?

a = int(input('Enter a number') b = 1 while (b<=a): if a%b==0: print('Prime') else: print('Not Prime')

29th Feb 2020, 8:36 AM
Laveezah Noor
Laveezah Noor - avatar
4 Réponses
+ 4
1. There is a mistake in your input line. a= int(input()) 2. All numbers are divisible by 1, so your code will present all numbers as prime. 3. Because b will be smaller than the numbers you are testing, you will get an infinite loop. HINT: Try using a loop starting from 2 until the input number. Then test to see if each number in the loop is divisible to the input number. If not: PRIME if yes: NOT PRIME
29th Feb 2020, 9:22 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Missing statement of b value incrementing.., so for greater than zero value, it is Infinite loop.. And for prime number, factors only 1, and itself.. So no need to check 1 and from 2 to that number if any divisor present then break loop by printing not prime.. It it non of divisible by any then it it prime...
29th Feb 2020, 9:13 AM
Jayakrishna 🇮🇳
+ 2
Because any number is divisible by 1 and itself. There would now be an infinite loop, can you fix it?
29th Feb 2020, 9:14 AM
Seb TheS
Seb TheS - avatar
+ 1
you have an infinite loop with a> = 1, you need to start with b = 2, then increase it to a-1, and if a % b == 0 is True, then the number has more than two divisors, and is not a prime number, the loop must be break, or the else condition, which must be after the cycle and report a prime if the loop ended without interruption. ..
29th Feb 2020, 9:21 AM
Vitaly Sokol
Vitaly Sokol - avatar