Skip to main content
added 401 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 2, 42 bytes

f=lambda n,p=2:n%p and f(n,p+1)or p**n%n<1

Try it online!

Since Python doesn't have any built-ins for primes, we make do with checking divisibility.

We find the smallest prime p that's a factor of n by counting up p=2,3,4,... until n is divisible by p, that is n%p is zero. There, we check that this p is the only prime factor by checking that a high power of p is divisible by n. For this, p**n suffices.

As a program:

43 bytes

n=input()
p=2
while n%p:p+=1
print p**n%n<1

Try it online!

This could be shorter with exit codes if those are allowed.

46 bytes

lambda n:all(n%p for p in range(2,n)if p**n%n)

Try it online!

Python 2, 42 bytes

f=lambda n,p=2:n%p and f(n,p+1)or p**n%n<1

Try it online!

Since Python doesn't have any built-ins for primes, we make do with checking divisibility.

We find the smallest prime p that's a factor of n by counting up p=2,3,4,... until n is divisible by p, that is n%p is zero. There, we check that this p is the only prime factor by checking that a high power of p is divisible by n. For this, p**n suffices.

As a program:

43 bytes

n=input()
p=2
while n%p:p+=1
print p**n%n<1

Try it online!

This could be shorter with exit codes if those are allowed.

Python 2, 42 bytes

f=lambda n,p=2:n%p and f(n,p+1)or p**n%n<1

Try it online!

Since Python doesn't have any built-ins for primes, we make do with checking divisibility.

We find the smallest prime p that's a factor of n by counting up p=2,3,4,... until n is divisible by p, that is n%p is zero. There, we check that this p is the only prime factor by checking that a high power of p is divisible by n. For this, p**n suffices.

As a program:

43 bytes

n=input()
p=2
while n%p:p+=1
print p**n%n<1

Try it online!

This could be shorter with exit codes if those are allowed.

46 bytes

lambda n:all(n%p for p in range(2,n)if p**n%n)

Try it online!

added 352 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 2, 42 bytes

f=lambda n,p=2:n%p and f(n,p+1)or p**n%n<1

Try it online!

Since Python doesn't have any built-ins for primes, we make do with checking divisibility.

We find the smallest prime p that's a factor of n by counting up p=2,3,4,... until n is divisible by p, that is n%p is zero. There, we check that this p is the only prime factor by checking that a high power of p is divisible by n. For this, p**n suffices.

As a program:

43 bytes

n=input()
p=2
while n%p:p+=1
print p**n%n<1

Try it online!

This could be shorter with exit codes if those are allowed.

Python 2, 42 bytes

f=lambda n,p=2:n%p and f(n,p+1)or p**n%n<1

Try it online!

Since Python doesn't have any built-ins for primes, we make do with checking divisibility.

We find the smallest prime p that's a factor of n by counting up p=2,3,4,... until n is divisible by p, that is n%p is zero. There, we check that this p is the only prime factor by checking that a high power of p is divisible by n. For this, p**n suffices.

Python 2, 42 bytes

f=lambda n,p=2:n%p and f(n,p+1)or p**n%n<1

Try it online!

Since Python doesn't have any built-ins for primes, we make do with checking divisibility.

We find the smallest prime p that's a factor of n by counting up p=2,3,4,... until n is divisible by p, that is n%p is zero. There, we check that this p is the only prime factor by checking that a high power of p is divisible by n. For this, p**n suffices.

As a program:

43 bytes

n=input()
p=2
while n%p:p+=1
print p**n%n<1

Try it online!

This could be shorter with exit codes if those are allowed.

Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 2, 42 bytes

f=lambda n,p=2:n%p and f(n,p+1)or p**n%n<1

Try it online!

Since Python doesn't have any built-ins for primes, we make do with checking divisibility.

We find the smallest prime p that's a factor of n by counting up p=2,3,4,... until n is divisible by p, that is n%p is zero. There, we check that this p is the only prime factor by checking that a high power of p is divisible by n. For this, p**n suffices.