Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #You were using a logic that applies to any number, use modulo % for this
- # 1st example..
- a = 1
- while a < 1000:
- divisors = [i for i in range(2, a + 1) if a % i == 0]
- if len(divisors) == 1:
- print(a)
- a += 1
- # 2nd example..(most comprehensible)
- for a in range(1, 1001):
- for i in range(2, a + 1):
- if a % i == 0 and i != a:
- break
- else:
- print(a)
- # 3nd example..
- for a in range(1, 1001):
- if len([i for i in range(2, a + 1) if a % i == 0]) == 1:
- print(a)
- # 4th but not advisable for a total newbie..
- maxnum = int(input("Up to what number check primeness?: "))
- print('\n'.join(str(a) for a in [n for n in range(1, maxnum + 1) if len([i for i in range(2, maxnum + 1) if n % i == 0]) == 1]))
Add Comment
Please, Sign In to add comment