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.

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
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.
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


In reply to Symmetrical Numbers (GOLF) by tachyon

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.