Skip to main content
5 of 8
deleted 4 characters in body
rayryeng
  • 1.6k
  • 1
  • 15
  • 20

Python 2, 46 65 bytes

Fix made thanks to feersum & Mauris

This finds the remainder / modulus of the input integer n divided by every number from 2 up to n-1. If there is at least one number in this sequence that has no remainder, or results in 0, this means that the number is not prime. If every value in this sequence is non-zero, the value is prime. Also by definition, 1 isn't prime and so that has to be taken care of separately.

def f(n):print(False if n==1 else all([n%i for i in range(2,n)])) 

Example Runs

I ran this in IPython:

In [31]: def f(n):print(False if n==1 else all([n%i for i in range(2,n)])) In [32]: f(1) False In [33]: f(2) True In [34]: f(3) True In [35]: f(5) True In [36]: f(4) False In [37]: f(6) False In [38]: f(10) False In [39]: f(13) True In [40]: f(15) False In [41]: f(19) True In [42]: f(120) False 
rayryeng
  • 1.6k
  • 1
  • 15
  • 20