# print('{0} has {1} elements.'.format(greatest,num)) def main(limit): chainLength = [1] longest = 0 gen = 0 for i in range(2,limit+1): chain = 0 while (i not in range(1,len(chainLength)+1)): if i % 2 == 0: i = int(i/2) chain += 1 else: i = (3*i + 1) chain += 1 chain += chainLength[i-1] chainLength.append(chain) for i in range(0,len(chainLength)): if (chainLength[i] > longest): longest = chainLength[i] gen = i+1 return str(gen) + " generates a chain of length " + str(longest) # print(main(1000000), analytics.lap(), analytics.maxMem()) print(Main2(MAX), analytics.lap(), analytics.maxMem())
multiply by #14 and divide by #1 and then you don’t need to remultiply all the numbers every time. you’re doing 2 operations instead of 12. store the largest one so far in a variable, and compare new one to that and replace if new one is larger""" def string_slicer(string, n): Max_prod = 1 Max_prod_str = '' i = 0 while i < (len(string) - n): sliding_window = string[i:i + n] # window = str(string[i + start:(i+start) +n]) # print("window", sliding_window) window_prod = 1 if "0" not in sliding_window: for j in sliding_window: window_prod *= int(j) if window_prod >= Max_prod: Max_prod = window_prod Max_prod_str = sliding_window i += 1 return Max_prod # print(product_getter_5000(l)) # print(product_getter_5000(string_slice(number, 4))) # print(product_getter_5000(string_slice(number, 13))) # print(string_slicer(number, 13)) print(string_slicer(number, 13), analytics.lap(), analytics.maxMem()) print(product_getter_5000(string_slice(number, 13)), analytics.lap(), analytics.maxMem())
Diag = [] for diag in list_of_lists: if len(diag) > 3: i = 0 while i < (len(diag) - n): sliding_window = diag[i:i + n] window_prod = 1 if "00" not in sliding_window: for j in sliding_window: window_prod *= int(j) if window_prod >= prod: prod = window_prod prod_numbs = sliding_window Diag = diag[:] i += 1 return prod, prod_numbs, Diag def main(matrix, n): A = matrix window = n lists = [] prod_r, prod_num_r, row = row_checker(rows(A), window) prod_c, prod_num_c, col = col_checker(cols(A), window) prod_d, prod_num_d, diag = diags_checker(diags(A), window) return max(prod_r, prod_c, prod_d) print(main(A, 4), analytics.lap(), analytics.maxMem())
# filename = "primes" test = 600851475143 test1 = 83 # print(len(prime_tests.primes)) # The prime factors of 13195 are 5, 7, 13 and 29. # What is the largest prime factor of the number 600851475143 ? factor_list = [] def main(n): pf = set([]) while n%2==0: pf.add(2) n //= 2 for i in range(3,int(n**(.5)),2): while n % i == 0: pf.add(i) n //= i if n > 1: pf.add(n) LPF = sorted(list(pf)) print(LPF) if LPF == n: return LPF print(main(test), analytics.lap(), analytics.maxMem())