Skip to content

Solutions for prime and palindrome #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,78 @@
This is a test is intended to test your knowledge of basic programming, coding style, algorithms, django and javascript.
You will be judged more on the style of the code, maintainibility and readability than how the UI looks.
# Python Challenge


## Overview

Some programming exercises

### Sieve / Primes

Relied heavily on this [paper](http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf). Executes in under 20 seconds on my machine when asking for the 1 millionth prime.

- Implemented the 'failful sieve' according to that paper in three incarnations
- Naive - tries all candidates (including evens) up to p^2
- Used wheel skipping multiples of 2 & 3
- Used wheel skipping multiples of 2, 3 & 5 (however, something did not perform well in this implementation)
- Other optimizations included
- Uses lazy iteration described in the paper. Does not generate all multiples of p up to limit but only as needed.
- Optimizations not attempted
- Composites managed with priority queue. The paper indicated that a priority queue yielded large performance increases. However, after reading [this answer](http://stackoverflow.com/questions/13463417/the-genuine-sieve-of-eratosthenes-in-python-why-is-heapq-slower-than-dict) on stackoverflow it seems a plain dict is probably just as fast and possibly better.

### Palindrome

The approach taken avoids making a brute force attempt at combing all n-digit numbers and testing for palindrome. Instead, it first generates the list of all palindromes for the expected range and then tests for factors in the n-digit range. This seems like a good approach since the number of palindromes grows slowly in relation to the number of digits in the products.

Other points of interest in the implementation:
- Implemented a function which takes the number of digits of the factors as a parameter. Tested for n=2 & 3. It should work for larger n without modification.
- Avoided testing for palindrome-ness or manipulating the data as strings
- Next optimization that I would address would be to avoid brute forcing the n-digit factor testing of palindromes. For large n this could be expensive. Going after prime factors might be a sensible next step.

## Installation

### Manual installation

#### Virtualenv
Recommended that you create a virtualenv to install this code and it's dependencies into.

See [virtualenv docs](https://pypi.python.org/pypi/virtualenv)

#### Run the install
You can install the code simply by running

`python setup.py test`

If the tests pass proceed with the intsall. Execute

`python setup.py install`

from the project's root directory.

## Usage

After installation the following will be available on your path

`nth-prime.py n`

You can run it without arguments to get a help message or specify an integer of reasonable size to see what the nth prime is.

`palindrome-product.py`


## Testing
You can run the automated tests with

`python setup.py test`

If the package is already installed there might be some conflicts. You can usually clean these up by deleting the build directory

`rm -rf build/`

from the project's root directory.

The run tests from setup.py or by `py.test`

## Information

Source: https://github.com/lakeslc/python_test

Authors: [Jonathan Randall](https://github.com/lakeslc/)
20 changes: 0 additions & 20 deletions array.txt

This file was deleted.

38 changes: 38 additions & 0 deletions bin/nth-prime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python

import argparse
import sys
import datetime
from datetime import timedelta
from primes.sieves import WheelSieve

desc = '''Find the nth prime number using the sieve of Eratosthenes'''

parser = argparse.ArgumentParser(description=desc)
parser.add_argument('nth', metavar='n', type=int,
help='\'n\' as in the nth prime number')
args = parser.parse_args()


if args.nth < 1:
msg = 'Must specify an number greater than 0'
raise argparse.ArgumentTypeError(msg)

if args.nth > 1000000:
msg = 'Umm... that\'s a really big number... maybe try something smaller?'
raise argparse.ArgumentTypeError(msg)

start = datetime.datetime.now()
primes = WheelSieve().sieve()
last_prime = 0
for i in xrange(args.nth):
last_prime = primes.next()
finish = datetime.datetime.now()
delta = finish - start

msg = '''The nth prime for n={0:,} is {1:,}
Finished in {2} milliseconds'''

print msg.format(args.nth,
last_prime,
(delta.seconds * 1000) + (delta.microseconds/1000.0))
23 changes: 23 additions & 0 deletions bin/palindrome-product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python

import sys
import datetime
from datetime import timedelta
from palindrome.products import largest_palindrome

desc = '''Find the largest palindrome made from the\
product of two 3-digit numbers.'''

start = datetime.datetime.now()
result = largest_palindrome(3)
finish = datetime.datetime.now()

msg = '''That largest palindrome with two 3-digit factors is {0:,}
It's factors are {1} & {2}
Finished in {3} milliseconds'''

delta = finish - start
print msg.format(result.palindrome,
result.factor1,
result.factor2,
(delta.seconds * 1000) + (delta.microseconds/1000.0))
6 changes: 0 additions & 6 deletions django_test/README.md

This file was deleted.

151 changes: 0 additions & 151 deletions django_test/django_test/settings.py

This file was deleted.

17 changes: 0 additions & 17 deletions django_test/django_test/urls.py

This file was deleted.

28 changes: 0 additions & 28 deletions django_test/django_test/wsgi.py

This file was deleted.

10 changes: 0 additions & 10 deletions django_test/manage.py

This file was deleted.

File renamed without changes.
Loading