cored has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; my $vlnum = 0; my $vldig = 0; my $vlsum = 0; my $vltmp = 0; print "Insert a number: "; chomp ( $vlnum = <STDIN> ); $vltmp = $vlnum; while ( $vlnum > 0 ) { $vldig = $vlnum / 10; $vlsum = $vlsum + ($vldig * $vldig * $vldig); $vlnum = $vlnum % 10; } if ( $vlsum == $vltmp ) { print "Is narc dude!"; } else { print "Try a +nother number!"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mathematics ethics
by dada (Chaplain) on Oct 28, 2002 at 15:12 UTC | |
that said, your script does not work. the while loop should look like: as for simplicity, I would do it like this: cheers, Aldo King of Laziness, Wizard of Impatience, Lord of Hubris | [reply] [d/l] [select] |
by RMGir (Prior) on Oct 28, 2002 at 16:00 UTC | |
And in that case, there's a nice complete list available as a link from that page, so you can just do it by table lookups.
But if it's really numbers equal to the sum of their digits ** 3, then I'm not sure what the exhaustive list would look like... I think there WOULD be one, though, since 9**3 is only 729, so that limits how big your number could be. So you could still precompute the possible answers, if you needed an efficient way to answer this question...
Hehe, it doesn't seem to be a problem that cries out for optimization, does it? :)
| [reply] [d/l] |
|
Re: Mathematics ethics
by thinker (Parson) on Oct 28, 2002 at 15:50 UTC | |
I think ($vldig * $vldig * $vldig) confused the issue a little. From this definition
NARCISSISTIC NUMBERS:
DEFINITION
A narcissistic number is an n-digit number that is the sum of the
n-th powers of its digits.
Examples:
153 = 1^3 + 5^3 + 3^3.
548834 = 5^6 + 4^6 + 8^6 + 8^6 + 3^6 + 4^6.
My attempt at a solution is
I hope this helps thinker | [reply] [d/l] |
|
Re: Mathematics ethics
by Thelonius (Priest) on Oct 28, 2002 at 15:08 UTC | |
| [reply] [d/l] |
|
Re: Mathematics ethics
by diotalevi (Canon) on Oct 28, 2002 at 14:38 UTC | |
| [reply] [d/l] [select] |
|
Re: Mathematics ethics
by greenback (Initiate) on Oct 28, 2002 at 16:06 UTC | |
| [reply] [d/l] |
|
Golf: Re: Mathematics ethics
by sauoq (Abbot) on Oct 28, 2002 at 23:43 UTC | |
My golfed solution
prints 1 if a number is narcisistic and "" otherwise. It should work reliably for numbers that don't require exponents in their representations as doubles on your system. That's up to and including the narcisistic 28116440335967 for me. I'm sure someone can do better... -sauoq "My two cents aren't worth a dime."; | [reply] [d/l] |
|
Re: Mathematics ethics
by BrowserUk (Patriarch) on Oct 28, 2002 at 16:58 UTC | |
Another way.
Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy | [reply] [d/l] |
|
Re: Mathematics ethics
by Enlil (Parson) on Oct 29, 2002 at 00:48 UTC | |
Not as elegant as sauoq's solution, but a solution nonetheless.
-enlil | [reply] [d/l] |