def problem74(): factorial_map = {} for n in range(10, 100): next_value = sum(mlib.factorial(int(c)) for c in str(n)) print next_value return 0
def problem24(): """ A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are: 012 021 102 120 201 210 What is the millionth lexicographic permutation of the digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? """ s = "0123456789" n = 10 ** 6 k = "" for x in range(9, -1, -1): for i in range(0, 10): if n > mlib.factorial(x): n -= mlib.factorial(x) else: k += s[i] s = s[:i] + s[i + 1 :] break return int(k)
def test_Factorial(self): """Tests mathlib.factorial""" randPositiveINT = self.rand_positive_int() randFLOAT1 = self.rand_float() self.assertEqual(mathlib.factorial(0), 1) self.assertEqual(mathlib.factorial(4), 24) self.assertTrue(math.isnan(mathlib.factorial(-5))) self.assertTrue(math.isnan(mathlib.factorial(4.532))) self.assertTrue( math.isnan(mathlib.factorial(randFLOAT1)) ) # QUESTION: is this correct? randFLOAT1 could be 2 for example. FIXED self.assertEqual(mathlib.factorial(randPositiveINT), math.factorial(randPositiveINT))
def problem34(): """ 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. """ fast_fac = {} for i in range(0, 10): fast_fac[i] = mlib.factorial(i) for i in range(10, fast_fac[9] * 9): fast_fac[i] = fast_fac[i / 10] + fast_fac[i % 10] total = 0 for n in range(10, fast_fac[9] * 9): if fast_fac[n] == n: total += n return total
def problem20(): """ n! means n x (n - 1) x ... x 3 x 2 x 1 Find the sum of the digits in the number 100! """ return sum([int(x) for x in str(mlib.factorial(100))])
def runTest(self): self.assertEqual(math.factorial(self.a),factorial(self.a)) self.assertEqual(factorial(self.g), 1)