Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Sum to 100 at Rosetta Code -- oneliner

by Discipulus (Canon)
on Feb 18, 2018 at 14:30 UTC ( [id://1209439]=note: print w/replies, xml ) Need Help??


in reply to Sum to 100 at Rosetta Code

just the first quest..
perl -E "say for grep{eval $_ == 100} glob join '{+,-,}',1..9"

PS sorry missed: -1+2-3+4+5+6+78+9

perl -E "say for grep{eval $_ == 100} glob '{-,}'.join '{+,-,}',1..9"

L*

UPDATE Feb 21 2018, just for my own pleasure:

1. Show all solutions that sum to 100
2. Show the sum that has the maximum number of solutions (from zero to infinity*)
3. Show the lowest positive sum that can't be expressed (has no solutions), using the rules for this task
4. Show the ten highest numbers that can be expressed using the rules for this task (extra credit)

perl -MList::Util="first" -E "@c[0..10**6]=(0..10**6);say for grep{$e= +eval;$c[$e]=undef if $e>=0;$h{$e}++;eval $_==100}glob'{-,}'.join'{+,- +,}',1..9;END{say for(sort{$h{$b}<=>$h{$a}}grep{$_>=0}keys %h)[0],firs +t{defined $_}@c;say for(sort{$b<=>$a}grep{$_>0}keys %h)[0..9]}" -1+2-3+4+5+6+78+9 1+2+3-4+5+6+78+9 1+2+34-5+67-8+9 1+23-4+5+6+78-9 1+23-4+56+7+8+9 12+3+4+5-6-7+89 12+3-4+5+67+8+9 12-3-4+5-6+7+89 123+4-5+67-89 123+45-67+8-9 123-4-5-6-7+8-9 123-45-67+89 9 211 123456789 23456790 23456788 12345687 12345669 3456801 3456792 3456790 3456788 3456786

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

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

    No recent polls found