mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Add generate primes challenge
This commit is contained in:
parent
260ad2edc2
commit
4f93526ae5
0
math_probability/generate_primes/__init__.py
Normal file
0
math_probability/generate_primes/__init__.py
Normal file
169
math_probability/generate_primes/check_prime_challenge.ipynb
Normal file
169
math_probability/generate_primes/check_prime_challenge.ipynb
Normal file
|
@ -0,0 +1,169 @@
|
|||
{
|
||||
"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": [
|
||||
"# Challenge 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)\n",
|
||||
"* [Solution Notebook](#Solution-Notebook)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"* 20 -> 2, 3, 5, 7, 11, 13, 17, 19"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class PrimeGenerator(object):\n",
|
||||
"\n",
|
||||
" def generate_primes(self, max_num):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load 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": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Solution Notebook\n",
|
||||
"\n",
|
||||
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
220
math_probability/generate_primes/check_prime_solution.ipynb
Normal file
220
math_probability/generate_primes/check_prime_solution.ipynb
Normal file
|
@ -0,0 +1,220 @@
|
|||
{
|
||||
"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",
|
||||
"* 20 -> 2, 3, 5, 7, 11, 13, 17, 19"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": [
|
||||
"%run -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
|
||||
}
|
26
math_probability/generate_primes/test_generate_primes.py
Normal file
26
math_probability/generate_primes/test_generate_primes.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
from nose.tools import assert_equal, assert_raises
|
||||
|
||||
|
||||
class TestMath(object):
|
||||
|
||||
def test_generate_primes(self):
|
||||
prime_generator = PrimeGenerator()
|
||||
assert_raises(TypeError, prime_generator.generate_primes, None)
|
||||
assert_raises(TypeError, prime_generator.generate_primes, 98.6)
|
||||
assert_equal(prime_generator.generate_primes(20), [False, False, True,
|
||||
True, False, True,
|
||||
False, True, False,
|
||||
False, False, True,
|
||||
False, True, False,
|
||||
False, False, True,
|
||||
False, True])
|
||||
print('Success: generate_primes')
|
||||
|
||||
|
||||
def main():
|
||||
test = TestMath()
|
||||
test.test_generate_primes()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user