Archive for March, 2009

Problem 2 ver. 1: brute-force solution

March 4th, 2009 by Ivan Lakhturov | 0 Category: Programming | Tags: |

I've written the generic procedure to generate lists iteratively:

  1.  

I've refactored (create-filtered-numbers-list from to filter) function into (numbers-filters) and added also (numbers) for future use:

  1.  

Now the brute-force solution for the problem 2

Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million

looks like this:

  1.  

The complexity is O(n). I decided not to generate a filtered list, but rather first generate, then filter, (fibonacci) function goes to the library for future use (for future functions I will not mention placing into my library). This is an optimal algorithm for generating a Fibonacci sequence, unlike computing the n-th member of it, which can be done more efficiently. However, for computing a sum of members, perhaps, there is a better solution, I will think about it later.

Code colorizer installed

March 3rd, 2009 by Ivan Lakhturov | 0 Category: Miscellaneous | Tags: |

I found the Source Code syntax highlighting plugin for WordPress, which makes use of GeSHi highliter and installed it. Now to get proper colorizing (the default code-tag of WordPress looks pale) for Scheme code I embed it into <pre lang="scheme">...<pre> tags.

Actually, I dont like default colors and already created dups of templates for Scheme R6RS and Nemerle. But will elaborate those later.

There is also JavaScript-based highligher named SHJS, which can simulate different IDEs, i.e. has many templates for each language, but the list of supported languages looks somewhat narrower and doesn't contain Lisp / Scheme. Which is not a problem of course, but anyway, I don't want client-side colorizing.

Hacked the WordPress theme again

March 3rd, 2009 by Ivan Lakhturov | 0 Category: Miscellaneous | Tags: |

Now you can see tags nearby postings. This is simply by adding the clause

<?php the_tags('<strong>Tags:</strong> ', ' | ', ''); ?>

to the <div class="date_author_comments"> in files single.php, index.php, search.php and archive.php of the WhitePress theme (version 1.1.7).

Backing up and upgrading WordPress

March 3rd, 2009 by Ivan Lakhturov | 0 Category: Miscellaneous | Tags: |

Overlooked the manual for backing up. It seems, the easiest way of backing up a WordPress database, listed here is to use the appropriate plugin. Installation didn't bring troubles. Backed up the DB also smoothly, however, had to choose "save to server" option, cause "download to my computer" didn't work (in Opera browser). There are just 10 tables in WordPress engine. Upgraded the engine using the manual without a problem as well.

Problem 1 ver. 2: brute-force solution generalized to a list of divisors

March 3rd, 2009 by Ivan Lakhturov | 0 Category: Programming | Tags: |

I refactored the previous solution and extracted such library functions, they could be useful in future:

  1.  

I think for these algoritms, where we have to iterate through large lists of numbers, the laziness (e.g. in Haskell) would give slightly more elegant code at certain places.

The first problem solution now becomes:

  1.  

For a list of divisors with length k the number of operations will be k*n. Actually, less than that because of shortcut (exists). Assuming k fixed and small, the complexity is still O(n). We could make use of the not yet written (create-numbers-list) function and filter that afterwards instead of the (create-filtered-numbers-list) function, but let us take less space in memory at once.

How to write and connect an own library in R6RS Scheme (PLT Windows solution)

March 2nd, 2009 by Ivan Lakhturov | 0 Category: Programming | Tags:

It took me quite a while to understand. Today I'm using a laptop with Windows, so here is solution for the windows-version of the PLT Scheme implementation. Under Linux it will not differ much. By the way, to use R6RS in PLT DrScheme you need to choose Module language (Ctrl+L) and write #!r6rs in the beginning of each program.

First, write your own library. I separated the function (divides?) from the previous program:

  1.  

Second, save it under the path, for example, C:\Projects\projecteulersolutions\project-euler-lib.scm and, as it is explained in "C:\Program Files\PLT\doc\r6rs\Installing_Libraries.html", compile and install the library into collections folder by running the command (the first way explained there seems not to be working):

"C:\Program Files\PLT\plt-r6rs.exe" --install C:\Projects\projecteulersolutions\project-euler-lib.scm

Some files drop into the directory "C:\Users\User\AppData\Roaming\PLT Scheme\4.1.4\collects\project-euler-lib"

Third, you connect it to the program by replacing (import (rnrs)) clause with (import (rnrs) (project-euler-lib)). And, of course, delete (divides?) function form the top-level program body.

Fourth, before recompiling a library, don't forget to clean the folder:

rmdir /s /q "C:\Users\User\AppData\Roaming\PLT Scheme\4.1.4\collects\project-euler-lib"

Problem 1 ver. 1: brute-force solution

March 2nd, 2009 by Ivan Lakhturov | 0 Category: Programming | Tags: |

Let us start with solutions to Project Euler problems. I will always start with brute-force solutions if those are possible and later on post optimized solutions. Every solution will be given in Scheme (R6RS) along with quick estimate of complexity. The reason for solving those problems is training myself in Scheme and, of course, fun.

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

Solution:

  1.  

This is the simplest straightforward approach. Backward-recursion is used, with tail-recursion optimization, which is obligatory for all Scheme implementations, will be transformed into iteration. Backward direction is to simplify a function signature (otherwise an extra parameter is needed).

The complexity is O(n), assuming the (mod) operation is atomic. More strictly, the number of operations is 2*n. By the way, in R5RS (mod) is called (remainder).