''' n! means n x (n - 1) x ... x 3 x 2 x 1 For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! ''' import math def main(): # Find the sum of the digits in the number 100! (100 factorial) n = 100 x = list(str(math.factorial(n))) # calc -> str -> array of chars return sum([int(i) for i in x]) # chars -> ints -> sum it! if __name__ == '__main__': import boilerplate, time, resource t = time.time() r = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss boilerplate.all(main(), t, r)
def main(): # same as p18... but with a much larger test set lines = open('p067.in').read().split('\n') #file -> list arr = [] for i in lines: temp = i.split(' ') arr.append( [int(x) for x in temp] ) # recursively sum through all possible paths, keeping the largest sum def add_next(my_sum,arr,row,idx,biggest_sum): my_sum += arr[row][idx] row += 1 if row<len(arr): if (idx - 1 >= 0): biggest_sum = add_next(my_sum,arr,row,idx-1,biggest_sum) # idx-1 if (idx + 1 <= len(arr[row])): biggest_sum = add_next(my_sum,arr,row,idx+1,biggest_sum) # idx+1 biggest_sum = add_next(my_sum,arr,row,idx,biggest_sum) # same idx biggest_sum = max(my_sum, biggest_sum) return biggest_sum return add_next(0,arr,0,0,0) if __name__ == '__main__': import boilerplate, time boilerplate.all(time.time(),main()) # about 1 second... not too bad
''' for i in xrange(1,2000): n = count_rectangles((i,1)) if fabs(n-target) < min_diff: min_diff = fabs(n-target) min_diff_d = (i,1) print min_diff, min_diff_d ''' return min_diff_d[0]*min_diff_d[1] def how_many_fit(s,l): #small, large assert(s[0] <= l[0] and s[1] <= l[1]) ret = 0 for i in xrange(l[0]-s[0]+1): for j in xrange(l[1]-s[1]+1): ret += 1 return ret def count_rectangles(l): total = 0 for i in xrange(1,l[0]+1): for j in xrange(1,l[1]+1): ans = how_many_fit((i,j),l) total += ans return total if __name__ == '__main__': import boilerplate, time boilerplate.all(time.time(),main())
while tmp > 0: result += f[tmp%10] tmp = tmp/10 return result d = {} def chain_length(n): ret_length = 0 terms = set([]) start_n = n while n not in terms: if n in d: ret_length += d[n] break else: terms.add(n) ret_length += 1 n = dig_fac_sum(n) d[start_n] = ret_length return ret_length if __name__ == '__main__': import boilerplate, time boilerplate.all(time.time(),main(1e6)) # 2 seconds with caching... not great but it'll have to do for now
What is the 10 001st prime number? """ import math def main(n): nth_prime = 0 i = 1 while nth_prime < n: if is_prime(i): nth_prime += 1 i += 2 return i - 2 def is_prime(n): for i in xrange(2, int(math.sqrt(n) + 1)): if n % i == 0: return False return True if __name__ == "__main__": import boilerplate, time, resource t = time.time() r = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss boilerplate.all(main(10001), t, r)
if n%i == 0: return False return True # returns reduced fraction if possible, otherwise it returns False def reduce_f(x,y): for i in xrange(int(y/2),2,-1): if x%i == 0: if y%i ==0: return x/i, y/i return False def main(): # the next 3 lines run the old method #x = rpf_set(1000) # all the reduced fractions... 8 -> 1e6 for final answer #y = ordered(x) # order them... now we have a list of tuples s.t. (real,(num,den)) #return left_of(y,(3,7)) # which one comes right before 3/7 ? # the rest of these lines run the new 'method' ... it's just some basic math m = int(1000000/float(7)) #multiplier ret = reduce_f(m*3, m*7+1) if (ret): return ret[0] else: return m*3 # just return n if __name__ == '__main__': import boilerplate, time boilerplate.all(time.time(),main()) # works but too slow as usual. I can do 1000... but not 1 000 000
def main(file_name): lines = open(file_name).read().split('\n') arr = [] for i in lines: temp = i.split(',') arr.append( [int(x) for x in temp] ) for i in xrange(len(arr)): for j in xrange(len(arr[0])): if i*j > 0: arr[i][j] += min(arr[i-1][j], arr[i][j-1]) elif i: arr[i][j] += arr[i-1][j] elif j: arr[i][j] += arr[i][j-1] return arr[-1][-1] if __name__ == '__main__': import boilerplate, time boilerplate.all(time.time(),main('p081.in'))
import math def factors(n): return reduce(list.__add__, ([i, n // i] for i in range(1, int(math.sqrt(n)) + 1) if n % i == 0)) def is_prime(n): for i in xrange(2, int(math.sqrt(n)) + 1): if n % i == 0: return False return True def main(n): factor_list = factors(n) factor_list.sort(reverse=True) for i in factor_list: if is_prime(i): return i return -1 if __name__ == "__main__": import boilerplate, time, resource t = time.time() r = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss boilerplate.all(main(600851475143), t, r)
if is_prime(i): results.add(i) if is_prime(int(n/i)): results.add(int(n/i)) return len(results) def main(): # find sequence n = 4 seq_count = 0 i = 210 while (True): i += 1 if count_prime_factors(i) == n: seq_count += 1 else: seq_count = 0 if seq_count == n: return i-n+1 break if __name__ == '__main__': import boilerplate, time, resource t = time.time() r = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss boilerplate.all(main(), t, r) # this feels slow... but maybe this is the best I can do for now?
def main(_in): #number spiral diagonals _max = _in+_in # calculating number of elements from dimensions l = [1] # list of the diagonal elements i = l[0] # starting point add_amount = 0 n = 1 # number of elements so far while (True): add_amount += 2 cycle_counter = 4 # each cycle adds four new elements while (cycle_counter > 0): n += 1 if n>=_max: return sum(l) # if the last element gets us to the end... return! i += add_amount l.append(i) cycle_counter -= 1 if __name__ == '__main__': import boilerplate, time boilerplate.all(time.time(),main(1001))