2017-03-29 16:45:04 +08:00
{
" cells " : [
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Solution Notebook "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Problem: Generate a list of primes. \n " ,
" \n " ,
" * [Constraints](#Constraints) \n " ,
" * [Test Cases](#Test-Cases) \n " ,
" * [Algorithm](#Algorithm) \n " ,
" * [Code](#Code) \n " ,
" * [Unit Test](#Unit-Test) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Constraints \n " ,
" \n " ,
" * Is it correct that 1 is not considered a prime number? \n " ,
" * Yes \n " ,
" * Can we assume the inputs are valid? \n " ,
" * No \n " ,
" * Can we assume this fits memory? \n " ,
" * Yes "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
" * None -> Exception \n " ,
" * Not an int -> Exception \n " ,
2017-12-07 09:10:38 +08:00
" * 20 -> [False, False, True, True, False, True, False, True, False, False, False, True, False, True, False, False, False, True, False, True] "
2017-03-29 16:45:04 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Algorithm \n " ,
" \n " ,
" For a number to be prime, it must be 2 or greater and cannot be divisible by another number other than itself (and 1). \n " ,
" \n " ,
" We ' ll use the Sieve of Eratosthenes. All non-prime numbers are divisible by a prime number. \n " ,
" \n " ,
" * Use an array (or bit array, bit vector) to keep track of each integer up to the max \n " ,
" * Start at 2, end at sqrt(max) \n " ,
" * We can use sqrt(max) instead of max because: \n " ,
" * For each value that divides the input number evenly, there is a complement b where a * b = n \n " ,
" * If a > sqrt(n) then b < sqrt(n) because sqrt(n^2) = n \n " ,
" * \" Cross off \" all numbers divisible by 2, 3, 5, 7, ... by setting array[index] to False \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(n log log n) \n " ,
" * Space: O(n) \n " ,
" \n " ,
" Wikipedia ' s animation: \n " ,
" \n " ,
" ![alt text](https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 1 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
" import math \n " ,
" \n " ,
" \n " ,
" class PrimeGenerator(object): \n " ,
" \n " ,
" def generate_primes(self, max_num): \n " ,
" if max_num is None: \n " ,
" raise TypeError( ' max_num cannot be None ' ) \n " ,
" array = [True] * max_num \n " ,
" array[0] = False \n " ,
" array[1] = False \n " ,
" prime = 2 \n " ,
" while prime <= math.sqrt(max_num): \n " ,
" self._cross_off(array, prime) \n " ,
" prime = self._next_prime(array, prime) \n " ,
" return array \n " ,
" \n " ,
" def _cross_off(self, array, prime): \n " ,
" for index in range(prime*prime, len(array), prime): \n " ,
" # Start with prime*prime because if we have a k*prime \n " ,
" # where k < prime, this value would have already been \n " ,
" # previously crossed off \n " ,
" array[index] = False \n " ,
" \n " ,
" def _next_prime(self, array, prime): \n " ,
" next = prime + 1 \n " ,
" while next < len(array) and not array[next]: \n " ,
" next += 1 \n " ,
" return next "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 2 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting test_generate_primes.py \n "
]
}
] ,
" source " : [
" %% writefile test_generate_primes.py \n " ,
" from nose.tools import assert_equal, assert_raises \n " ,
" \n " ,
" \n " ,
" class TestMath(object): \n " ,
" \n " ,
" def test_generate_primes(self): \n " ,
" prime_generator = PrimeGenerator() \n " ,
" assert_raises(TypeError, prime_generator.generate_primes, None) \n " ,
" assert_raises(TypeError, prime_generator.generate_primes, 98.6) \n " ,
" assert_equal(prime_generator.generate_primes(20), [False, False, True, \n " ,
" True, False, True, \n " ,
" False, True, False, \n " ,
" False, False, True, \n " ,
" False, True, False, \n " ,
" False, False, True, \n " ,
" False, True]) \n " ,
" print( ' Success: generate_primes ' ) \n " ,
" \n " ,
" \n " ,
" def main(): \n " ,
" test = TestMath() \n " ,
" test.test_generate_primes() \n " ,
" \n " ,
" \n " ,
" if __name__ == ' __main__ ' : \n " ,
" main() "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 3 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Success: generate_primes \n "
]
}
] ,
" source " : [
" %r un -i test_generate_primes.py "
]
}
] ,
" metadata " : {
" kernelspec " : {
" display_name " : " Python 3 " ,
" language " : " python " ,
" name " : " python3 "
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
" version " : 3
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
" pygments_lexer " : " ipython3 " ,
" version " : " 3.5.0 "
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}