Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Match multiple number

by davido (Cardinal)
on Aug 17, 2018 at 22:21 UTC ( [id://1220541]=note: print w/replies, xml ) Need Help??


in reply to Match multiple number

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

Dave

Replies are listed 'Best First'.
Re^2: Match multiple number
by tobyink (Canon) on Aug 18, 2018 at 02:58 UTC

    Interesting. I guess this works for the same reason it does with 3 in decimals.

Log In?
Username:
Password:

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

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

    No recent polls found