Comparing two numbers written in index form like 211 and 37 is not difficult, as any calculator would confirm that 211 = 2048 < 37 = 2187. However, confirming that 632382518061 > 519432525806 would be much more difficult, as both numbers contain over three million digits. Using base_exp.txt (right click and 'Save Link/Target As...'), a 22K text file containing one thousand lines with a base/exponent pair on each line, determine which line number has the greatest numerical value. NOTE: The first two lines in the file represent the numbers in the example given above. ''' import Functions import time import math start = time.clock() best_line = 1 current_line = 1 best_num = 0 ls = Functions.import_csv('base_exp.txt') for pair in ls: print('Current line under test...',current_line) test = int(pair[1]) * math.log(int(pair[0])) #instead of compare actual base**exp, compare exponent * log(base) if test > best_num: best_num = test best_line = current_line current_line += 1 print('The line with the largest number is ', best_line) Functions.runtime(start)
The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? ''' # This program uses brute force solution, and loads a csv of all primes < 1e6. # Answer in approx 16s. Not best solution. import Functions import time global primes_ls primes_ls = Functions.import_csv('primes_1e6.csv') primes_ls = list(map(int, primes_ls)) # Need to add list on outside, Python 3 def sum_of_primes(num_terms, start): global primes_ls ans = primes_ls[start] for i in range(1, num_terms): ans += primes_ls[i] return ans start_time = time.clock() prime_sum = 0 start_index = primes_ls.index(953)
return False start = time.clock() x1 = 0 x2 = 0 x3 = 0 y1 = 0 y2 = 0 y3 = 0 ''' Need to determine if origin is in triangle. If this is the case, then the y intercept of 2 of the lines which enclose the triangle will be within the upper and lower y bounds formed by the points at the vertices. ''' ls = Functions.import_csv('triangles.txt') enc_origin = [] num = 0 for coords in ls: x1 = int(coords[0]) y1 = int(coords[1]) x2 = int(coords[2]) y2 = int(coords[3]) x3 = int(coords[4]) y3 = int(coords[5]) #print('current coords under test...',coords) test = triangle(x1,y1,x2,y2,x3,y3) if test == True: num += 1