forked from chanshik/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime_factorization.py
62 lines (45 loc) · 1.64 KB
/
prime_factorization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
The prime factorization of a positive integer is a list of the integer's prime factors,
together with their multiplicities;
the process of determining these factors is called integer factorization.
The fundamental theorem of arithmetic says that
every positive integer has a single unique prime factorization.
The prime factorization of 24, for instance, is (2^3) * (3^1).
Without using the prime library, write a class called PrimeFactorizer
that takes in an integer greater than 1 and has a method called factor
which returns a hash where the keys are prime numbers
and the values are the multiplicities.
PrimeFactorizer(24).factor #should return { 2: 3, 3: 1 }
"""
from collections import defaultdict
from math import sqrt
class PrimeFactorizer(object):
def __init__(self, n):
self.factor = self.calc_factor(n)
@staticmethod
def is_prime(n):
if n <= 1:
return False
max_n = int(sqrt(n) + 1)
for i in xrange(2, max_n):
if n % i == 0:
return False
return True
@staticmethod
def calc_factor(target):
factors = defaultdict(int)
n = target
for i in xrange(2, n + 1):
if not PrimeFactorizer.is_prime(i):
continue
while n % i == 0:
n /= i
factors[i] += 1
if n <= 1:
break
return dict(factors)
from unittest import TestCase
class TestPrimeFactorizer(TestCase):
def test_PrimeFactorizer(self):
self.assertEquals(PrimeFactorizer(13).factor, {13: 1})
self.assertEquals(PrimeFactorizer(24).factor, {2: 3, 3: 1})