Created
May 29, 2018 16:56
-
-
Save brunogfranca/047762e9486d90558ca2f8888660163f to your computer and use it in GitHub Desktop.
Versão teste primos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prime_factors(val): | |
x = 2 | |
factors = [] | |
while x*x <= val: | |
if val%x: | |
x += 1 | |
else: | |
val //= x | |
factors.append(x) | |
if val > 1: | |
factors.append(val) | |
return factors | |
def prime_reduction_calc(number): | |
reductions = 1 | |
n = number | |
while True: | |
factors = list(prime_factors(n)) | |
if len(factors) == 1: | |
return factors[0], reductions | |
break | |
if reductions > 1 and n == number: | |
return None | |
reductions += 1 | |
n = sum(factors) | |
def prime_reduction(number): | |
row = prime_reduction_calc(number) | |
if not row: | |
return | |
x, y = row | |
# print('{} {}'.format(x, y)) | |
# if __name__ == '__main__': | |
# numers = (2, 3, 5, 76, 100, 2001, 4) | |
# for number in numers: | |
# prime_reduction(number) | |
if __name__ == '__main__': | |
def run(): | |
for number in range(2, 100000): | |
if number==4: | |
continue | |
prime_reduction(number) | |
import cProfile, pstats, io | |
pr = cProfile.Profile() | |
pr.enable() | |
run() | |
pr.disable() | |
s = io.StringIO() | |
sortby = 'cumulative' | |
ps = pstats.Stats(pr, stream=s).sort_stats(sortby) | |
ps.print_stats() | |
print(s.getvalue()) | |
# import sys | |
# for line in sys.stdin: | |
# number = int(line) | |
# prime_reduction(number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment