def backsolveFnDigSum(n): numSum = 0 for i in xrange(9, 0, -1): numOccur = n / fa(i) numSum += i * numOccur n = n % fa(i) return numSum
def backsolveFnDigSum(n): numSum = 0 for i in xrange(9,0,-1): numOccur = n/fa(i) numSum += i * numOccur n = n % fa(i) return numSum
def pw_combinations(n=8, min=5, crazy=False): count = 0 #For the crazy case, the number of special characters can be greater than one. if crazy == True: for k in range(min, n + 1): #j is number of numerical characters (j>=2): for j in range(2, k): #s is number of special characters (s>=1): for s in range(1, k): #l is the number of lowercase characters (l>=1): for l in range(1, k): #u is the number of uppercase characters (u>=1): for u in range(1, k): #We require the sum to be k for counting purposes: if j + s + l + u == k: #We apply combinatorial arguments for each distinct possible case, #using factorials to count distinct orders of character classes. count += (10**j) * (32**s) * (26**(l + u)) * ( fa(k)) / (fa(j) * fa(s) * fa(l) * fa(u)) return count #For the false case, we simply require s=1: elif crazy == False: for k in range(min, n + 1): #j is number of numerical characters (j>=2): for j in range(2, k): #l is the number of lowercase characters (l>=1): for l in range(1, k): #u is the number of uppercase characters (u>=1): for u in range(1, k): #We require the sum to be k for counting purposes: if j + 1 + l + u == k: #Same as above with s=1: count += (10**j) * 32 * (26**(l + u)) * (fa(k)) / ( fa(j) * fa(l) * fa(u)) return count
def countDie(minNum, dieNum, sumz, prod, numInRow): if minNum > maxDieNum or sumz > total: return 0 if dieNum == numDie: if sumz == total: return fa(numDie) / prod / fa(numInRow) else: return 0 if dieNum >= numDie - topDice: a = countDie(minNum, dieNum + 1, sumz + minNum, prod, numInRow + 1) else: a = countDie(minNum, dieNum + 1, sumz, prod, numInRow + 1) b = countDie(minNum + 1, dieNum, sumz, prod * fa(numInRow), 0) return a + b
def countAllLenPerm(wordDict, maxLen): counts = [0] * (max(wordDict.values())+1) x = symbols('x') multiple = 1 val = 1 for value in wordDict.values(): counts[value] += 1 for i in xrange(1,len(counts)): val += x**i / fa(i) multiple *= val**counts[i] coeff = Poly(expand(multiple)).all_coeffs()[::-1] return sum(coeff[x] * fa(x) for x in xrange(0,maxLen+1))
def recurse_count(depth, min_num, size, DEPTH, num_count, prod ): #min num has to be at least 2. if size < min_num**depth: return 0 if depth == 1: p = perm(SIZE,DEPTH) n = ((size-min_num)*p)/ (prod * fa(num_count)) n += p/(prod * fa(num_count+1)) return n sumz = 0 sumz += recurse_count(depth-1, min_num, size/min_num, DEPTH, num_count+1, prod) for i in xrange(min_num+1, size/min_num+1): if i > size/i: break sumz += recurse_count(depth-1,i, size/i, DEPTH, 1, prod * fa(num_count)) return sumz
def getTotalCount(a,b,c,d,e): #using i,a,b,c,d,e so it's not long var names if (a,b,c,d,e) in possCounts: return possCounts[(a,b,c,d,e)] if a > numRight or c < 0 or d < 0: return 0 if a == numRight and c == 0 and b+d == numWrong: possCounts[(a,b,c,d,e)] = fa(d+e) return fa(d+e) count = 0 """place prime in right spot""" count += getTotalCount(a+1,b,c-1,d,e) """place prime which could be right in wrong spot""" count += (c-1) * getTotalCount(a,b+1,c-2,d+1,e) """place prime which is already wrong in wrong spot""" count += d * getTotalCount(a,b+1,c-1,d,e) """place a composite in""" count += e * getTotalCount(a,b,c-1,d+1,e-1) possCounts[(a,b,c,d,e)] = count return count
def recurse_count(depth, min_num, size, DEPTH, num_count, prod): #min num has to be at least 2. if size < min_num**depth: return 0 if depth == 1: p = perm(SIZE, DEPTH) n = ((size - min_num) * p) / (prod * fa(num_count)) n += p / (prod * fa(num_count + 1)) return n sumz = 0 sumz += recurse_count(depth - 1, min_num, size / min_num, DEPTH, num_count + 1, prod) for i in xrange(min_num + 1, size / min_num + 1): if i > size / i: break sumz += recurse_count(depth - 1, i, size / i, DEPTH, 1, prod * fa(num_count)) return sumz
def main(): low = 100000 r = range(low,100*low) facts = {str(x):fa(x) for x in range(10)} count = 0 res = [145, 40585] for i in r: st = str(i) s = sum([facts[x] for x in st]) if s == i: count+=1 res.append(i) print(res) print(sum(res))
def factorial(x): """ return each element's factorial of x x: 1-dimension ndarray math.factorial() will check the input """ try: from math import factorial as fa except: raise OpException('can not import math.factorial() in function factorial()!') res = np.zeros_like(x) * np.nan for i in range(len(x)): res[i] = fa(x[i]) return res
def factorial(x): """ [Definition] 对x做阶乘,return each element's factorial of x,x: 1-dimension ndarray [Category] 数论 """ try: from math import factorial as fa except: raise Exception( 'can not import math.factorial() in function factorial()!') res = np.zeros_like(x) * np.nan for i in range(len(x)): res[i] = fa(x[i]) return res
def f(n): return sum([fa(int(i)) for i in str(n)])
def ncr(n,r): return fa(n)/(fa(r) * fa(n-r))
from math import factorial as fa th = [4,3,3,2,2,2,2,1,1,1,1,1,0,0,0,0,0] tw = [0,1,0,3,2,1,0,4,3,2,1,0,6,5,4,3,2] on = [0,1,3,0,2,4,6,1,3,5,7,9,0,2,4,6,8] print len(th) sumz = 0 for i in range(0,len(th)): a = 10-on[i]-tw[i]-th[i] #numbers untouched by removal b =(6**a) *(2**on[i]) #bottom of 18!/(3!^a*2^ones[i]) c= fa(th[i]) * fa(tw[i]) *fa(on[i]) #part for 10C(a,b,c) d = fa(10)/(c* fa(a)) # 10C(a,b,c) e = (fa(18)*d)/b print a,b,c,d, e sumz += e #this part is to account for no leading zeroes print sumz *9 /10 #137225088000 #wrong answers: #20748433305600000 #227372725275696000 #right answer: #227485267000992000 #note: took me a while to get this one since i thought there were 16 partitions of 12 instead of 17 (with each number <= 3 and no more than 10 numbers) #OH! Okay, I got it. The way I did this was by listing out all possible combinations of 3a+2b+c = 12...
from math import factorial as fa n = int(input("n: ")) r = int(input("r: ")) print(fa(n) // fa(n - r))
# Printing a Pascal triangle upto n rows from math import factorial as fa n = int(input()) for i in range(n): for j in range(i+1): ans = (fa(i) / (fa(j)*fa(i-j))) print(int(ans),end=" ") print("\n")
def ncr(n, r): return fa(n) / (fa(r) * fa(n - r))
lst=[1,2,3,4] print("%d %d %d %d"%(lst[0],lst[1],lst[2],lst[3])) # In[15]: from math import floor as f1 f1(123.456) # In[16]: from math import factorial as fa fa(5) # In[19]: import random def generateRandomnumbers(n,lb,ub): for i in range(0,n): print(random.randint(lb,ub),end=" ") return generateRandomnumbers(10,100,1000000) # In[21]:
def P(n, m): # n!/(n-m)! if n < m: return -1 return fa(n)/fa(n-m)
numer = functools.reduce(op.mul, range(n, n - r, -1)) denom = functools.reduce(op.mul, range(1, r + 1)) return (numer // denom) alku = time.time() print("Aloitus") n = 12345 # Laudan sivun pituus. Lauta = n*n npow = n**2 summa = 0 valisumma = 0 #print("Checkpoint 1") valisumma = 4 * ((fa(n)) / (fa(n - 3) * fa(3))) #print("Checkpoint 2") summa += (2 * n + 2) * ((fa(n)) / (fa(n - 2) * fa(2))) #print("Checkpoint 3") summa += valisumma #print("Checkpoint 4") kaikki = nCr(npow, 2) #print("Checkpoint 5") print("") print("Mahdollisia sijoituksia:") print("Hyokkayksia: ", int(summa)) print("Kaikki tavat: ", int(kaikki)) tulos = kaikki - summa print("Aika: ", time.time() - alku)
power = n dig_mod2 = [] for i in range(0, len(power_vals)): dig_mod2.append(power % 2) power /= 2 answer = [[1, 0], [0, 1]] for i in range(0, len(power_vals)): if dig_mod2[i] == 1: answer = mmultiply(answer, power_vals[i]) return answer[0][1] SIZE = 100 n = 10**15 MOD = fa(15) sumz = 0 for x in range(0, SIZE + 1): mod = MOD * (x**2 + x - 1) num = (-x + pow(x, n + 1, mod) * fibo(n + 1, mod) + pow(x, n + 2, mod) * fibo(n, mod)) % (mod) denom = (x**2 + x - 1) if gcd(denom, fa(15)) == 1: sumz += num * ext_gcd(denom, fa(15))[0] elif num % denom == 0: sumz += num / denom else: num /= gcd(denom, fa(15)) denom /= gcd(denom, fa(15)) denom = ext_gcd(denom, fa(15))[0] sumz += (num * denom) % MOD
def C(n, m): # n!/[(n-m)!m!] if n < m: return -1 return fa(n)/(fa(n-m)*fa(m))
def bcoef(n, k): return fa(n) / fa(k) / fa(n - k)
def fact_sumz(n): if n == 0: return 0 #base case return fa(n%10) + fact_sumz(n/10)
def fact_sumz(n): if n == 0: return 0 #base case return fa(n % 10) + fact_sumz(n / 10)
def f(n): return sum([fa(int(i)) for i in str(n)])
power = n dig_mod2 = [] for i in range(0,len(power_vals)): dig_mod2.append(power%2) power /= 2 answer = [[1,0],[0,1]] for i in range(0,len(power_vals)): if dig_mod2[i] == 1: answer = mmultiply(answer, power_vals[i]) return answer[0][1] SIZE = 100 n = 10**15 MOD = fa(15) sumz = 0 for x in range(0,SIZE+1): mod = MOD * (x**2+x-1) num = (-x + pow(x,n+1,mod) * fibo(n+1,mod) + pow(x,n+2, mod) * fibo(n,mod)) % (mod) denom = (x**2 + x - 1) if gcd(denom,fa(15)) == 1: sumz += num * ext_gcd(denom,fa(15))[0] elif num % denom == 0: sumz += num /denom else: num /= gcd(denom, fa(15)) denom /= gcd(denom,fa(15)) denom = ext_gcd(denom, fa(15))[0] sumz += (num * denom) % MOD
if a == numRight and c == 0 and b+d == numWrong: possCounts[(a,b,c,d,e)] = fa(d+e) return fa(d+e) count = 0 """place prime in right spot""" count += getTotalCount(a+1,b,c-1,d,e) """place prime which could be right in wrong spot""" count += (c-1) * getTotalCount(a,b+1,c-2,d+1,e) """place prime which is already wrong in wrong spot""" count += d * getTotalCount(a,b+1,c-1,d,e) """place a composite in""" count += e * getTotalCount(a,b,c-1,d+1,e-1) possCounts[(a,b,c,d,e)] = count return count print getTotalCount(0,0,numPrimes,0,SIZE-numPrimes) *1.0 / fa(100) print "Time Taken:", time.time() - START """ variables that matter: a--number of numPrimes placed in right slot b--number of numPrimes placed in wrong slot c--number of numPrimes not placed which can still be put in right slot d--number of numPrimes not placed which can not be placed in right slot e--numComposites left = (100-i) - c -d a from 0 to 3 b from 0 to 22 c from 0 to 25 d from 0 to 25 e from 50 to 75
import time START = time.time() from math import factorial as fa val = 1000000 count = [0] * 9 pos = 0 while val > 0: while val >= fa(9-pos): val = val - fa(9-pos) count[pos] = count[pos]+1 pos = pos +1 #print count print count items = range(0,10) answer = [0]*10 for i in xrange(0,9): answer[i] = items[count[i]] del items[count[i]] #print items, answer answer = sum([10**(len(answer)-i -1) * answer[i] for i in range(len(answer))]) print answer print "Time Taken:", time.time() - START
def comb(n, r): return fa(n) // (fa(n-r) * fa(r))
from math import factorial as fa for i in range(0, 947): print(str(i) + '! = ' + str(fa(i)) + "\n\n")
def all_comb(self): ''' 所有组合数 ''' # n!/[(n-m)!m!] n=52 m=7 return fa(52)/(fa(49)*fa(3))
from math import factorial as fa n = int(input("n: ")) r = int(input("r: ")) print(fa(n) // (fa(r) * fa(n - r)))