Programming
Workshop

Spring 2010
course
navigation

Fri jan 29 - practicing python

A bit more python syntax structures and syntax

dictionaries : data = {'color':'red', 'size':3} print data['color']
map, filter, lambda (functional programming style) : map(lambda x: 2*x**2, [12, 24, 30]) filter(lambda x: x<25, [10, 20, 30])
list comprehensions (no, I don't know why they call it that) : [i**2 for i in range(100) if 3*i > 20 if 3*i < 50]

modules, docs, tests

#!/usr/bin/env python """ foo.py An example of a python module containing a single simple class and a function. One way to test python code is with the doctest module: simply insert in any doc string what you'd get by running something interactively. Here's a test for the class. >>> b = Bar() >>> b.doit(21) 43 The test for the function baz(x) is given within the foo function. To run the tests, simply run python on this file. $ python foo.py To use the file from another python file, the syntax is from foo import Bar, baz # ... use Bar and baz Jim Mahoney, Jan 29 2010, GPL """ class Bar: """ A short example of a class. """ def __init__(self, multiplier=2): self.multiplier = multiplier def doit(self, x): """ Return x * the multiplier. """ return self.multiplier * x def baz(x="this", y="that"): """ Return the two passed arguments connected with ' and '. Convert small integer inputs (e.g. 2) to corresponding words, (e.g. "two"). >>> baz() 'this and that' >>> baz("it") 'it and that' >>> baz("one", 2) 'one and two' """ return str(x) + " and " + str(y) def _test(): from doctest import testmod testmod() if __name__=="__main__": _test()

primes and factors number problems

From http://projecteuler.net :
All of these have to do with divisors and primes. 3) The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? 7) By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13. What is the 10001-st prime number? 10) The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 12) The sequence of triangle numbers is generated by adding consecutive integers. For example, the 7th triangle number is 1+2+3+4+5+6+7 = 28. What is the value of the first triangle number to have over 500 divisors? 23) A perfect number's proper divisors sum to the number. An abundant number's proper divisors sum to more than the number. Find the sum of all positive integers which cannot be written as the sum of two abundant numbers. 27) Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0. 35) The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? 41) We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? 47) Find the first four consecutive integers to have four distinct prime factors. 49) The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence? 50) Which prime, below one-million, can be written as the sum of the most consecutive primes? 51) Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family. 60) Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
So: let's pick one of the early ones and give it a shot, eh?

Jim's primes.py API

I decided that there was so much primes stuff, I should create a primes.py module to do all of that. Here's its doc string.
""" Routines for working with primes and their divisors. >>> p = Primes(1000) >>> len(p) 168 >>> p[-1] 997 >>> 101 in p True >>> p.divisors(336) [1, 2, 3, 4, 6, 7, 8, 12, 14, 16, 21, 24, 28, 42, 48, 56, 84, 112, 168, 336] >>> p[0:4] [2, 3, 5, 7] >>> p.number2powers(336) [4, 1, 0, 1] >>> p.powers2number([4, 1, 0, 1]) # 2**4 + 3**1 + 5**0 + 7**1 336 >>> p.sum_divisors(336) # sum of factors less than 336 656 # Euler's totient function : >>> p.phi(25) # phi(n) = count relatively prime < n 20 # much faster than repeated p.phi() : >>> phi_list(10) # [phi(0), phi(1), ... phi(n-1)] [0, 0, 1, 2, 2, 4, 2, 6, 4, 6] Defining a Primes object with, say,"p=Primes(1000)" doesn't actually calculate anything. The first time that p is accessed in any way, all the primes up to 1000 are found and stored. All subsequent methods are fast. The Primes objects are saved so that subsequent objects can re-use the previous calculations if possible. The string representation gives a summary of how many primes were found and how long it took to do the calcultion. For example, on a 2.4GHz MacBookPro, str(Primes(2e6)) gives '148933 primes up to 2000000 found in 00:00:18.22'. This file aso has an interface to a probabilistic prime test as defined in miller_rabin.py. >>> from primes import is_prime >>> is_prime(101) True >>> is_prime(100) False Jim Mahoney | mahoney@marlboro.edu | Nov 2008 v0.1 | GPL """
http://cs.marlboro.edu/ courses/ spring2010/programming/ notes/ jan_29
last modified Friday January 29 2010 12:15 pm EST

attachments [paper clip]

     name last modified size
   foo.py Jan 29 2010 11:53 am 1.27kB