コード例 #1
0
ファイル: prime_partitions.py プロジェクト: tompko/praxis
def prime_partitions(num):
    """Calculate a list of all prime partitions of num"""
    parts = {}

    parts[0] = set([()])
    parts[1] = set([])
    primes = sieve(num+1)

    for n in range(2, num + 1):
        parts[n] = set()
        for p in itertools.takewhile(lambda x: x <= n, primes):
            for pa in parts[n - p]:
                parts[n].add(tuple(sorted(pa + (p,))))
    return sorted(parts[num])
コード例 #2
0
 def test_corner_cases(self):
     self.assertEqual(sieve(-1), [])
     self.assertEqual(sieve(0), [])
     self.assertEqual(sieve(1), [])
     self.assertEqual(sieve(2), [2])
コード例 #3
0
 def test_some_primes(self):
     primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 
               31, 37, 41, 43, 47, 53, 59, 61, 67,
               71, 73, 79, 83, 89, 97]
     self.assertEqual(sieve(100), primes)
コード例 #4
0
 def test_programming_praxis(self):
     magic_number = 15485863
     self.assertEqual(len(sieve(magic_number)), 1000000)
コード例 #5
0
array = []
dec_to_base_recursive.change_base(number=10, base=2, callback=array)
""" NUMERIC OPTIMISATION """

gcd_iterative.gcd(a=5, b=10)

gcd_recursive.gcd(a=5, b=10)

lcm_iterative.lcm(a=5, b=10)

lcm_recursive.lcm(a=5, b=10)

prime_factorisation.factorise(number=100)

change_making.change(amount=6)

exponentiation_by_squaring.exponentiate(base=2, power=10)
""" SEQUENCES """

fibonacci_iterative.fibonacci(count=5)

fibonacci_recursive.fibonacci(count=5)

sieve_of_eratosthenes.sieve(limit=13)
""" NUMBER THEORY """

prime_number.is_prime(number=13)

perfect_number.is_perfect(number=6)