Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Base20 Blues

by JimDuyer (Sexton)
on Jan 10, 2011 at 00:36 UTC ( [id://881391]=perlquestion: print w/replies, xml ) Need Help??

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

Hello there; I am rather new getting back to Perl, and I needed to do a script that would convert normal numbers to Mayan Base 20 counting symbols. I have the symbols part done, but I was hoping someone could help me put my script code into shape. I'm sure you can thing of some ways to make it faster or smaller or both. I only need to do from 1 to 9999, and I am substituting anything but numbers to //. Any help would be appreciated...
#!/usr/bin/perl print "Content-type: text/html\n\n"; use strict; use POSIX qw( ceil floor ); my $num = 9998; my $string1=''; my $string2=''; my $string3=''; my $string4=''; my $first8 = floor($num/8000); my $a = ($first8*8000); if ($first8 > 0) { $string1 .="1st line has $first8 times 8000 = $a"; } else { $string1 .="1st line has nothing "; } print "$string1 <br>\n"; my $second8= $num - $a; $second8 = floor($second8/400); my $b = ($second8*400); if ($second8 > 0) { my $third8 = $num - $second8 - ($second8*400); $string2 .="2nd line has $second8 times 400 = $b"; } else { $string2 .= "2nd line has nothing"; } print "$string2 <br>\n"; my $third8= $num-$a-$b; $third8 = floor($third8/20); my $c = ($third8*20); if ($third8 > 0) { $string3 .="3rd line has $third8 times 20 = $c"; } else { $string3 .="3rd line has nothing"; } print "$string3 <br>\n"; my $cumulative = $num - $a - $b - $c; if ($cumulative > 1) { $string4.="4th line has 1 times all = $cumulative"; } else { $string4.= "NOTHING LEFT OVER "; } print "$string4<br>\n";

Replies are listed 'Best First'.
Re: Base20 Blues
by ikegami (Patriarch) on Jan 10, 2011 at 00:46 UTC
    my @digits; while ($num > 0) { unshift @digits, $num % 20; $num = int($num / 20); } @digits = 0 if !@digits;

    Or you can use an existing module, like Convert::BaseN. (I have no experience with it; I just picked one pretty arbitrarily.)

      Sorry, Convert::BaseN Won't work, as it only supports number bases that are powers of two. From the POD docs:

      Convert::BaseN - encoding and decoding of base{2,4,8,16,32,64} strings

      Mind you, I would have made the same mistake. I had already given you a ++ before I checked the pod for that module.

      Thanks for your help to all. I really like the simplicity of ikegami's reply, but while it gives the answers, how would I make it so that it returns something when the result is zero, because the maya had a zero place holder. The others are also great, and I will learn from them; I just would rather not have to use a cpan module for such a simple task, and I need the results by number so I can output a symbol gif for each. Again, thanks to you pros.

        how would I make it so that it returns something when the result is zero

        It currently puts 0 in @digits. Adjust the last line as desired.

        @digits = 0 if !@digits;

        I just would rather not have to use a cpan module for such a simple task

        Well, it was complex enough that you wanted help with it. I'm also convinced mine can give the wrong result due to floating point error.

        my @digits; do { unshift @digits, $num % 20; $num = ( $num - ( $num % 20 ) ) / 20; } while $num > 0;
Re: Base20 Blues
by Anonyrnous Monk (Hermit) on Jan 10, 2011 at 02:08 UTC

    TIMTOWTDI :)

    #!/usr/bin/perl -wl use strict; use Math::Fleximal; my $b20 = Math::Fleximal->new("9998") ->change_flex([ map "<$_>", 0..19 ]) ->to_str(); print "$b20\n"; # ...verbosely: my $e = 0; $e++ while $b20 =~ s/.*\K<(\d+)>/ my $m = 20**$e; sprintf "%2d * %4d (20^%d) = %d\n", $1, $m, $e, $1*$m /e; print $b20; __END__ <1><4><19><18> 1 * 8000 (20^3) = 8000 4 * 400 (20^2) = 1600 19 * 20 (20^1) = 380 18 * 1 (20^0) = 18

    See also Math::Fleximal

Re: Base20 Blues
by zentara (Archbishop) on Jan 10, 2011 at 12:00 UTC
    use Math::BaseCnv;

    You have a base20 system in the mayan symbols. So assign each symbol a number in a hash, then convert your numbers to base 20, and retreive the symbol from the hash.

    Whether the mayans counted with a pure base20 system, I don't know, but you try it and see.

    10 fingers, 10 toes, makes sense for a counting system. :-)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Base20 Blues
by PeterPeiGuo (Hermit) on Jan 10, 2011 at 00:50 UTC

    Put duplicated code into method(s). You can also do this recursively, if you prefer.

    Peter (Guo) Pei

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found