Archive for July, 2007

isPrime in One Line of Perl

Tuesday, July 17th, 2007

As a follow up to Finding the first 20 terms in the Fibonacci Series, next in the 3Monkey Golf series — Determining if a number is prime. The following is a fairly simple Perl one liner to do the trick. I’m interested in seeing implementations in other languages. I will be posting follow ups with the best submissions from both Golf challenges at the end of the week.

Perl Code

perl -wle'print(1)if(1xtarget_number)!~/^1?$|^(11+?)\1+$/'

Character count = 36

Golf Rules:

  • Only pure source characters are counted, that is, in my example the characters “perl -wle'” and trailing “‘” do not count.
  • For this challenge, the meta variable target_number counts as a single character, butshould be able to be replaced with any reasonable number.
  • Output must be of the form 0, 1, Y, N, true, false, or some other easily recognizable indication of Yes or No.
  • Please note that use og characters such as > and < require escaping as & lt; and & gt;
    If you have difficulty posting your solution please email me the solution at stacy (at) 3monkeyweb (period) com and I will make sure it gets posted.

    Good luck, and until next time.


    3Monkeys on Emacs: Part 6 – Some Fun

    Saturday, July 14th, 2007

    Previous in this series : Part 5 – Replace

    Sometimes you need to step back and have a little fun. In keeping with that philosophy, I’m taking a small break from some of the more serious Emacs topics, and presenting a few fun things. Emacs has several built in stress relievers, from funny quotes to interactive games. Below are some diversions for your enjoyment.

    Yow M-x yow A simple diversion that displays a random Zippy quote in the echo area.
    Doctor M-x doctor Need a little psychotherapy? Call in the doctor.
    Life M-x life Conway’s Game of Life simulation. Still a fascination after all of these years.
    Hanoi M-x hanoi Solves the Tower of Hanoi puzzle. To specify the number of rings us C-u number M-x hanoi, where number is the desired number of rings.
    Mpuz M-x mpuz A multiplication puzzle where numbers are replaced with letters, the user must determine which numbers map to which letters.
    Blackbox M-x blackbox An analytical puzzle where the user must deduce the locations of balls based on certain reactions to events. See the full documentation with this command C-h f blackbox.
    Pegs M-x solitaire The classic peg jump game, where the user jumps one peg with a neighboring peg removing it in the process. The goal is to leave only one peg.
    Snake M-x snake The user navigates a snake in a square box, avoiding doubling back on itself or running into a wall, all while trying to collect tokens.
    Tetris M-x tetris The classic Tetris game we all are familiar with in one form or another.
    Adventure M-x dunnet A text based adventure game much like Zork. We all remember playing Zork on PDP-10’s right?

    Until next time.


    Fibonacci Series in One Line of Perl

    Thursday, July 12th, 2007

    Not ground breaking but here are the first 20 terms in the Fibonacci Series.

    perl -e'@p=(0,1);until($#p>20){print"$p[-2]\n";push @p,$p[-2]+$p[-1]}'


    Can you do better? What about different languages?

    Update: it has been suggested that for languages such as Java and C to be included in this experiment, that only the total number of pure source characters be included in the count. Therefore my perl solution in the comments below has 56 pure source characters.

    For more information on the Fibonacci Series see