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

Re: Simple Math Puzzle...

by EvdB (Deacon)
on May 12, 2003 at 07:47 UTC ( [id://257368]=note: print w/replies, xml ) Need Help??


in reply to Simple Math Puzzle...

I whipped up a brute force method which might be of interest as a reference.

# Crude but effective hunter. use strict; my %primes = map { $_ => 1 } ( 2,3,5,7 ); for (10234..98765) { my @a = split //; # Digit 1 is 1 less than digit 3. next unless $a[0] == $a[2]-1; # Digit 1 is higher than the sum of digits 4 and 5. next unless $a[0] > $a[3]+$a[4]; my @sorted = sort @a; my $highest = pop @sorted; my $lowest = shift @sorted; # Digit 3 is the highest number. next unless $a[2] == $highest; # Digit 2 is lowest. next unless $a[1] == $lowest; # Digit 5 is between digit 2 and digit 1 and is half of digit 4. my ($low, $high) = sort ($a[0], $a[1]); next unless $a[4] < $high; next unless $a[4] > $low; next unless $a[4] == $a[3]/2; # It has 2 prime digits. my $p_count = 0; foreach (@a) { $p_count++ if $primes{ $_ }; } next unless $p_count == 2; # There are no duplicates. my %uniq = (); foreach (@a) { $uniq{$_} = 1; } next unless scalar( keys %uniq ) == 5; # All tests passed - print number. print @a, "\n"; }

The results produced match those given above - so it must be right....

-- tidiness is the memory loss of environmental mnemonics

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found