In Re: [OT] How do I really learn javascript?, dragonchild remarks, "A lot of programmers have a program they always write to learn a new language." Upon reading this, I realized that I always write a prime number generator. Here's one I wrote in Perl and posted in Re: ulam's spiral too slow:

use strict; use warnings; my @primes = ( 2, 3 ); my $candidate = $primes[-1] + 2; my $max_prime = shift || 100_000; CANDIDATE: while ( $candidate < $max_prime ) { my $sqrt_candidate = sqrt $candidate; PRIME: foreach my $prime ( @primes ) { last PRIME if $prime > $sqrt_candidate; next CANDIDATE if 0 == $candidate % $prime; } push @primes, $candidate; } continue { $candidate += 2; } print join q{,}, @primes; print "\n";

I've written programs like this in Python, C, BASIC, and others. I wrote one in C when learning to use an arbitrary precision math library once. It's a fairly straight forward math problem, and I can tell if the output is correct without any test framework. It requires looping, and I can expand it to use subroutines when it's time to learn that.

I recall another programmer saying he always writes a hex dump program in a new language. Of course, there's also the ever-popular Hello world program.

I'd love to hear from the monks. What program do you write in a new language, and why?


In reply to What do you write when learning a new language? by kyle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.