[HackerRank][Python3] Time Complexity: Primality
2018. 7. 21. 02:41 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/ctci-big-o/problem
My Solution :
#!/usr/bin/env python3 def primality(n): if n < 2: return False if n < 4: return True if n % 2 == 0 or n % 3 == 0: return False for i in range(5, int(n**0.5)+2, 2): if n % i == 0: return False return True p = int(input()) for _ in range(p): n = int(input()) result = primality(n) print('Prime') if result else print('Not prime')
Comment :
이 문제는 소수 여부를 판별하는 것이고, n 이하의 모든 소수를 구하는 문제는 아래 풀이 참고 요망
'프로그래밍 > HackerRank' 카테고리의 다른 글
[HackerRank][Python3] BFS: Shortest Reach in a Graph (0) | 2018.07.22 |
---|---|
[HackerRank][Python3] Find the nearest clone (0) | 2018.07.22 |
[HackerRank][Python3] DFS: Connected Cell in a Grid (0) | 2018.07.21 |
[HackerRank][Python3] Friend Circle Queries (0) | 2018.07.21 |
[HackerRank][Python3] Recursion: Davis' Staircase (0) | 2018.07.21 |
[HackerRank][Python3] Recursion: Fibonacci Numbers (0) | 2018.07.20 |
[HackerRank][Python3] Tree: Huffman Decoding (0) | 2018.07.20 |
[HackerRank][Python3] Binary Search Tree : Lowest Common Ancestor (0) | 2018.07.20 |
최근에 달린 댓글 최근에 달린 댓글