Guest User

Untitled

a guest
May 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #You were using a logic that applies to any number, use modulo % for this
  2.  
  3. # 1st example..
  4. a = 1
  5. while a < 1000:
  6. divisors = [i for i in range(2, a + 1) if a % i == 0]
  7. if len(divisors) == 1:
  8. print(a)
  9. a += 1
  10.  
  11. # 2nd example..(most comprehensible)
  12. for a in range(1, 1001):
  13. for i in range(2, a + 1):
  14. if a % i == 0 and i != a:
  15. break
  16. else:
  17. print(a)
  18.  
  19.  
  20. # 3nd example..
  21. for a in range(1, 1001):
  22. if len([i for i in range(2, a + 1) if a % i == 0]) == 1:
  23. print(a)
  24.  
  25. # 4th but not advisable for a total newbie..
  26. maxnum = int(input("Up to what number check primeness?: "))
  27. 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