Project Euler #3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Simply factorize the target number and select the maximum value from the factors.  Should be pretty simple.  It should be noted, however, that simply iterating through every number is not going to suffice, because a number can have two of the same factors.

For example, the number 28 has a prime factorization of 2, 2, and 7.  The fact that there are two 2’s in the factorization should be taken into account when writing your solution.

Project Euler #2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

We are given our bounds in the problem,  1..4000000 , and we are to find and sum every even fibonacci number inside the given bounds.

To start off, we should define a function to generate a list of fibonacci numbers.  I opted for a recursive function, but this could easily be achieved using simple loops as well.

def fib(nums, maxNum):
    if not (nums[-1] >= maxNum):
        nums.append(nums[-1] + nums[-2])
        fib(nums, maxNum)
    return nums

With that function defined, the rest of the problem is simply iterating through the array of fibonacci numbers, testing if they are even, and adding them to a sum value.

Project Euler # 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

This one is pretty simple from the get-go.  Simply iterate through numbers  1..999  and check if they are a multiple of 3 or 5.  If they are, add them to the total.

Creating an easy-use framework for Python applications

Recently I have been working on a framework for database driven Python applications.

Current code can be found here.

The idea here is to abstract the database work from the programming of the application.  Instead of writing a series of complex SQL commands to modify and migrate database tables, you can simply edit a single row in the `ApplicationData` or `DocumentDictionary` tables, and accomplish the work of a whole database migration with a simple `update` statement.

Data

All data is stored as JSON values, so if, for example, you would traditionally have a table like this:

point2d
id x-value y-value
1 0 0
2 1 1

With this framework, you would simple create an entry in the `DocumentDictionary` table for each of the columns, specifying the DocType, FieldName, and a few other things. Then, when creating a new Document of type `point2d`, the framework would create an entry in the `DocumentData` table, with the JSONData looking something like `{‘x-value’: 0, ‘y-value’: 0}`.

One of the downsides to using this system is that it adds computational overhead to database and data access. I believe that the ease-of-use justifies this, and the abstraction allows for more readable code overall.

Classes

Currently the framework provides a set of 4 classes to be used when programming.

  • framework.Framework(db = ‘/DB/application.sqlite’)
    • You’d instantiate this at the start of your application, optionally specifying your database location if it is different than the standard.
    • Stores all your important stuff in Memory, such as your current DocumentTypes, and currently open Docs
  • framework.Document()
    • Can be created anytime, by any object. Stores data in JSON, as seen in the DB structure.
    • Calling Document.commit() will write your Document to the database.  You should avoid doing this directly, because if Document().__dict__[“id”] already exists, you are going to overwrite it and lose your data.
  • framework.DocumentType()
    • Has two instance variables, one containing a string stating the DocType, the other being a list of framework.Field() s.
    • These are loaded from the database by Framework().loadDocTypes() , but you can create them yourself and write them to the database at will.  Please make sure that you do not overwrite anything important.
  • framework.Field()
    • Simply specifies a Field for use in a DocType
    • Corresponds to a typical Column in a database structure
    • Can be manually created and added to a DocType to create new “columns” in the database