def permute(digits, index): if len(digits) == 0: return [] base = euler.factorial(len(digits) - 1) here = digits.pop(index / base) rest = permute(digits, index % base) answer = [here] answer.extend(rest) return answer
def permute(digits, index): if len(digits)==0: return [] base = euler.factorial( len(digits) -1) here = digits.pop(index/base) rest = permute(digits, index % base) answer = [here] answer.extend(rest) return answer
def permutation(orig_nums, n): nums = list(orig_nums) perm = [] while len(nums): divider = factorial(len(nums) - 1) pos = n / divider n = n % divider perm.append(nums[pos]) print divider, pos, n nums = nums[0:pos] + nums[pos + 1 :] return perm
import euler print sum( map( int, list( str( euler.factorial(100) ) ) ) )
def nCk(n, k): return factorial(n) // (factorial(k) * factorial(n-k))
#!/usr/bin/python # coding: UTF-8 """ @author: CaiKnife Factorial digit sum Problem 20 n! means n (n 1) ... 3 2 1 For example, 10! = 10 9 ... 3 2 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! """ from euler import factorial print sum([int(x) for x in str(factorial(100))])
def is_curious(n): return sum([factorial(x) for x in get_digits(n)]) == n
def testFactorial(self): for i in range(1,20): a = euler.naive_factorial(i) b = euler.factorial(i) print a,b self.assert_( a == b)
#!/usr/bin/env python # Copyright (c) 2008 by Steingrim Dovland <*****@*****.**> from euler import factorial # n! means n * (n - 1) * ... * 3 * 2 * 1 # # Find the sum of the digits in the number 100! print sum(map(int, str(factorial(100))))
import sys sys.path.append("..") from euler import factorial_memory as factorial result = 0 memory = {0: 1, 1: 1} for i in range(3, 1000000): digits = [int(x) for x in str(i)] factorials = [factorial(x, memory) for x in digits] if sum(factorials) == i: result += i print(result) """ Runtime without memory: 6.095s Runtime with memory: 2.910s Reducing search space from 1,000,000 to 100,000 reduces time to: 0.292s """
import euler f = [euler.factorial(i) for i in range(10)] def factorialChainNext(n): seq = euler.intToSeq(n) seq = [f[i] for i in seq] return sum(seq) def factorialChain(n): lst = [] while n not in lst: lst.append(n) n = factorialChainNext(n) assert (len(lst) <= 60) lst.append(n) return lst def factorialChainLength(n): return len(factorialChain(n)) - 1 def main(): cnt = 0 for i in range(3000): fc = factorialChain(i) fcl = len(fc) - 1 if fcl == 60:
import euler f = [ euler.factorial(i) for i in range(10) ] def factorialChainNext(n): seq = euler.intToSeq(n) seq = [ f[i] for i in seq ] return sum(seq) def factorialChain(n): lst = [] while n not in lst: lst.append(n) n = factorialChainNext(n) assert(len(lst)<=60) lst.append(n) return lst def factorialChainLength(n): return len(factorialChain(n))-1 def main(): cnt = 0 for i in range(3000): fc = factorialChain(i) fcl = len(fc)-1 if fcl==60: cnt +=1 print i,fcl,cnt if i % 10000 == 0: print '#',i
import euler from numba import jit @jit def main(memoize): i = 0 for i in range(3000000): s = 0 for j in list(str(i)): s += memoize[int(j)] if s==i: print s memoize = dict(zip([i for i in range(0,10)],[euler.factorial(int(i))for i in range(0,10)])) main(memoize)
def ncr(n, r): nfac, rfac, nrfac = factorial(n), factorial(r), factorial(n - r) return nfac / (rfac * nrfac)
def expfact(x): return tuple([ factorial(int(c)) for c in str(x) ])
import sys sys.path.append("..") from euler import factorial f = factorial(100) digits = [int(i) for i in str(f)] result = sum(digits) print(result)
""" @author: CaiKnife Combinatoric selections Problem 53 There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 In combinatorics, we use the notation, 5C3 = 10. In general, nCr = n! r!(nr)! ,where r n, n! = n(n1)...321, and 0! = 1. It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066. How many, not necessarily distinct, values of nCr, for 1 n 100, are greater than one-million? """ from euler import factorial count = 0 for n in xrange(23, 101): for r in range(1, n): if factorial(n) / factorial(r) / factorial(n - r) > 1000000: count += 1 print count
from euler import digits, factorial print sum(digits(factorial(100)))
#!/usr/bin/python import euler # I did a quick search to find some information about this and found # http://en.wikipedia.org/wiki/Catalan_number, which is close to what I wanted # it describes the number of paths without crossing the diagonal. I then # searched for the sequence: 1, 2, 6 on # http://www.research.att.com/~njas/sequences # and found the "Central binomial coefficients" C(2n,n) = (2n)!/(n!)^2 # # One description jumped out at me: # The number of direct routes from my home to Granny's when Granny # lives n blocks south and n blocks east of my home in Grid City. To # obtain a direct route, from the 2n blocks, choose n blocks on which one # travels south. # Which is what I'm looking for. # n = 20 paths = euler.factorial(2 * n) / (euler.factorial(n) ** 2) print "monotonic paths on",n,"x",n,"grid",paths
#!/usr/bin/python import euler nfact = str(euler.factorial(100)) sum = 0 for c in nfact: sum += int(c) print "100! digit sum =",sum
def digit_factorial_sum(n): ds = digits(n) return sum([ factorial(d) for d in ds ])
import euler factorial = euler.factorial(100) print sum(map(int, str(factorial)))
def digit_factorial_sum(n): ds = digits(n) return sum([factorial(d) for d in ds])