in reply to Cube/digit script.
update:
I misread the spec. The sum() threw me.
The corrected output is
Here's my take on it :-)153 370 371 407
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
#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Cube/digit script.
by Andrew_Levenson (Hermit) on Mar 10, 2006 at 14:11 UTC | |
|
Re^2: Cube/digit script.
by Andrew_Levenson (Hermit) on Mar 10, 2006 at 14:21 UTC |