def step(self): ''' Computes only 1 iteration Adds logs at each step ''' if not self.step_by_step and not self.is_over: self.step_by_step = True self.start_time = time.time() if self.it and not self.is_over: offset = 10 self.log("Iteration %d" % (self.cpt), offset) self.cpt += 1 self.it -=1 n = self.n b = self.b self.a = randint(1, n) self.log("Random A between 1 and %d: A = %d" % (self.n, self.a), offset * 2) b_primes = primes(b) self.log("B = %d, computing B- primes: %s" % (self.b, b_primes), offset * 2) if len(b_primes) > 0: self.log("Start for loop", offset * 2) for q in b_primes : self.log("Q = %d" %(q), offset * 3) e = abs((log(b)) / (log(q))) self.log("Ln(b) / Ln(q): E = %d" % (e), offset * 3) self.a = pow(self.a, int(pow(q, e)), n) self.log("Modular exponentiation A ^ Q ^ E %% N: A = %d" % (self.a), offset * 3) self.g = gcd(self.a - 1, n) self.log("Greatest Common Divisor between %d and %d: G = %d" % (self.a - 1, self.n, self.g), offset * 2) if 1 < self.g < n: self.is_over = True self.step_by_step = False self.log("Successful factorization: %d" % (self.g)) self.log("Duration: %fs" % (time.time() - self.start_time)) if self.g == 1 : self.b += 1 self.log("G = 1 => Incrementing B and changing A", offset * 2) elif not self.it and not self.is_over: self.is_over = True self.step_by_step = False self.log("End of iterations, factorization failed") self.log("Duration: %fs" % (time.time() - self.start_time))
def test_PrimeCount(self): """ Testing the number of 100- primes (25) then 1000- primes (168) """ self.assertEqual(len(primes(100)), 25) self.assertEqual(len(primes(1000)), 168) # 0.025s à l'exécution....