def checkperm(x,y): xa = int2array(x) ya = int2array(y) isperm = True for elem in xa: if elem in ya: ya.pop(ya.index(elem)) else: isperm = False break return isperm
def checkdigits(t): check = 0 ar = int2array(t[0]) br = int2array(t[1]) ar.sort() br.sort() if len(ar) == len(br): for ii in range(len(ar)): if ar[ii] != br[ii]: break if ii == len(ar)-1: check = 1 return check
def replaceprime(prime): findall = lambda my_list: [i for i, x in enumerate(my_list) if x == '1'] ndig = len(int2array(prime)) pgroup = [] replacement_dig = make_palindrome_half(ndig-1) # Ignore the last replacement rule, it is '0000' for reprule in replacement_dig[:-1]: # Run replacement for all integers subgroup = [] newprime = int2array(prime) for cint in range(10): for ind in findall(reprule): newprime[ind] = cint # Check that the length is the same if len(newprime) == len(int2array(array2int(newprime))): subgroup.append(array2int(newprime)) pgroup.append(subgroup) return pgroup
from eulermath import int2array if __name__ = "__main__": # nexp = 15 # Test case, should give 26 nexp = 1000 print "The sum of the digits in 2^", nexp, "is:", sum(int2array(2**nexp))
import math from eulermath import int2array # Applies the factorial to each element in a list def facArray(arr): newarr = [] for i in arr: newarr.append(math.factorial(i)) return newarr if __name__ == "__main__": # Find the upper bound we need to go to for ii in range(2, 10): ninestr = int("9"*ii) ninetotal = sum(facArray(int2array(ninestr))) if (ninetotal < ninestr): print "max bound = ", ninestr nmax = ninestr break totalsum = 0 n = 3 while (n < nmax): intarr = int2array(n) intsum = sum(facArray(intarr)) if intsum == n: print intsum totalsum += intsum n += 1
import math from eulermath import int2array if __name__ == "__main__": # n = 10 # Test case, should give 27 n = 100 # print math.factorial(n) myarray = int2array(math.factorial(n)) sumdigits = 0 # print myarray for x in myarray: sumdigits += x print "The sum of the digits of", (str(n)+"!"), "is:", sumdigits
def truncateRL(num): arr = int2array(num) arr.pop() tnum = array2int(arr) return tnum
# Find the one other sequence of 4-digit permuted increasing primes # The first is; 1487, 4817, 8147 (difference is 3330) from eulermath import (isprime, int2array, array2int, find_primes_in_list, find_arithemetric_series) from itertools import permutations, combinations if __name__ == "__main__": # Start by finding all 4-digit primes print "Finding arithemetric series of primes between 1001 and 9999:" exclude = [] for trial in xrange(1001, 9999, 2): if trial not in exclude: if isprime(trial): # Find the permutations of this number tp = permutations(int2array(trial)) # Get the unique values and sort them tpl = list(set([array2int(x) for x in tp])) tpp = find_primes_in_list(tpl) tpn = [] for x in tpp: if x > 1000: tpn.append(x) # Exclude the others for p in tpn: exclude.append(p) # Find any arithemetric series of length 3 in the prime list aseries = find_arithemetric_series(tpn, 3) if len(aseries) > 0: print aseries for series in aseries: print series
def nextterm(n): return sum(map(factorial, int2array(n)))
tsublist.append(slist[ii]) trimmed.append(tsublist) return trimmed if __name__ == "__main__": # Import the data sub = [] p123 = [] p12 = [] p23 = [] p1 = [] p2 = [] p3 = [] fnames = open('Euler79.dat') for line in fnames: val = int2array(int(line)) # All values sub.append(val) # Single values p1.append(val[0]) p2.append(val[1]) p3.append(val[2]) # Pairs p12.append(array2int(val[0:2])) p23.append(array2int(val[1:3])) p123.append(array2int(val)) # Flatten out the sublist and take all unique elements fsl = list(set([x for arr in sub for x in arr])) # this length is the shortest password size
def haslongnum(frac): ln = 0 if len(int2array(frac.numerator)) > len(int2array(frac.denominator)): ln = 1 return ln
# Applies the power operator to each element in a list def powArray(arr, mypow): newarr = [] for i in arr: newarr.append(i**mypow) return newarr if __name__ == "__main__": # npow = 4 #Test case, should give 19316 = 1634 + 8208 + 9474 npow = 5 # nmax = 10**6 # Old upper bound, set arbitrarially # Find the upper bound we need to go to for ii in range(2, 10): ninestr = int("9"*ii) ninetotal = sum(powArray(int2array(ninestr), npow)) if (ninetotal < ninestr): print "max bound = ", ninestr nmax = ninestr break totalsum = 0 n = 2 while (n < nmax): intarr = int2array(n) intsum = sum(powArray(intarr, npow)) if intsum == n: print intsum totalsum += intsum n += 1
from eulermath import int2array if __name__ == "__main__": # ndigmax = 3 # Test case, should give the 12th term, 144 ndigmax = 1000 ndigs = 1 a = 1 b = 1 n = 2 # Current term number while ndigs < ndigmax: nb = b + a a = b b = nb n += 1 ndigs = len(int2array(b)) # print n, b print "The first term to have over", ndigmax, "digits is the", n, "th term = ", b
# Find the maximum digit sum of a^b, where a,b <= 100 from eulermath import int2array if __name__ == "__main__": nmax = 100 smax = 0 for a in range(1, nmax+1): for b in range(1, nmax+1): p = a**b s = sum(int2array(p)) # print "%d^%d = %d, digit sum = %d" % (a,b,p,s) if s > smax: smax = s # print "found a larger sum" print "%d/%d complete" % (a, nmax) print "The maximum digit sum is: %d" % (smax)
# Find the sum of the digits in the numerator # for the 100th convergent of the continued # fraction for e from fractions import Fraction from eulermath import int2array, frac2str, makecontfrac def convterms_e(n): enot = [0]*n for x in range(n): if x == 0: enot[x] = 2 elif (x+2) % 3 == 1: enot[x] = 2*((x+2)/3) else: enot[x] = 1 return enot if __name__ == "__main__": print convterms_e(10) print makecontfrac(convterms_e(10)) t100 = makecontfrac(convterms_e(100)) snd = sum(int2array(t100.numerator)) print ("The sum of the digits in the numerator of the " + "100th convergent of e is: %d" % (snd))