Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

It's been a while since i've done much Perl, or posted here, so please take a tetanus shot and bear with me, because i'm a little rusty. This might not be the most interesting problem, but i certainly had fun with it...

A Kaprekar number is one where if you square it and break it in half (the right side larger for oddly lengthed numbers), the two halves add up to the original number. This is only roughly the definition, you can go here for a longer layman's definition, and here for a rigorous definition.

One example Kaprekar number: 297
297**2 == 88,209.
88 + 209 == 297

Given a subroutine outline like so:
sub kap_check { ... }

the golf is to fill in the interesting bits that accept one numeric argument and return true or false depending on whether the input is a Kaprekar number or not. If you take a look at the rigorous page, you'll notice some strange numbers that don't fit in with the layman's definition above (like 4879). Ignore those numbers for the purpose of this golf.

My solution at 53 chars:
#23456789_123456789_123456789_123456789_123456789_123456789_ $_=$_[0]*pop;$==.5*length;/(.{$=})(.*)/;$1+$2==$_**.5
jynx

In reply to [golf] Kaprekar Numbers by jynx

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found