Totally useless but a little bit of fun with a silly mathematical property:
#!/usr/bin/env perl
+
+
use strict;
+
use warnings;
+
use List::Util qw(sum);
+
+
# For each $n, convert $n to hex digits, add the digits together. If t
+he
# result has more than one hex digit, repeat this process adding the
+
# digits together. When there is only one hex digit remaining, if the
+
# digit is a '5', an 'a', or an 'f', the original number was divisible
+
# by 5.
+
+
for my $n (1..100) {
+
my $h = sprintf '%x', $n;
+
while(length($h) > 1) {
+
$h = sprintf '%x', sum(map {hex $_} split //, $h);
}
+
print "$n is a multiple of 5\n" if $h =~ m/^[5af]$/;
+
}
Output:
5 is a multiple of 5
10 is a multiple of 5
15 is a multiple of 5
20 is a multiple of 5
25 is a multiple of 5
30 is a multiple of 5
35 is a multiple of 5
40 is a multiple of 5
45 is a multiple of 5
50 is a multiple of 5
55 is a multiple of 5
60 is a multiple of 5
65 is a multiple of 5
70 is a multiple of 5
75 is a multiple of 5
80 is a multiple of 5
85 is a multiple of 5
90 is a multiple of 5
95 is a multiple of 5
100 is a multiple of 5
-
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.
|