Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
742 B
0
Indexable
Never
#User function Template for python3

import math

class Solution:
    def isPrime (self, N):
        # As number 1 is not prime hence
        if N == 1:
            return 0
        
        # Now we can check whether the N is divisible by any of the numbers,
        # The idea i am using is that, atleast one multiple should be below sqrt(n)
        # hence taking the upper limit to sqrt(n):- making the time complexity to sqrt(n)
        # Also the space complexity will be O(1)
        
        SqrtN = int((math.sqrt(N)) // 1)
        # print(SqrtN)
        
        for i in range(2,SqrtN + 1):
            if N % i == 0:
                return 0
        
        return 1
                
        # code here
Leave a Comment