Posts

Check the given number is power of 2 using BitWise

  # if a number is power of 2 then n&(n-1) results 0 #lets see n=4 #in binary 4 is 100 #in binary 3 is 011 #so bitwise & = 000 =>0 def ispowerof2(n):     x=n      y=n&(n-1)          print(x and not(y))      n=int(input()) ispowerof2(n)  

Find the even odd using bitwise

  We can use bitwise And to check even or odd if n is a number then  n&1 result the 1 else result the 0 def oddeven(n):     if n&1==1:         print("ODD")     else:         print("EVEN")           n=int(input()) oddeven(n)

Print all Prime numbers Using Sieve of Eratosthenes

  from math import sqrt def allPrime(n):     l=[True]*(n+1)     l[0]=False     l[1]=False     for i in range(2,int(sqrt(n))+1):         if l[i]==True:             for j in range(i*i,n+1,i):                 l[j]=False                      for i in range(n+1):         if l[i]==True:             print(i,end=" ")   n=int(input()) allPrime(n)

Is it a Prime?

  from math import sqrt def isPrime(n):     if n==0 or n==1:         return False     if n==2 or n==3:         return True     if n%2==0 or n%3==0:         return False     for i in range(5,int(sqrt(n))+1):         if n%i==0 or n%(i+2)==0:             return False     return True      n=int(input()) print("Is",n,"a Prime",isPrime(n))