def elemop(N=1000): r''' (Takes about 40ms on a first-generation Macbook Pro) ''' for i in range(N): assert a + b == 579 assert a - b == -333 assert b * a == a * b == 56088 assert b % a == 87 assert divmod(a, b) == (0, 123) assert divmod(b, a) == (3, 87) assert -a == -123 assert pow(a, 10) == 792594609605189126649 assert pow(a, 7, b) == 99 assert cmp(a, b) == -1 assert '7' in str(c) assert '0' not in str(c) assert a.sqrt() == 11 assert _g.lcm(a, b) == 18696 assert _g.fac(7) == 5040 assert _g.fib(17) == 1597 assert _g.divm(b, a, 20) == 12 assert _g.divm(4, 8, 20) == 3 assert _g.divm(4, 8, 20) == 3 assert _g.mpz(20) == 20 assert _g.mpz(8) == 8 assert _g.mpz(4) == 4 assert a.invert(100) == 87
def apply_int(self, n, evaluation): 'Factorial[n_Integer]' if n.value < 0: return Symbol('ComplexInfinity') else: return Integer(fac(n.value))
def permutations(word, num): ''' Returns string permutation number "num" ''' #Recursion ends when the passed the word becomes a long one character. if len(word) == 1: return word[0] #Deducted symbol symbol = str() #And the word without deducted symbol for further recursion. new_word = str() #The total number of permutations in the given word. segment = gmpy.fac(len(word)) #We find the index of the desired character #by rounding to the next higher result of separating #the total number permutations to segments. index = int(math.ceil( float(num) / (float(segment)/(len(word))) )) #We remove the indexed character from the word, and store it. #New word will participate in the recursion for i in xrange(len(word)): if i != (index - 1): new_word += word[i] else: symbol = word[i] #Finding number desired combination in new segment if num > (segment / len(word)): num -= (index - 1) * (segment / (len(word))) print "%i %i" %(segment, num) #Returns indexed character and go to futher recursion #with new word and new number of combination return symbol + permutations(new_word, num)
def elemop(N=1000): r''' (Takes about 40ms on a first-generation Macbook Pro) ''' for i in range(N): assert a+b == 579 assert a-b == -333 assert b*a == a*b == 56088 assert b%a == 87 assert divmod(a, b) == (0, 123) assert divmod(b, a) == (3, 87) assert -a == -123 assert pow(a, 10) == 792594609605189126649 assert pow(a, 7, b) == 99 assert cmp(a, b) == -1 assert '7' in str(c) assert '0' not in str(c) assert a.sqrt() == 11 assert _g.lcm(a, b) == 18696 assert _g.fac(7) == 5040 assert _g.fib(17) == 1597 assert _g.divm(b, a, 20) == 12 assert _g.divm(4, 8, 20) == 3 assert _g.divm(4, 8, 20) == 3 assert _g.mpz(20) == 20 assert _g.mpz(8) == 8 assert _g.mpz(4) == 4 assert a.invert(100) == 87
def num_check(num): ''' Testing digits of number return true if num = sum( fac(digit)) ''' if num <= 2: return False if num == sum(gmpy.fac(int(ch)) for ch in str(num)): return True return False
def exp_coefficients(M, bits): N = 2*M+50 Qs = [fac(k) for k in range(N)] prevstop = 0 for k in range(N): if Qs[k] >= 2**bits-1: q = Qs[k-1] for i in range(k, N): Qs[i] //= q for i in range(prevstop, k): Qs[i] = q prevstop = k Ps = Qs[:] fact = 1 for k in range(1, N): assert Qs[k] < 2**bits-1 if Qs[k] == Qs[k-1]: fact *= k else: fact = k Ps[k] //= fact return map(int, Ps)[:N], map(int, Qs)[:N]
def exp_coefficients(M, bits): N = 2 * M + 50 Qs = [fac(k) for k in range(N)] prevstop = 0 for k in range(N): if Qs[k] >= 2**bits - 1: q = Qs[k - 1] for i in range(k, N): Qs[i] //= q for i in range(prevstop, k): Qs[i] = q prevstop = k Ps = Qs[:] fact = 1 for k in range(1, N): assert Qs[k] < 2**bits - 1 if Qs[k] == Qs[k - 1]: fact *= k else: fact = k Ps[k] //= fact return map(int, Ps)[:N], map(int, Qs)[:N]
from itertools import count from gmpy import mpz, fac for i in count(): if len(str(fac(i))) >= 2010: break print "answer is %d" % i
from gmpy import fac problem708 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for i in xrange(1, 1001): problem708[int(str(fac(i)).replace("0", "")[-1:])] += 1 print "answer is %d" % max(problem708)
import sys import math import gmpy def sum_of_digits(n): ''' Find sum of digits in number by converting it to string ''' s = str(n) num = (int(i) for i in s) return sum(num) ''' One can write his own Stirling Series with best number of terms for particular n or use a GMP-inspired algorithm explained at: http://gmplib.org/manual/Factorial-Algorithm.html with Karatsuba multiplication module. But we can cheat and use gmpy library =) ''' print sum_of_digits(gmpy.fac(int(sys.argv[1])))
import gmpy from gmpy import fac a = fac(599999) print(a.numdigits())
#!/usr/bin/python # Calculate digits of e # Marcus Kazmierczak, [email protected] # July 29th, 2004 # the formula # e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ... 1/N! # You should chop off the last five digits returned # they aren't necessarily accurate # precision library import gmpy # how many digits (roughly) N = 1000 gmpy.set_minprec(int(N*3.5+16)) e = gmpy.mpf('1.0') for n in range(1,N+3): e = gmpy.fdigits(gmpy.mpf(e) + (gmpy.mpf('1.0') / gmpy.fac(n))) print e
# This is also for the Brown numbers project # This only outputs the number I care about. # % Brown numbers have the following properties: # % (m,n) such that: # % n!+1 = m^2 # % Paul Erdos postulated that there are only 3 sets. import math import time import gmpy n = 1000000000 start = time.time() test = gmpy.fac(n)+1 if (gmpy.sqrtrem(test))[1]==0: m = gmpy.sqrt(test) end = time.time() # Final time calculation, duplicate for legibility print 'It is a brown number!' # For legibility print 'n = ' + str(n) print 'm = ' + str(m) print 'time = ' + str(end-start) else: end = time.time() # Final time calculation, duplicate for legibility print 'Nope.' print end-start
# This is also for the Brown numbers project # This puts out less data for easier number processing # % Brown numbers have the following properties: # % (m,n) such that: # % n!+1 = m^2 # % Paul Erdos postulated that there are only 3 sets. import math import time import gmpy n = 2 while n > 0: # An intentional infinite loop start = time.time() test = gmpy.fac(n) + 1 if (gmpy.sqrtrem(test))[1] == 0: m = gmpy.sqrt(test) end = time.time() # Final time calculation, duplicate for legibility print '-------------------------' # For legibility print 'n = ' + str(n) print 'm = ' + str(m) print 'time = ' + str(end - start) print '-------------------------' # For legibility else: end = time.time() # Final time calculation, duplicate for legibility print n print end - start n += 1
def _lru_fac(x): return fac(x)
def central_bin_coef(n): return gmpy.fac(2 * n) // gmpy.fac(n) ** 2
def A(k, n): return gmpy.comb(n, k) * gmpy.fac(k)
import gmpy as _g import time print "Typical expected results would be:",""" D:\PySym>python timing.py Factorial of 10000 took 0.0619989238859 (35660 digits) Fibonacci of 10000 took 0.000744228458022 (2090 digits) Factorial of 100000 took 4.44311764676 (456574 digits) Fibonacci of 100000 took 0.022344453738 (20899 digits) Factorial of 1000000 took 152.151135367 (5565709 digits) Fibonacci of 1000000 took 0.670207059778 (208988 digits) """ print "Actual timings and results...:" for i in (10000,100000,1000000): start=time.clock() x=_g.fac(i) stend=time.clock() print "Factorial of %d took %s (%d digits)" % ( i, stend-start, x.numdigits()) start=time.clock() x=_g.fib(i) stend=time.clock() print "Fibonacci of %d took %s (%d digits)" % ( i, stend-start, x.numdigits())