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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |