예제 #1
0
 def test_fact(self):
     self.assertEqual(utils.fact(0), 1)
     self.assertEqual(utils.fact(5), 120)
     self.assertEqual(utils.fact(3), 6)
     with self.assertRaises(ValueError):
         utils.fact(-5)
     pass
 def test_fact(self):
     # with self.assertRaises(ValueError):
     #     utils.fact(-1)
     self.assertEqual(utils.fact(0), 1)
     self.assertEqual(utils.fact(1), 1)
     self.assertEqual(utils.fact(2), 2)
     self.assertEqual(utils.fact(5), 120)
    def test_fact(self):
        # À compléter...
        self.assertEqual(utils.fact(6), 720)
        self.assertEqual(utils.fact(0), 1)

        with self.assertRaises(ValueError):
            utils.fact(-5)
예제 #4
0
def nperms_with_part(part):
    ret = multinomial(part)
    for p in part:
        ret *= fact(p - 1)
    for n in same_ns(part):
        ret //= fact(n)
    return ret
 def test_fact(self):
     # À compléter...
     self.assertEqual(utils.fact(4), 24)
     self.assertEqual(utils.fact(7), 5040)
     self.assertEqual(utils.fact(0), 1)
     with self.assertRaises(ValueError):
         utils.fact(-2)
 def test_fact(self):
     list=[1,1,2,6,24,120,720,5040,40320,362880,3628800]
     for i in range(10):
         self.assertEqual(utils.fact(i),list[i])
     with self.assertRaises(ValueError):
         utils.fact(-1)
     with self.assertRaises(TypeError):
         utils.fact('a')
예제 #7
0
def pb15():
    """
    Problem 15 : Lattice paths
    One could prove that the number of such paths are Catalan Numbers i.e. (2n)!/(n!)^2 or (2n n).
    See utils.fact for implementation of the factorial function.
    """
    res = utils.fact(40) // (utils.fact(20)**2)
    return res
예제 #8
0
def bin(n, r):
    """
    Computes the binomial factor
    >>> bin(2,1)
    2
    >>> bin(23,10)
    1144066
    """
    return fact(n) / (fact(r) * fact(n - r))
예제 #9
0
파일: p239.py 프로젝트: doboy/euler
def do( N ):
    ways = ( choose( 25, N )
             * product( xrange( 75, 100 - N ) )
             * fact( 75 ) )

    all = fact( 100 )
    ret = ways * 10 ** 50 / all
    import sys
    print >>sys.stderr, (ret, N)
    return ret
    def test_fact(self):
        results_fact = (1,1,2,6,24,120,720,5040,40320,362880)
        for i in range(10):
            self.assertEqual(utils.fact(i) , results_fact[i])

        with self.assertRaises(ValueError):
            utils.fact(-1)

        with self.assertRaises(TypeError):
            utils.fact("2")
예제 #11
0
def postfact():
    numinput= int(request.forms.get('factorial'))
    fact= utils.fact(numinput)
    return '''
           the results: {}<br /><br />
           <a href= https://boiling-beach-99041.herokuapp.com>Home</a>
           '''.format(fact)
예제 #12
0
def pb20():
    """
    Problem 20 : Factorial digits sum.
    Basic implementation as we convert multiple times.
    """
    n = utils.fact(100)
    return sum(list(map(int, list(str(n)))))
예제 #13
0
def main():
	print 'task 20'
	res = 0
	s = str(utils.fact(100))
	for c in s:
		res += int(c)

	print res
예제 #14
0
def T(n, k):
    if k == 0:
        return 1
    else:
        ans = 1
        for j in xrange(k):
            ans *= (n + j)
        return ans / fact(k)
예제 #15
0
파일: euler11_20.py 프로젝트: poifra/euler
def prob12():
    limit = 500
    nbDiv = 0
    i = 1
    while nbDiv < limit:
        triangular = (i * (i + 1)) / 2
        nbDiv = len(utils.fact(triangular))
        i += 1
    return triangular
예제 #16
0
    def Answer_fact(self, number):
        try:
            nbr = int(number)
            x = str(utils.fact(nbr))

            return {"number": number, "fact": x}

        except:
            return {"number": number, "fact": "None"}
예제 #17
0
파일: p307.py 프로젝트: doboy/euler
def logP( k, n ):
    total = 0
    curr = choose( n, k / 2 + k % 2 )

    for r in xrange( k / 2 + k % 2, k + 1 ):
        total += curr
        curr = curr * ( n - r ) / ( r + 1 )

    return log( total ) + log( fact( k ) )
예제 #18
0
파일: p307.py 프로젝트: doboy/euler
def p( k, n ):
    total = 0
    curr = choose( n, k / 2 + k % 2 )

    for r in xrange( k / 2 + k % 2, k + 1 ):
        total += curr
        curr = curr * ( n - r ) / ( r + 1 )

    return total * fact( k )
예제 #19
0
파일: euler11_20.py 프로젝트: poifra/euler
def prob12():
    limit = 500
    nbDiv = 0
    i = 1
    while nbDiv < limit:
        triangular = (i * (i+1))/2
        nbDiv = len(utils.fact(triangular))
        i+=1
    return triangular
예제 #20
0
def factorial_page():
	decoded = request.forms.decode("utf-8")
	print(decoded)
	try:
		number = int(decoded.get("Number"))
	except ValueError:
		return "<h1> Please enter a <strong>valid</strong> number</h1>"

	try:
		result = fact(number)
	except ValueError:
		result = None

	return "The factorial of {} is {}".format(number, result)
예제 #21
0
def c(n, r):
    n_fact = fact(n)
    r_fact = fact(r)
    nr_fact = fact(n - r)
    return n_fact / (r_fact * nr_fact)
def test_fact2():
    assert fact(4) == 24
 def test_fact(self):
     self.assertEqual(utils.fact(4), 24)
     with self.assertRaises(ValueError):
         utils.fact(-1)
예제 #24
0
def fact(n) :
    var = int(n)
    result = utils.fact(var)
    return template('La factorielle de {{a}} est {{b}}', a=n,b=result)
def test_fact2():
    assert fact(4) == 24
 def test_fact ( self ):
     self.assertEqual(utils.fact(1),1)
     with self.assertRaises(ValueError):
         utils.fact(-2)
     self.assertEqual ( utils . fact (0) , 1)
예제 #27
0
 def test_fact(self):
     self.assertEqual(utils.fact(0), 0)
     self.assertEqual(utils.fact(4), 24)
     self.assertRaises(ValueError, utils.fact(-1))
     pass
def test_fact3():
    with pytest.raises(ValueError):
        n = fact(-1)
예제 #29
0
#! python3
"""How many such routes are there through a 20×20 grid?"""
import sys
from os.path import dirname
sys.path.insert(0, dirname(dirname(__file__)))
from utils import fact

print(fact(2 * 20) // (fact(20)**2))  # 2n! / n!^2
예제 #30
0
 def test_fact(self):
     # À compléter...
     self.assertEqual(utils.fact(0), 1)
     self.assertIsNone(utils.fact("zetze"))
     self.assertEqual(utils.fact(9), 362880)
예제 #31
0
def good(n):
    return (sum(fact(int(x)) for x in str(n)) == n)
def test_fact3():
    with pytest.raises(ValueError):
        n = fact(-1)
예제 #33
0
 def test_fact(self):
     self.assertEqual(utils.fact(0), 1)
     self.assertEqual(utils.fact(1), 1)
     self.assertEqual(utils.fact(5), 120)
예제 #34
0
 def test_fact(self):
     self.assertEqual(utils.fact(0) , 1)
     self.assertEqual(utils.fact(5) , 120)
예제 #35
0
 def test_some_number(self):
     self.assertEquals(fact(4), 24)
예제 #36
0
 def test_fact(self):
     self.assertEqual(utils.fact(5), 120)
     self.assertEqual(utils.fact(2), 2)
     pass
예제 #37
0
 def test_fact(self):
     self.assertEqual(utils.fact(5), 120)
     with self.assertRaises(ValueError):
         utils.fact(-5)
def test_fact():
    assert fact(3) == 6
예제 #39
0
 def test_simple_value(self):
     self.assertEquals(fact(0), 1)
예제 #40
0
def C(n, r):
    return fact(n) / (fact(r) * fact(n - r))
예제 #41
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Project Euler
# Problem 74
#
# Determine the number of factorial chains that contain exactly sixty
# non-repeating terms.

from utils import fact

LIMIT = 1000000
FACTS = [fact(n) for n in xrange(0,10)]

def fact_digits(n):
    result = 0
    while n:
        result += FACTS[n%10]
        n //= 10
    return result

def p074():
    untried = set(xrange(1,LIMIT))
    lengths = {}
    while(untried):
        n = untried.pop()
        seen = []
        in_cycle = n in seen
        in_lengths = n in lengths
        while not in_cycle and not in_lengths:
            seen.append(n)
def test_fact():
    with pytest.raises(ValueError):
        utils.fact(-2)
    assert utils.fact(0) == 1
    assert utils.fact(4) == 24
    assert utils.fact(5) == 120
예제 #43
0
 def test_fact(self):
     self.assertEqual(utils.fact(3), 6)
     pass
예제 #44
0
def binom(n, k):
    return utils.fact(n) // (utils.fact(k) * utils.fact(n - k))
from __future__ import division
from plotty import *
from utils import fact,Fibn
import itertools

savefigs=[3]

for fig in savefigs:
    if fig==1:
        N=30
        p=[]
        for h in range(31):
        
            p.append(fact(N)/fact(h)/fact(N-h) * (0.5)**N)
            
            
        h=range(31)
        
        figure(1)
        #lineplot(h,p)
        plot(h,p,'--o',linewidth=3,markersize=10)
        xlabel('Number of heads')
        ylabel('$P(h,N=30)$')
        grid(True)
        draw()
        
        savefig('../../figs/coinflips1.pdf')

    elif fig==2:
        # solution for streaks http://marknelson.us/2011/01/17/20-heads-in-a-row-what-are-the-odds/
        N=50
예제 #46
0
	def test_fact(self):
		self.assertEqual(1, utils.fact(0))
		self.assertEqual(1, utils.fact(1))
		self.assertEqual(6, utils.fact(3))
		with self.assertRaises(ValueError):
			utils.fact(-1)
 def test_fact(self):
     self.assertEqual(utils.fact(3), 6)
     self.assertEqual(utils.fact(7), 5040)
     self.assertEqual(utils.fact(0), 1)
     self.assertEqual(utils.fact(-4), ValueError)
예제 #48
0
 def test_fact(self):
     self.assertEqual(utils.fact(3), 6)
     self.assertEqual(utils.fact(4), 24)
     # pour tester si la fonction lance une exception
     with self.assertRaises(ValueError):
         utils.fact(-1)
def test_fact():
    assert fact(3) == 6
예제 #50
0
def prob_20():
    return sum(int(x) for x in str(fact(100)))
예제 #51
0
def c(n, r):
    return (fact(n) / fact(r)) / fact(n - r)
예제 #52
0
 def test_fact(self):
     self.assertEqual(utils.fact(3), 6)
     self.assertEqual(utils.fact(4), 24)
     self.assertEqual(utils.fact(0), 1)
     with self.assertRaises(ValueError):
         utils.fact(-5)
예제 #53
0
def dfsum(n):
    s = 0
    for i in range(numDigits(n, 10)):
        s += fact(nthDigit(n, i, 10))
    return s
예제 #54
0
 def test_fact(self):
     self.assertEqual(utils.fact(3),6)
     with self.assertRaises(ValueError):
         utils.fact(-1)
예제 #55
0
 def test_fact(self):
     self.assertEqual(utils.fact(0), 2)
     pass
예제 #56
0
 def test_fact(self):
     # À compléter...
     self.assertEqual(utils.fact(3), 6)
 def test_fact(self):
     with self.assertRaises(ValueError):
         utils.fact(-1)
     self.assertEqual(utils.fact(5),120)
     pass