コード例 #1
0
ファイル: problems_61_80.py プロジェクト: dzhou/project-euler
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
コード例 #2
0
ファイル: problems_21_40.py プロジェクト: dzhou/project-euler
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)
コード例 #3
0
 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))
コード例 #4
0
ファイル: problems_21_40.py プロジェクト: dzhou/project-euler
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
コード例 #5
0
ファイル: problems_1_20.py プロジェクト: dzhou/project-euler
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))])
コード例 #6
0
 def runTest(self):
     self.assertEqual(math.factorial(self.a),factorial(self.a))
     self.assertEqual(factorial(self.g), 1)