Beispiel #1
0
def phi_method_2(num):
    factors = factorize(num)
    phi_count = 1
    for factor in factors:
        phi_count = phi_count * (factor - 1)

    return phi_count
Beispiel #2
0
def check3(n):
    # factors = factorize(n)
    p = max(factorize(n))
    # print(n,p)
    x = n - p
    while x > sqrt(n) - 1:
        if x % p == 0:
            if pow(x, 2, n) == x:
                # print(x,n)
                return x
        if (x + 1) % p == 0:
            if pow(x + 1, 2, n) == x:
                # print(x+1,n)
                return x + 1
        x -= p
    return 1
 def test_factors_of_one(self):
     self.assertEqual(factorize(1), [])
 def test_factors_of_zero(self):
     self.assertEqual(factorize(0), [])
 def test_factors_of_prime_int_squared(self):
     self.assertEqual(factorize(281476419553081), [16777259, 16777259])
 def test_factors_of_prime(self):
     self.assertEqual(factorize(13713779), [13713779])
 def test_factors_maxint64(self):
     self.assertEqual(factorize(18446744073709551615),
                      [3, 5, 17, 257, 641, 65537, 6700417])
 def test_factors_of_large_int(self):
     self.assertEqual(factorize(246819835539726757),
                      [7, 11, 37, 131, 557, 18379, 64601])
 def test_factors_of_medium_int(self):
     self.assertEqual(factorize(3825476), [2, 2, 17, 101, 557])
 def test_repeated_factors(self):
     self.assertEqual(factorize(71672), [2, 2, 2, 17, 17, 31])
 def test_factors_of_small_int(self):
     self.assertEqual(factorize(15), [3, 5])
 def test_factors_of_two(self):
     self.assertEqual(factorize(2), [2])
Beispiel #13
0
def phi_method_2(num):
    factors = factorize(num)
    phi_count = 1
    for factor in factors:
        phi_count = phi_count * (factor - 1)

    return phi_count


#==============================================================================

# check both methods agree

print('Calculating...')
all_agreed = True
for i in range(9999):

    # if has 2 factors
    if len(factorize(i)) == 2:
        # if two factors are not same
        if factorize(i)[0] != factorize(i)[1]:
            # if methods disagree
            if phi_method_1(i) != phi_method_2(i):
                print(
                    f'{i} - method 1: {phi_method_1(i)} - method 2: {phi_method_2(i)}'
                )
                all_agreed = False

if all_agreed:
    print('Both methods agreed :)')