Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Newbie question

by karlgoethebier (Abbot)
on Aug 26, 2022 at 10:38 UTC ( [id://11146448]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Newbie question
in thread Newbie question

Yes, sure. Some more inspiration might be found here. Sometimes it is good to take a look under a hood.

sub sumdigits { my($n,$base) = @_; my $sum = 0; $base = 2 if !defined $base && $n =~ s/^0b//; $base = 16 if !defined $base && $n =~ s/^0x//; if (!defined $base || $base == 10) { $n =~ tr/0123456789//cd; $sum += $_ for (split(//,$n)); } else { croak "sumdigits: invalid base $base" if $base < 2; my $cmap = substr("0123456789abcdefghijklmnopqrstuvwxyz",0,$base); for my $c (split(//,lc($n))) { my $p = index($cmap,$c); $sum += $p if $p > 0; } } $sum; }

From Math::Prime::Util::PP. List::MoreUtils::PP might be also of interest.

 

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

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

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

    No recent polls found