Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Mathematics ethics

by cored (Scribe)
on Oct 28, 2002 at 14:19 UTC ( [id://208505]=perlquestion: print w/replies, xml ) Need Help??

cored has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, this is an script that tell me if a number is narcicist, but i dont know i want to make it simple check it and tell me what you think thx ...
#!/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
    it took me quite a lot to understand that you were talking about narcissistic numbers (thanks Thelonius!).

    that said, your script does not work. the while loop should look like:

    while ( $vlnum > 0 ) { $vldig = $vlnum % 10; $vlsum = $vlsum + ($vldig * $vldig * $vldig); $vlnum = $vlnum / 10; }
    as for simplicity, I would do it like this:
    #!/usr/bin/perl -w use strict; my $vlnum = 0; my $vlsum = 0; print "Insert a number: "; chomp ( $vlnum = <STDIN> ); # map { $vlsum += $_ ** 3 } split //, $vlnum; # oops, this should be (thanks [broquaint]!): $vlsum += $_ ** 3 for split //, $vlnum; if ( $vlsum == $vlnum ) { print "Is narc dude!"; } else { print "Try another number!"; }
    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris

      Hmmm, If you're really going for narcissistic numbers as defined by that link, shouldn't the power be the length of the number, instead of 3?

      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.

      #!/usr/bin/perl -w # Silly super-efficient interactive narcissistic checker... use strict; # http://mathworld.wolfram.com/packages/Narcissistic.dat my %narcs=map {$_ => 1} ( 0,1,2,3,4,5,6,7,8,9,153,370,371,407,1634,8208,9474,54748,92727,93084, 548834,1741725,4210818,9800817,9926315,24678050,24678051,88593477, 146511208,472335975,534494836,912985153,4679307774,32164049650, 32164049651,40028394225,42678290603,44708635679,49388550606, 82693916578,94204591914,28116440335967,4338281769391370, 4338281769391371,21897142587612075,35641594208964132,35875699062250035 +, 1517841543307505039,3289582984443187032,4498128791164624869, 4929273885928088826,63105425988599693916,128468643043731391252, 449177399146038697307,21887696841122916288858,27879694893054074471405, 27907865009977052567814,28361281321319229463398, 35452590104031691935943,174088005938065293023722, 188451485447897896036875,239313664430041569350093, 1550475334214501539088894,1553242162893771850669378, 3706907995955475988644380,3706907995955475988644381, 4422095118095899619457938,121204998563613372405438066, 121270696006801314328439376,128851796696487777842012787, 174650464499531377631639254,177265453171792792366489765, 14607640612971980372614873089,19008174136254279995012734740, 19008174136254279995012734741,23866716435523975980390369295, 1145037275765491025924292050346,1927890457142960697580636236639, 2309092682616190307509695338915,17333509997782249308725103962772, 186709961001538790100634132976990,186709961001538790100634132976991, 1122763285329372541592822900204593,12639369517103790328947807201478392 +, 12679937780272278566303885594196922, 1219167219625434121569735803609966019, 12815792078366059955099770545296129367, 115132219018763992565095597973971522400, 115132219018763992565095597973971522401); while(<>) { chomp; if(exists $narcs{$_}) { print "Is narc, dude\n"; } else { print "Not narc, please try again\n"; } }

      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? :)
      --
      Mike

Re: Mathematics ethics
by thinker (Parson) on Oct 28, 2002 at 15:50 UTC
    Hi cored,

    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
    !/usr/bin/perl -w use strict; my $vlnum = 0; my $soln=0; print "Insert a number: "; chomp ( $vlnum = <STDIN> ); die "Not a number\n" unless $vlnum=~/^\d*$/; my @digits=split //, $vlnum; $soln += $_**@digits for @digits; if($vlnum == $soln) { print "$vlnum is a narcissistic number\n" } else{ print "$vlnum is not a narcissistic number!\n" };

    I hope this helps

    thinker
Re: Mathematics ethics
by Thelonius (Priest) on Oct 28, 2002 at 15:08 UTC
    Have you tested this code? It looks wrong to me. I would worry about it being right before it being simple. I think you should have:
    while ( $vlnum > 0 ) { $vldig = $vlnum % 10; $vlsum = $vlsum + ($vldig * $vldig * $vldig); $vlnum = int($vlnum / 10); }
Re: Mathematics ethics
by diotalevi (Canon) on Oct 28, 2002 at 14:38 UTC

    So what is a "narcicist number"? Your code was already pretty clear, a slight addition of syntactic sugar just makes it even clearer. So go on - explain what the heck this does, means and what ethics has to do with anything here. What they said.

    while ( $vlnum > 0 ) { $vlsum += ($vlnum / 10) ** 3; $vlnum %= 10; }
    __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;
Re: Mathematics ethics
by greenback (Initiate) on Oct 28, 2002 at 16:06 UTC
    As long as we're in base 10, here's all 88 narcissistic numbers, along with some code from this discussion.
    #!/usr/bin/perl -w use strict; my (@narcissists, %foo); my $vlnum = 0; print "Insert a number: "; chomp ( $vlnum = <STDIN> ); @narcissists = ( # Numbers, lot's of numbers... 0,1,2,3,4,5,6,7,8,9,153,370,371,407,1634,8208,9474,54748,92727,930 +84, 548834,1741725,4210818,9800817,9926315,24678050,24678051,88593477, 146511208,472335975,534494836,912985153,4679307774,32164049650, 32164049651,40028394225,42678290603,44708635679,49388550606, 82693916578,94204591914,28116440335967,4338281769391370, 4338281769391371,21897142587612075,35641594208964132,3587569906225 +0035, 1517841543307505039,3289582984443187032,4498128791164624869, 4929273885928088826,63105425988599693916,128468643043731391252, 449177399146038697307,21887696841122916288858,27879694893054074471 +405, 27907865009977052567814,28361281321319229463398, 35452590104031691935943,174088005938065293023722, 188451485447897896036875,239313664430041569350093, 1550475334214501539088894,1553242162893771850669378, 3706907995955475988644380,3706907995955475988644381, 4422095118095899619457938,121204998563613372405438066, 121270696006801314328439376,128851796696487777842012787, 174650464499531377631639254,177265453171792792366489765, 14607640612971980372614873089,19008174136254279995012734740, 19008174136254279995012734741,23866716435523975980390369295, 1145037275765491025924292050346,1927890457142960697580636236639, 2309092682616190307509695338915,17333509997782249308725103962772, 186709961001538790100634132976990,18670996100153879010063413297699 +1, 1122763285329372541592822900204593,1263936951710379032894780720147 +8392, 12679937780272278566303885594196922, 1219167219625434121569735803609966019, 12815792078366059955099770545296129367, 115132219018763992565095597973971522400, 115132219018763992565095597973971522401 ); @foo{@narcissists} = (); if ( exists $foo{$vlnum} ) { print "Is narc dude!"; } else { print "Tr +y another number!"; }
Golf: Re: Mathematics ethics
by sauoq (Abbot) on Oct 28, 2002 at 23:43 UTC

    My golfed solution

    perl -nle 'my$s;$t=$_;s/(\d)/$s+=$1**length/ge;print$t==$s'

    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.";
    
Re: Mathematics ethics
by BrowserUk (Patriarch) on Oct 28, 2002 at 16:58 UTC

    Another way.

    #! perl -sw use strict; local $\=$/; local $,=$/; sub IsNarcissistic ($) { my %narcissistic; @narcissistic{do{local $/;split /\s/, <<'END'; 4 5 6 9 93084 1741725 4210818 9800817 9926315 472335975 40028394225 42678290603 2 44708635679 49388550606 3 82693916578 534494 +836 54748 35641594208964132 35875699062250035 32164049651 146511208 94204591914 +912985153 1517841543307505039 3289582984443187032 4498128791164624869 4929273885 +928088826 449177399146038697307 21887696841122916288858 63105425988599693916 7 8 + 88593477 28361281321319229463398 153 35452590104031691935943 370 43382817693913 +71 548834 174088005938065293023722 188451485447897896036875 23931366443004156935 +0093 8208 1553242162893771850669378 3706907995955475988644380 1 4338281769391370 + 24678051 121204998563613372405438066 121270696006801314328439376 27907865009977 +052567814 128851796696487777842012787 27879694893054074471405 0 3706907995955475 +988644381 174650464499531377631639254 1550475334214501539088894 4422095118095899 +619457938 14607640612971980372614873089 28116440335967 1634 19008174136254279995 +012734740 19008174136254279995012734741 128468643043731391252 177265453171792792 +366489765 1145037275765491025924292050346 32164049650 371 1927890457142960697580 +636236639 2309092682616190307509695338915 21897142587612075 23866716435523975980 +390369295 186709961001538790100634132976990 24678050 407 17333509997782249308725 +103962772 186709961001538790100634132976991 4679307774 1122763285329372541592822 +900204593 1219167219625434121569735803609966019 92727 12639369517103790328947807 +201478392 12815792078366059955099770545296129367 8208 12679937780272278566303885 +594196922 115132219018763992565095597973971522400 115132219018763992565095597973 +971522401 END }} = undef; return exists $narcissistic{$_[0]}; } print "Insert a number: "; chomp ( my $vlnum = <STDIN> ); print IsNarcissistic($vlnum) ? "Is narc dude!" : "Try another number!" +;

    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: Mathematics ethics
by Enlil (Parson) on Oct 29, 2002 at 00:48 UTC
    I started playing with the BigInt Mod this morning, and coincidentally this thread showed up. I figured this would be a good place to try my skill.

    Not as elegant as sauoq's solution, but a solution nonetheless.

    use strict; use warnings; use Math::BigInt; print "Enter a Number:"; my $input = <STDIN>; chomp($input); my $tester=Math::BigInt->new($input); die "Not a Number" if $tester =~ /^[^+]|[^0-9+]/; my $power = length($tester) - 1; my $val = Math::BigInt->new(0); my @num_array = split //,$tester; foreach my $curr_numb (1 .. $#num_array) { my $curr_val = Math::BigInt->new($num_array[$curr_numb]); $curr_val = $curr_val ** $power; $val += $curr_val; } $val eq $tester ? print "$tester is narc\n" : print "Nope!";

    -enlil

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://208505]
Approved by nefertari
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found