def classes(n): """ Returns different classes for a positive integer n. Two integers are in same class only and only if they have same gcd with n. Parameters ---------- n : int denotes positive integer of which class distribution is neede return : dictionary key : int denotes the gcd values value : array denotes the elements belonging to same class """ classes_dict = {} for i in range(1,n+1): if gcd(i,n) in classes_dict: classes_dict[gcd(i,n)].append(i) else: classes_dict[gcd(i,n)] = [i] return classes_dict
def isSol(a, b, m): """ Checks if solution exist for the linear congruence equation ax ≡ b (mod m) Parameters ---------- a : int denotes a in ax ≡ b (mod m) b : int denotes b in ax ≡ b (mod m) m : int denotes m in ax ≡ b (mod m) return : bool returns true if solution exist otherwise false """ return isDivisible(gcd(a, m), b)
def if_infinite_prime_AP(a, d): """ Returns true if there exist infinte number of prime numbers in the arithmetic progression having first term as 'a' and common difference 'd' Parameters ---------- a : int integer denoting first term of AP d : int integer denoting common difference of Ap return : bool return true if there exist infinte primes in AP otherwise false """ if (a != int(a) or d != int(d)): raise ValueError("a and d must be integer") return gcd(a, d) == 1
def is_gauss_sum_separable(n, k): """ Returns true if gauss sum is separble for dirichlet characters mod k calculated for positive integer n Parameters ---------- n : int denotes positive integer for which dirichlet characters are calculated k : int denotes positive intger mod return : bool returns true if gauss sum is separable otherwise false """ if (n != int(n) or k != int(k) or n < 1 or k < 1): raise ValueError("n and k must be positive integers") return gcd(n, k) == 1 #conductor of dirichlet character mod k
def isInteger_sol(a, b, c): """ Checks if integer solution exist for ax + by = c Parameters ---------- a : int denotes a in ax + by = c b : int denotes b in ax + by = c c : int denotes c in ax + by = c return : bool return true if integer solution exist otherwise false """ return isDivisible(gcd(a, b), c) # def integer_sol(a,b,c)
def sol_count(a, b, m): """ Returns number of solutions of ax ≡ b (mod m) that are least residue (mod m) Parameters ---------- a : int denotes a in ax ≡ b (mod m) b : int denotes b in ax ≡ b (mod m) m : int denotes m in ax ≡ b (mod m) """ if (isSol(a, b, m) == False): return 0 elif (isCoprime(a, m)): return 1 else: return gcd(a, m)
def isTerminate(num,den): """ Checks if given fraction's decimal expansion terminates or not Parameters ---------- num : int denotes numerator of fraction den : int denotes denominator of fraction return : bool returns true if decimal expansion terminates otherwise false """ if(num!=int(num) or den!=int(den)): raise ValueError( "num and den are integers" ) if(den == 0): raise ValueError( "den cannot be zero" ) num = abs(num) den = abs(den) if(num == 0): return True GCD = gcd(num,den) num = num/GCD den = den/GCD while(den > 0): if(den%2 == 0): den = den//2 else: break while(den > 0): if(den%5 == 0): den = den//5 else: break return den==1
def find_sol(a, b, m): """ Finds all solutions of equation ax ≡ b (mod m) Parameters ---------- a : int denotes a in ax ≡ b (mod m) b : int denotes b in ax ≡ b (mod m) m : int denotes m in ax ≡ b (mod m) return : array returns an array of integers denoting solutions of ax ≡ b (mod m) """ solutions = [] if (isSol(a, b, m) == False): return solutions elif (sol_count(a, b, m) == 1): for i in range(1, m): if (((a * i) % m) == (b % m)): solutions.append(i) return solutions else: temp_gcd = gcd(a, m) a = a // temp_gcd b = b // temp_gcd m = m // temp_gcd temp_ans = -1 for i in range(1, m): if (((a * i) % m) == (b % m)): temp_ans = i break for i in range(temp_gcd): solutions.append(temp_ans + (i * m)) return solutions
def test_gcd2(): a = 343 b = 280 assert gcd(a, b) == 7
def test_gcd1(): a = 2 b = 100 assert gcd(a, b) == 2