I just came across an interesing mathematical widget.
111,111,111 * 111,111,111 = 12,345,678,987,654,321
So here is the challenge:
1 Write a subroutine that takes one argument. This argument is to be a positive integer.
2 The sub is to find all the symmetrical squares between zero and the supplied argument.
3 Not only must the square be symmetrical, the number itself must also be symmetrical - thus conforming to the original example where both the number and its square exhibit symetry.
4 The sub is to return a reference to a hash which contains only those symetrical numbers that have symmetrical squares as its keys with the matching symmetrical squares as the values.
5 There is no requirement for use strict.
Here is a very straight forward example with the test code and desired output included.
Official scoring will be done with the "Official GOLF score" (tm) code presented below. It comfoms to the standard set down in RFC foobar with the baz ammendments. In short this standard specifies that we count the chars from (but not including) the subs opening curly up to (but not including) the closing curly. Leading/trailing whitespace and newlines are not counted. The code for this is presented below.use strict; my $ref = symetrical(100); for my $key (sort {$a <=> $b} keys %$ref) { printf "%-10d%d\n",$key,$ref->{$key}; } sub symetrical { my %symet; my $max = shift; for my $i (0..$max) { my $square = $i * $i; if (sym($square)) { $symet{$i} = $square if sym($i); } } sub sym { $num = shift; ($num eq reverse $num) ? $num : ''; } return \%symet; } __END__ Sample output 1 1 2 4 3 9 11 121 22 484 101 10201 111 12321 121 14641 202 40804 212 44944
package GOLF; use strict; my $code = <<'CODE'; # paste code in here CODE $code =~ s/^\s+|\s+$//gm; $code =~ s/\n//g; $code =~ s/}$//; $code =~ s/^sub\s+(\w+)\s*{//; printf "Length of sub '$1' => %d chars\n", length $code;
My effort stands at 194 chars, but I think I can do better ;-)
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Symetrical Numbers (GOLF)
by MeowChow (Vicar) on Jul 11, 2001 at 22:54 UTC | |
by chipmunk (Parson) on Jul 12, 2001 at 00:06 UTC | |
by MeowChow (Vicar) on Jul 12, 2001 at 00:15 UTC | |
|
(tye)Re: Symetrical Numbers (GOLF)
by tye (Sage) on Jul 11, 2001 at 22:47 UTC | |
|
Re: Symetrical Numbers (GOLF)
by particle (Vicar) on Jul 11, 2001 at 23:36 UTC | |
by tachyon (Chancellor) on Jul 11, 2001 at 23:43 UTC | |
by particle (Vicar) on Jul 12, 2001 at 00:08 UTC | |
|
Re: Symetrical Numbers (GOLF)
by no_slogan (Deacon) on Jul 11, 2001 at 22:55 UTC | |
|
Re: Symetrical Numbers (GOLF)
by jmcnamara (Monsignor) on Jul 12, 2001 at 01:17 UTC | |
by MeowChow (Vicar) on Jul 12, 2001 at 01:32 UTC | |
|
Re: Symetrical Numbers (GOLF)
by Masem (Monsignor) on Jul 11, 2001 at 23:04 UTC | |
by MeowChow (Vicar) on Jul 11, 2001 at 23:30 UTC | |
by tachyon (Chancellor) on Jul 11, 2001 at 23:17 UTC |