Puzzles

Lately I've been using Pharo to solve Project Euler puzzles. Several things I've found:

  • Getting an idea to start is helpful. Resources like the Encyclopedia of Integer Sequences are a good start.
  • Early problems simply deal with integer overflow, this isn't a problem in Smalltalk since SmallIntegers are automatically converted into LargeIntegers.
  • More complex code can be profiled by adding them to a block and using timeProfile:

    [ 4700 naturalSummarySizeModulo: 1000000 ] timeProfile.

    This generates a diagram like:

    Results of profile
    One can find where the majority of the time is being spent.
  • If the code runs for a long while, one technique is to place it in a block and use fork to put it in the background. If a large amount of space is used in the background process, it isn't a good idea to save the image (it may hang, save the code to a repository instead). It also isn't such a good idea to increase the process priority.

    For a running process that is forked into the background, it is possible to bring up the process browser and profile the process:

    Difference between the server time
    More details are in the profiling chapter of Deep into Pharo.
  • Several of the Project Euler puzzles involve downloading a list of numbers and evaluating them. This can be done by something similar to:

    (ZnClient new
    	beOneShot;
    	get: 'https://projecteuler.net/project/resources/p079_keylog.txt';
    	contents) substrings collect: [ :e | e asNumber ]

Posted by jborden at 11 October 2017, 11:17 am link