Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
68
Task/M-bius-function/Python/m-bius-function-1.py
Normal file
68
Task/M-bius-function/Python/m-bius-function-1.py
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
# Python Program to evaluate
|
||||
# Mobius def M(N) = 1 if N = 1
|
||||
# M(N) = 0 if any prime factor
|
||||
# of N is contained twice
|
||||
# M(N) = (-1)^(no of distinct
|
||||
# prime factors)
|
||||
# Python Program to
|
||||
# evaluate Mobius def
|
||||
# M(N) = 1 if N = 1
|
||||
# M(N) = 0 if any
|
||||
# prime factor of
|
||||
# N is contained twice
|
||||
# M(N) = (-1)^(no of
|
||||
# distinct prime factors)
|
||||
|
||||
# def to check if
|
||||
# n is prime or not
|
||||
def isPrime(n) :
|
||||
|
||||
if (n < 2) :
|
||||
return False
|
||||
for i in range(2, n + 1) :
|
||||
if (i * i <= n and n % i == 0) :
|
||||
return False
|
||||
return True
|
||||
|
||||
def mobius(N) :
|
||||
|
||||
# Base Case
|
||||
if (N == 1) :
|
||||
return 1
|
||||
|
||||
# For a prime factor i
|
||||
# check if i^2 is also
|
||||
# a factor.
|
||||
p = 0
|
||||
for i in range(1, N + 1) :
|
||||
if (N % i == 0 and
|
||||
isPrime(i)) :
|
||||
|
||||
# Check if N is
|
||||
# divisible by i^2
|
||||
if (N % (i * i) == 0) :
|
||||
return 0
|
||||
else :
|
||||
|
||||
# i occurs only once,
|
||||
# increase f
|
||||
p = p + 1
|
||||
|
||||
# All prime factors are
|
||||
# contained only once
|
||||
# Return 1 if p is even
|
||||
# else -1
|
||||
if(p % 2 != 0) :
|
||||
return -1
|
||||
else :
|
||||
return 1
|
||||
|
||||
# Driver Code
|
||||
print("Mobius numbers from 1..99:")
|
||||
|
||||
for i in range(1, 100):
|
||||
print(f"{mobius(i):>4}", end = '')
|
||||
|
||||
if i % 20 == 0: print()
|
||||
# This code is contributed by
|
||||
# Manish Shaw(manishshaw1)
|
||||
64
Task/M-bius-function/Python/m-bius-function-2.py
Normal file
64
Task/M-bius-function/Python/m-bius-function-2.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
# Python Program to evaluate
|
||||
# Mobius def M(N) = 1 if N = 1
|
||||
# M(N) = 0 if any prime factor
|
||||
# of N is contained twice
|
||||
# M(N) = (-1)^(no of distinct
|
||||
# prime factors)
|
||||
import math
|
||||
|
||||
# def to check if n
|
||||
# is prime or not
|
||||
def isPrime(n) :
|
||||
|
||||
if (n < 2) :
|
||||
return False
|
||||
for i in range(2, n + 1) :
|
||||
if (n % i == 0) :
|
||||
return False
|
||||
i = i * i
|
||||
return True
|
||||
|
||||
def mobius(n) :
|
||||
|
||||
p = 0
|
||||
|
||||
# Handling 2 separately
|
||||
if (n % 2 == 0) :
|
||||
|
||||
n = int(n / 2)
|
||||
p = p + 1
|
||||
|
||||
# If 2^2 also
|
||||
# divides N
|
||||
if (n % 2 == 0) :
|
||||
return 0
|
||||
|
||||
|
||||
# Check for all
|
||||
# other prime factors
|
||||
for i in range(3, int(math.sqrt(n)) + 1) :
|
||||
|
||||
# If i divides n
|
||||
if (n % i == 0) :
|
||||
|
||||
n = int(n / i)
|
||||
p = p + 1
|
||||
|
||||
# If i^2 also
|
||||
# divides N
|
||||
if (n % i == 0) :
|
||||
return 0
|
||||
i = i + 2
|
||||
|
||||
if(p % 2 == 0) :
|
||||
return -1
|
||||
else :
|
||||
return 1
|
||||
|
||||
# Driver Code
|
||||
print("Mobius numbers from 1..99:")
|
||||
|
||||
for i in range(1, 100):
|
||||
print(f"{mobius(i):>4}", end = '')
|
||||
# This code is contributed by
|
||||
# Manish Shaw(manishshaw1)
|
||||
Loading…
Add table
Add a link
Reference in a new issue