Hi Andrew_Levenson

update:

I misread the spec. The sum() threw me.

The corrected output is

153 370 371 407
Here's my take on it :-)

Do you have a sub called sum? I've taken the liberty of assuming you may not.

I've also taken on board japhy's point about scoping and the for loop.

You're not storing the cubes anywhere so I've added a var to hold it.

There is, however, a slight snag. It only finds three!

666 870 960
With the code straigtened out a bit perhaps you can identify the error?

#!/bin/perl5 use strict; use warnings; my @numbers; for my $i (100..999){ my @num = split(//, $i); my $cubed; for my $j (@num){ $cubed += $j**3; } #my $added; #$added += $_ for @num; #print "$i -> $cubed -> $added\n"; if( ($cubed) == $i ){ push @numbers, $i; } } print "$_\n" for @numbers;

Hope this helps


In reply to Re: Cube/digit script. by wfsp
in thread Cube/digit script. by Andrew_Levenson

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.