Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I was going through old scripts I had lying around and decided to clean them up a bit. I don't remember why I wrote them or what I am going to do with them. I think they are lukewarm uses for perl, and I probably reinvented the wheel on some of them. Instead of them just lying around my hard drive collecting dust, I share them with you, kind reader. Do with them what you will.

The first function lists primes, the second two functions list fractions, the last few are about Roman numerals. I lumped them together in a module called Numbers because I ran out of imagination.

Welcome to my sandbox.

package Fun::Numbers; use strict; use warnings; use List::Util qw(sum); ## Primes function # primes gets a list of primes between 1 and a specified number. # If the user wants all primes from 1 to 100, the usage would be... # primes(100); sub primes { my $last_number = shift; my @primes; for my $number (1..$last_number) { # all numbers ending in 2, 4, 6, 8, or 0 are divisible by 2. # all numbers ending in 5 or 0 are divisible by 5. next if $number =~ /(2|4|5|6|8|0)$/; # numbers where the sum of the digits are evenly divisible by 3 ar +e divisible by 3. next if sum(split(//,$number)) % 3 == 0; # numbers where the sum of the digits are evenly divisible by 9 ar +e divisible by 9. next if sum(split(//,$number)) % 9 == 0; # There are other tests like the two above, however, they require +breaking the number # and doing other calculations based on the number. I decided to s +kip those as they # are computation heavy. my $match = 0; for my $divisor (2..$last_number) { $match++, last if ($number != $divisor && $number % $divisor == +0); } push @primes, $number if $match == 0; # adding 2, 3, and 5 back to the list since they were excluded abo +ve. push @primes, (2,3,5) if $number == 1; } return @primes; } ## Fraction functions # fraction_value returns the fractional value of a number. # If the user wants 78/99 of 28, the usage would be... # fraction_value(78, 99, 28); sub fraction_value { my ($numerator, $denominator, $number) = @_; return ($number / $denominator) * $numerator; } # fractions_values returns a hash of all the fractional values of a nu +mber. # If the user wants the values of 1/2 to 98/99 of 28, the usage would +be... # fractions_values(99, 28); # fractions_values function written with the help of mofino and go|dfi +sh in #perlcafe on freenode. sub fractions_values { my ($max_denom, $number) = @_; my $max_denominator = $max_denom ? $max_denom : 4; my %fractions; for my $numerator (1..$max_denominator) { for my $denominator (($numerator + 1)..$max_denominator) { my $fraction = "$numerator/$denominator"; $fractions{$fraction} = fraction_value($numerator, $denominator, + $number); } } return %fractions; } ## Roman numerals functions # The use of lowercase letters to represent larger numbers is from Mat +h::Roman. # The use of underscore after to represent larger numbers is from Text +::Roman. # The use of underscore before to represent larger numbers is for comp +leteness. my %big_Roman_numerals = ( 'simple' => { 'lowercase' => [qw(v x l c d m)], 'underscore after' => [qw(V_ X_ L_ C_ D_ M_)], 'underscore before' => [qw(_V _X _L _C _D _M)] }, 'complex' => { 'lowercase' => [qw(Mv Mx xl xc cd cm)], 'underscore after' => [qw(MV_ MX_ X_L_ X_C_ C_D_ C_M_)], 'underscore before' => [qw(M_V M_X _X_L _X_C _C_D _C_M)] } ); # list_Roman_numerals_values returns a hash with the values of the ind +ividual Roman numerals. # If the user wants the Roman numerals for 4,000 and higher notated by + an underscore after the letter, the usage would be.. # list_Roman_numerals_values('underscore after'); # The other two options are 'lowercase' and 'underscore before'. sub list_Roman_numerals_values { my ($big_numeral) = @_; my @RSN = (qw(I V X L C D M), @{$big_Roman_numerals{'simple'}{$b +ig_numeral}}); # Roman simple numerals my @RCN = (qw(IV IX XL XC CD CM), @{$big_Roman_numerals{'complex'}{$ +big_numeral}}); # Roman complex numerals my %R2A; # Roman to Arabic @R2A{@RSN, @RCN} = qw( 1 5 10 50 100 500 1000 5000 10000 50000 100000 500000 1000000 4 9 40 90 400 900 4000 9000 40000 90000 400000 900000 ); # numeric values return %R2A; } # list_values_Roman_numerals returns a hash with the individual Roman +numerals of the values. # It is the reverse of list_Roman_numerals_values with the same usage. sub list_values_Roman_numerals { my ($big_numeral) = @_; my %A2R = reverse list_Roman_numerals_values($big_numeral); return %A2R; # Arabic to Roman } # Roman_overline returns a string with the large Roman numerals in an +HTML span to achieve the overline. # If the user wants the overline on 'mdxcMvXVIII', the usage would be. +.. # Roman_overline('mdxcMvXVIII', 'lowercase'); # Thanks to ikegami and runrig for their assistance in the CB the the +regexen. sub Roman_overline { my ($string, $notation) = @_; if ($notation eq 'lowercase') { $string =~ s/(\b|[VXLCDM]+)([vxlcdm]+)(\b|[IVXLCDM]+)/$1<span clas +s="overline">\U$2\E<\/span>$3/g; } if ($notation =~ /underscore/) { if ($notation =~ /after/) { $string =~ s/((\w_)+)/<span class="overline">$1<\/span>/g; } if ($notation =~ /before/) { $string =~ s/((_\w)+)/<span class="overline">$1<\/span>/g; } $string =~ s/_//g; } return $string; } 1;

If you want to see a Roman numeral (MDCCCMVCXXVII) with the overline, go to your display settings and set up the overline class in your style sheet as follows...

.overline { text-decoration: overline; }
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

In reply to Number functions I have lying around by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found