def test_gcd(self): self.assertEqual(fractions.gcd(30, 50), GCD.greatest_common_divisor(30, 50)) self.assertEqual(fractions.gcd(55555, 123450), GCD.greatest_common_divisor(55555, 123450)) self.assertEqual(fractions.gcd(-30, -50), GCD.greatest_common_divisor(-30, -50)) self.assertEqual(fractions.gcd(-1234, 1234), GCD.greatest_common_divisor(-1234, 1234))
def SS_Test(n, k): for i in range(k): a = random.randint(1, n - 1) d = GCD.GCD(a, n) if d > 1: print "Composite" return False t1 = pow(a, (n - 1) / 2, n) t2 = Jacobi.Jacobi(a, n) t2 = t2 % n #print "t1 is", t1, "t2 is", t2 if t1 != t2: print "Composite" return False print "Probably Prime" return
def lehman_simple(n): # Find a nontrivial factor m of n; if n prime then m==n # Pre: n > 1 # Post: return m; m > 1, n % m == 0; m == n only if n is prime n_1_3 = int(ceil(pow(n, 1.0/3.0))) n_1_6 = pow(n, 1.0/6.0) # 1. Trial division ub_d = max(n_1_3, 19) # Ensures n > 21 in step 2. for d in range(2,ub_d+1): if n % d == 0: return d # 2. Loop for k in range(1,n_1_3+1): lb = int(ceil(2*sqrt(k*n))) ub = int(floor(2*sqrt(k*n) + n_1_6/(4*sqrt(k)))) for a in range(lb, ub+1): delta = a*a - 4*k*n b = int(floor(sqrt(delta))) if b*b == delta: return GCD.gcd(a+b, n) return n
def modular_multiplicative_inv(a, m): if m == 1: return 0 if m < 1: raise ValueError('Modulus should be ve+ int > 0') # check for co-prime condition if gcd.greatest_common_divisor(a, m) != 1: raise ValueError('a and m are not co-primes') # Make var "a" positive if it's negative if a < 0: a %= m # Initialise vars m0 = m x0 = 0 x1 = 1 while a > 1: # Calculate quotient q; store m into temp t q = a / m t = m # Calculate m as remainder(a, m); store temp t into a m = a % m a = t # Assign x0 into temp t; Calculate x0 and store temp t into x1 t = x0 x0 = x1 - q * x0 x1 = t # If x1 is negative then add modulus m0 if x1 < 0: x1 += m0 return x1
def gcd(a): gcd = a[0] for x in a: gcd = GCD.gcd(x, gcd) return gcd
import SortLib import HeapSort import GCD t1 = GCD.euclidean(63, 82) print(t1) ''' List = [1,2,55,322,345,45,2,24,47,244,6,7,98,23,545,23,546,57,54,643,6,345,325,2,4,654,23,0] QS = List.copy() HS = List.copy() AHS = List.copy() MS = SortLib.MergeSort(List) SortLib.QuickSort(QS) HeapSort.HeapSort(HS) SortLib.ArrayHeapSort(AHS) print("Original: " + str(List)) print("Merge Sort result: " + str(MS)) print("Quick Sort result: " + str(QS)) print("Heap Sort result: " + str(HS)) print("Arry Heap Sort result: " + str(AHS)) '''
for denom in range(num+1, 100): #print(num, denom) denom_list = [] num_list = [] for i in str(num): num_list.append(int(i)) for i in str(denom): denom_list.append(int(i)) if str(num)[0] in str(denom) and str(num)[0] != '0': denom_list.remove(num_list[0]) num_list.pop(0) if num_list[0] != 0 and denom_list[0] != 0: if num/denom == num_list[0]/denom_list[0]: tot_num *= num tot_denom *= denom count += 1 print(num, denom) elif str(num)[1] in str(denom) and str(num)[1] != '0': denom_list.remove(num_list[1]) num_list.pop(1) if num_list[0] != 0 and denom_list[0] != 0: if num/denom == num_list[0]/denom_list[0]: tot_num *= num tot_denom *= denom print(num, denom) count += 1 print(tot_num, tot_denom) print(GCD.reduce_fraction(tot_num, tot_denom)) print(time.time()-start)
def GetLcm(a, b): return int(a * b / GCD.EuclidGCD(a, b))
def DecimalExpansion(n): for x in range(1, 2000): if((10**x) % n == 1 and GCD.gcd(10, n)): return x return -1
def gcd(self): print("Enter Two Numbers") x = input() y = input() GCD.gcd_alg(x,y)
def init_GCDs(self): self.GCDs = [] # Jolt def _effect_jolt(RDM): RDM.white_mana += 3 RDM.black_mana += 3 self.GCDs.append( GCD.GCD({ "name": "Jolt", "potency": 290, "cast_time": 200, "weave_windows": 2, "mp_cost": 200, "ready": True, "effect": _effect_jolt })) # Verthunder def _effect_verthunder(RDM): RDM.black_mana += 11 if RDM.acceleration > 0: RDM.verfire_ready = True RDM.acceleration -= 1 elif random.random() < 0.5: RDM.verfire_ready = True self.GCDs.append( GCD.GCD({ "name": "Verthunder", "potency": 370, "cast_time": 500, "weave_windows": 2, "mp_cost": 300, "ready": True, "effect": _effect_verthunder })) # Veraero def _effect_veraero(RDM): RDM.white_mana += 11 if RDM.acceleration > 0: RDM.verstone_ready = True RDM.acceleration -= 1 elif random.random() < 0.5: RDM.verstone_ready = True self.GCDs.append( GCD.GCD({ "name": "Veraero", "potency": 370, "cast_time": 500, "weave_windows": 2, "mp_cost": 300, "ready": True, "effect": _effect_veraero })) # Verfire def _effect_verfire(RDM): if RDM.verfire_ready: RDM.black_mana += 9 RDM.verfire_ready = False else: raise NameError('Verfire casted when not ready.\n') self.GCDs.append( GCD.GCD({ "name": "Verfire", "potency": 310, "cast_time": 200, "weave_windows": 2, "mp_cost": 200, "ready": False, "effect": _effect_verfire })) # Verstone def _effect_verstone(RDM): if RDM.verstone_ready: RDM.white_mana += 9 RDM.verstone_ready = False else: raise NameError('Verstone casted when not ready.\n') self.GCDs.append( GCD.GCD({ "name": "Verstone", "potency": 310, "cast_time": 200, "weave_windows": 2, "mp_cost": 200, "ready": False, "effect": _effect_verstone })) # Riposte def _effect_riposte(RDM): RDM.white_mana -= 30 RDM.black_mana -= 30 RDM.combo_location = 1 if RDM.white_mana < 0 or RDM.black_mana < 0: raise NameError( "Enchanted riposte casted without sufficient mana.\n") self.GCDs.append( GCD.GCD({ "name": "Enchanted Riposte", "potency": 220, "cast_time": 0, "weave_windows": 1, "mp_cost": 0, "ready": False, "effect": _effect_riposte })) # Zwerchhau def _effect_zwerchhau(RDM): RDM.white_mana -= 25 RDM.black_mana -= 25 RDM.combo_location = 2 if RDM.white_mana < 0 or RDM.black_mana < 0: raise NameError( "Enchanted zwerchhau casted without sufficient mana.\n") self.GCDs.append( GCD.GCD({ "name": "Enchanted Zwerchhau", "potency": 290, "cast_time": 0, "weave_windows": 1, "mp_cost": 0, "ready": False, "effect": _effect_zwerchhau })) # Redoublement def _effect_redoublement(RDM): RDM.white_mana -= 25 RDM.black_mana -= 25 RDM.combo_location = 3 if RDM.white_mana < 0 or RDM.black_mana < 0: raise NameError( "Enchanted redoublement casted without sufficient mana.\n") self.GCDs.append( GCD.GCD({ "name": "Enchanted Redoublement", "potency": 470, "cast_time": 0, "weave_windows": 2, "mp_cost": 0, "ready": False, "effect": _effect_redoublement })) # Verholy def _effect_verholy(RDM): if RDM.white_mana < RDM.black_mana: RDM.verstone_ready = True else: if random.random() < 0.2: RDM.verstone_ready = True RDM.white_mana += 21 RDM.combo_location = 4 self.GCDs.append( GCD.GCD({ "name": "Verholy", "potency": 600, "cast_time": 0, "weave_windows": 2, "mp_cost": 400, "ready": False, "effect": _effect_verholy })) # Verflare def _effect_verflare(RDM): if RDM.black_mana < RDM.white_mana: RDM.verfire_ready = True else: if random.random() < 0.2: RDM.verfire_ready = True RDM.black_mana += 21 RDM.combo_location = 4 self.GCDs.append( GCD.GCD({ "name": "Verflare", "potency": 600, "cast_time": 0, "weave_windows": 2, "mp_cost": 400, "ready": False, "effect": _effect_verflare })) # Scorch def _effect_scorch(RDM): RDM.white_mana += 7 RDM.black_mana += 7 RDM.combo_location = 0 self.GCDs.append( GCD.GCD({ "name": "Scorch", "potency": 700, "cast_time": 0, "weave_windows": 2, "mp_cost": 400, "ready": False, "effect": _effect_scorch }))