티스토리 뷰

공부

[Python] IsPrime

승가비 2021. 9. 5. 19:13
728x90
def is_prime(n):
  if n == 2 or n == 3: return True
  if n < 2 or n%2 == 0: return False
  if n < 9: return True
  if n%3 == 0: return False
  r = int(n**0.5)
  # since all primes > 3 are of the form 6n ± 1
  # start with f=5 (which is prime)
  # and test f, f+2 for being prime
  # then loop by 6. 
  f = 5
  while f <= r:
    print('\t',f)
    if n % f == 0: return False
    if n % (f+2) == 0: return False
    f += 6
  return True

https://stackoverflow.com/questions/15285534/isprime-function-for-python-language/15285588

 

isPrime Function for Python Language

So I was able to solve this problem with a little bit of help from the internet and this is what I got: def isPrime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return Fa...

stackoverflow.com

 

728x90

'공부' 카테고리의 다른 글

[Java] lombok @Data  (0) 2021.09.06
[Mac] Terminal Hostname 변경  (0) 2021.09.05
[Python] IsPrime  (0) 2021.09.05
[Java] new line replacement  (0) 2021.09.05
[Java] How to forward large files with RestTemplate?  (0) 2021.09.05
[Python] Tensorflow v1 (env v2)  (0) 2021.08.13
댓글
댓글쓰기 폼