Add time script Perl - perl

I would like to create a script that will receive 2 paramerters (hours and minutes) ( HH1:MN1 and HH2:MN2)
It has to valid if the #ARGV = 2
Valide if the time provide is correct (hours between 0 to 200 and minutes between 0 to 59)
Add those thow time and print to results
If it is more than 24 hr to print Nbday; HH:Min
if it is more than 7 days it will print Week; nddays; HH:Min.
I started with this but cant figureout how to continue
Any help or idea will be welcomed for the calculation
Thanks
#!/usr/bin/perl
if ($#ARGV != 2)
{
print STDERR "Erreur Parameters have to be 2\n";
exit (-1);
}
if ($ARGV[0] = ~ / ([0-9] | 1 [0-9] ? [0-9] | 200 ) : ( [0-5] ? [0-9] ) /)
{
$heures1 = $1;
$minutes1 = $2;
}
else
{
print STDERR "first parameter invalid\";
exit (-1);
}
if ($ARGV[1] = ~ / ([0-9] | 1 [0-9] ? [0-9] | 200 ) : ( [0-5] ? [0-9] ) /)
{
$heures2 = $3;
$minutes2 = $4;
}
`else `
{
print STDERR "Second parameter invalid\";
exit (-1);
$heures = $heures1 + $heures2;
$minutes = $minutes1 + $minutes2'

The validation code is pretty straightforward:
sub usage {
print STDERR $_[0] if #_;
print STDERR "usage: ...\n";
exit(1);
}
usage() if #ARGV != 2;
my ($hours1, $minutes1) = $ARGV[1] =~ /^([0-9]+):([0-9]+)\z/ or usage();
my ($hours2, $minutes2) = $ARGV[1] =~ /^([0-9]+):([0-9]+)\z/ or usage();
0 <= $hours1 && $hours1 <= 200 or usage("Invalid number of hours for first argument\n");
0 <= $minutes1 && $minutes1 <= 59 or usage("Invalid number of minutes for first argument\n");
0 <= $hours2 && $hours2 <= 200 or usage("Invalid number of hours for second argument\n");
0 <= $minutes2 && $minutes2 <= 59 or usage("Invalid number of minutes for second argument\n");
The range check can be done by regex, but it's error prone and unreadable.
/^0*(0|1[0-9]{0,2}|2(?:00?|[1-9])?|[3-9][0-9]?):0*(0|[1-5][0-9]?|[6-9])\z/
(The regex could be a little simpler, but it's written to virtually eliminate the possibility of backtracking.)
You already asked and we gracefully provided solutions to the math part, so why are you asking again?
my ($hours1, $minutes1) = split /:/, $arg1;
my ($hours2, $minutes2) = split /:/, $arg2;
my $hours = $hours1 + $hours2;
my $minutes = $minutes1 + $minutes2;
$hours += ($minutes - ($minutes % 60)) / 60; $minutes %= 60;
my $days = ($hours - ($hours % 24)) / 24; $hours %= 24;
my $weeks = ($days - ($days % 7)) / 7; $days %= 7;
As for the output part, you should be able to manage on your own. One useful tip:
sprintf('%02d', $minutes) # 0-padded to two digits

#!/usr/bin/perl
die "Erreur Parameters have to be 2" if (scalar(#ARGV) != 2)
if ($ARGV[0] =~ /^([0-9]|1[0-9]?[0-9]|200):([0-5]?[0-9])$/) {
$heures1 = $1;
$minutes1 = $2;
} else {
die "first parameter invalid";
}
if ($ARGV[1] =~ /^([0-9]|1[0-9]?[0-9]|200):([0-5]?[0-9])$/) {
$heures2 = $3;
$minutes2 = $4;
} else {
die "Second parameter invalid";
}
$heures = $heures1 + $heures2;
$minutes = $minutes1 + $minutes2'

Related

Create collection of bitwise matches from an integer range

The OVS documentation
... describes populating rules in the following format:
Range matches can be expressed as a collection of bitwise matches. For
example, suppose that the goal is to match TCP source ports 1000 to
1999, inclusive. The binary representations of 1000 and 1999 are:
01111101000
11111001111
The following series of bitwise matches will match 1000 and 1999 and
all the values in between:
01111101xxx
0111111xxxx
10xxxxxxxxx
110xxxxxxxx
1110xxxxxxx
11110xxxxxx
1111100xxxx
which can be written as the following matches:
tcp,tp_src=0x03e8/0xfff8
tcp,tp_src=0x03f0/0xfff0
tcp,tp_src=0x0400/0xfe00
tcp,tp_src=0x0600/0xff00
tcp,tp_src=0x0700/0xff80
tcp,tp_src=0x0780/0xffc0
tcp,tp_src=0x07c0/0xfff0
I'm trying to determine the correct way to generate those matches based on a minimum and maximum integer value in perl. I looked at the module Bit::Vector , but I wasn't able to figure out how to effectively use it for this purpose.
Let's pretend we trying to solve the equivalent problem for decimal for a second.
Say you want 567 (inclusive) to 1203 (exclusive).
Enlarging phase
You increment by 1 until you have the a multiple of 10 or you would exceed the range.
⇒598 (Creates 597-597)
⇒599 (Creates 598-598)
⇒600 (Creates 599-599)
You increment by 10 until you have a multiple of 100 or you would exceed the range.
You increment by 100 until you have a multiple of 1000 or you would exceed the range.
⇒700 (Creates 600-699)
⇒800 (Creates 700-799)
⇒900 (Creates 800-899)
⇒1000 (Creates 900-999)
You increment by 1000 until you have a multiple of 10000 or you would exceed the range.
[Would exceed limit]
Shrinking phase
You increment by 100 until you would exceed the range.
⇒1100 (Creates 1000-1099)
⇒1200 (Creates 1100-1199)
You increment by 10 until you would exceed the range.
You increment by 1 until you would exceed the range.
⇒1201 (Creates 1200-1200)
⇒1202 (Creates 1201-1201)
⇒1203 (Creates 1202-1202)
Same in binary, but with powers of 2 instead of powers of 10.
my $start = 1000;
my $end = 1999 + 1;
my #ranges;
my $this = $start;
my $this_power = 1;
OUTER: while (1) {
my $next_power = $this_power * 2;
while ($this % $next_power) {
my $next = $this + $this_power;
last OUTER if $next > $end;
my $mask = ~($this_power - 1) & 0xFFFF;
push #ranges, sprintf("0x%04x/0x%x", $this, $mask);
$this = $next;
}
$this_power = $next_power;
}
while ($this_power > 1) {
$this_power /= 2;
while (1) {
my $next = $this + $this_power;
last if $next > $end;
my $mask = ~($this_power - 1) & 0xFFFF;
push #ranges, sprintf("0x%04x/0x%x", $this, $mask);
$this = $next;
}
}
say for #ranges;
We can optimize that by taking advantage of the fact that we're dealing with binary.
my $start = 1000;
my $end = 1999 + 1;
my #ranges;
my $this = $start;
my $power = 1;
my $mask = 0xFFFF;
while ($start & $mask) {
if ($this & $power) {
push #ranges, sprintf("0x%04x/0x%x", $this, $mask);
$this += $power;
}
$mask &= ~$power;
$power <<= 1;
}
while ($end & ~$mask) {
$power >>= 1;
$mask |= $power;
if ($end & $power) {
push #ranges, sprintf("0x%04x/0x%x", $this, $mask);
$this |= $power;
}
}
say for #ranges;
Output:
0x03e8/0xfff8
0x03f0/0xfff0
0x0400/0xfe00
0x0600/0xff00
0x0700/0xff80
0x0780/0xffc0
0x07c0/0xfff0
I attempted to use the very elegant solution provided by #ikegami but found there were edge cases with resulting ports outside of the range or missing ports (e.g. 1-6, 1000-4000, 1000-10000). This alternative approach seems to avoid these issues.
my $LIMIT = 65535;
sub maxPort {
my ($port, $mask) = #_;
my $xid = $LIMIT - $mask;
my $nid = $port & $mask;
return $nid + $xid;
}
sub portMask {
my ($port, $end) = #_;
my $mask = $LIMIT;
my $test_mask = $LIMIT;
my $bit = 1;
my $net = $port & $LIMIT;
my $max_port = maxPort($net, $LIMIT);
while ($net && ($max_port <= $end)) {
$net = $port & $test_mask;
if ($net < $port) {
last;
}
$max_port = maxPort($net, $test_mask);
if ($max_port <= $end) {
$mask = $test_mask;
}
$test_mask -= $bit;
$bit <<= 1;
}
return $mask;
}
sub maskRange {
my ($start, $end) = #_;
my #portMasks;
if (($end <= $start) || ($end > $LIMIT)) {
exit 1;
}
my $mask = $LIMIT;
my $port = $start;
while ($port <= $end) {
$mask = portMask($port, $end);
push #portMasks, sprintf("0x%04x/0x%x", $port, $mask);
$port = maxPort($port, $mask) + 1;
}
return #portMasks;
}
my #ranges = maskRange(1000, 1999);
for (#ranges) {
print("$_", "\n");
}
Outputs:
0x03e8/0xfff8
0x03f0/0xfff0
0x0400/0xfe00
0x0600/0xff00
0x0700/0xff80
0x0780/0xffc0
0x07c0/0xfff0

Majority Element Failing to close cycles

I'm trying to figure out why this keeps printing the "majority element" candidate in every cycle.
The code I've been trying to make work is a Majority Element search (to find an element that is repeated more than half of the length of a list).
I can't separate the processes of finding the candidate and testing against the array because my input is a text file that has an indeterminate number of arrays. It's an exercise from rosalind.info that has different inputs every time you try to solve it.
An example of the input would be
-5 5 5 5 5 5 5 5 -8 7 7 7 1 7 3 7 -7 1 6 5 10 100 1000 1 -5 1 6 7 1 1 10 1
Here's what I've written so far.
foreach my $currentrow (#lists) {
my #row = ();
#row = split( /\s/, $currentrow );
my $length = $#row;
my $count = 0;
my $i = 0;
for $i ( 0 .. $length - 1 ) {
if ( $count == 0 ) {
$candidate = $row[$i];
$count++;
}
if ( ( $count > 0 ) and ( $i = $length - 1 ) ) {
my $counter2 = 0;
for my $j ( 0 .. $length - 1 ) {
if ( $row[$j] == $candidate ) {
$counter2++;
}
}
if ( $counter2 <= ( $#row / 2 ) and ( $i = $length - 1 ) ) {
$candidate = -1;
print $candidate, " ", $i, " ";
}
if ( $counter2 > ( $#row / 2 ) and ( $i = $length - 1 ) ) {
print $candidate, " ", $i, " ";
}
}
if ( $candidate == $row[$i] and $count > 0 ) {
$count = $count + 1;
}
if ( $candidate != $row[$i] and $count > 0 ) {
$count = $count - 1;
}
}
}
Do you have use strict and use warnings 'all' in place?
I imagine that your problem may be because of the test $i = $length - 1, which is an assignment, and should be $i == $length - 1
To find a majority element I would use a hash:
perl -nae '%h=(); $h{$_}+=2 for #F; $h{$_}>#F and print for keys %h; print "\n"'
Each line of input is treated separately. Each line of output matches a line of input and presents its majority element or is empty if there is no such element.
Edit: Now the solution uses autosplit (-a), which is shorter and work not only for numbers.

Subtract months from given date

I have a date in the future, from which I have to subtract 3 months at a time until the current date is reached.
The reached date after subtracting the months must be closest to the current date, but has to be in the future.
The day of the month is always the 23rd
i.e.:
future date = 2015/01/23
current date = 2014/06/05
result = 2014/07/23
I'm running Solaris, so don't have access to GNU date.
I tried to do this in Perl, but unfortunately I can only use the Time::Local module:
#!/bin/ksh
m_date="2019/05/23"
m_year=$(echo $m_date|cut -d/ -f1)
m_month=$(echo $m_date|cut -d/ -f2)
m_day=$(echo $m_date|cut -d/ -f3)
export m_year m_month m_day
perl -MTime::Local -le '
$time = timelocal(localtime);
$i = 3;
while (timelocal(0, 0, 0, $ENV{'m_day'}, $ENV{'m_month'} - $i, $ENV{'m_year'}) > $time) {
print scalar(localtime(timelocal(0, 0, 0, $ENV{'m_day'}, $ENV{'m_month'} - $i, $ENV{'m_year'})));
$i += 3;
}
'
This only works for months within one year. Is there any other way I can do this?
It is simple enough to just split the date strings and do the arithmetic on the fields.
use strict;
use warnings;
use 5.010;
my $future = '2015/01/23';
my $current = do {
my #current = localtime;
$current[3] += 1;
$current[5] += 1900;
sprintf '%04d/%02d/%02d', #current[5,4,3];
};
my $result;
for (my $test = $future; $test gt $current; ) {
$result = $test;
my #test = split /\//, $test;
if (($test[1] -= 3) < 1) {
--$test[0];
$test[1] += 12;
}
$test = sprintf '%04d/%02d/%02d', #test;
}
say $result;
output
2014/07/23
Alternatively you could just do the division to calculate how many whole quarters to subtract, like this
use strict;
use warnings;
use 5.010;
my $future = '2015/01/23';
my #current = (localtime)[5,4,3];
$current[1] += 1;
$current[0] += 1900;
my #future = split /\//, $future;
my $months = ($future[0] - $current[0]) * 12 + $future[1] - $current[1];
$months -= 1 if $current[2] >= 23;
my #result = #current;
$result[2] = 23;
$result[1] += $months % 3;
$result[0] += int(($result[1] - 1) / 12);
$result[1] = ($result[1] - 1) % 12 + 1;
my $result = sprintf '%04d/%02d/%02d', #result;
say $result;
The output is identical to that of the previous code
This is your script changed so it should work across multiple years,
perl -MTime::Local -le'
sub nextm {
$ENV{m_year}--, $ENV{m_month} +=12 if ($ENV{m_month} -= 3) <1;
timelocal(0, 0, 0, $ENV{m_day}, $ENV{m_month}, $ENV{m_year});
}
my $time = timelocal(localtime);
while ((my $c=nextm()) > $time) {
print scalar localtime($c);
}
'
Try something like:
#!/usr/bin/perl -w
# just convert the real date that you have to epoch
my $torig = 1558569600;
my $tnow = time;
# 3 months in seconds to use the epoch everywhere
my $estep = 3 * 30 * 24 * 3600;
while(($torig - $estep) > $tnow){
$torig -= $estep;
}
print $torig,"\n";
print scalar localtime($torig),"\n";
The only problem here is that a months is an approximation, if you need the very same day but minus 3 months, you could use DateCalc
I ended up scripting it all in KSH instead of perl, thanks to Borodin's logic.
#!/bin/ksh
set -A c_date $(date '+%Y %m %d')
IFS=/ d="2019/05/23"
set -A m_date $d
[[ ${c_date[2]} -gt ${m_date[2]} ]] && ((c_date[1]+=1))
c_date[2]=${m_date[2]}
c_date[1]=$(( (((${m_date[0]} - ${c_date[0]}) * 12) + (${m_date[1]} - ${c_date[1]})) % 3 + ${c_date[1]} ))
if [[ ${c_date[1]} -gt 12 ]] ; then
((c_date[0]+=1))
((c_date[1]-=12))
fi
echo ${c_date[#]}

Perl warning: "Found = in conditional, should be ==", but there's no equals sign on the line

I am running the following in Perl v5.12.3 on Mac OS X v10.7.2 (Lion):
#!/usr/local/bin/perl
use strict;
use warnings;
use DBI;
my $db = DBI->connect("dbi:SQLite:testdrive.db") or die "Cannot connect: $DBI::errstr";
my #times = ("13:00","14:30","16:00","17:30","19:00","20:30","22:00");
my $counter = 1;
for (my $d = 1; $d < 12; $d++) {
for (my $t = 0; $t < 7; $t++) {
# Weekend days have seven slots, weekdays
# have only four (barring second friday)
if (($d+4) % 7 < 2 || ($t > 3)) {
$db->do("INSERT INTO tbl_timeslot VALUES ($counter, '$times[$t]', $d);");
$counter++;
# Add 4:00 slot for second Friday
} elsif (($d = 9) && ($t = 3)) {
$db->do("INSERT INTO tbl_timeslot VALUES ($counter, '$times[$t]', $d);");
$counter++;
}
}
}
$db->disconnect;
I get a "Found = in conditional, should be == at addtimes.pl line 16" warning, but there's no equal sign on that line. Also, the loop seems to start at $d == 9. What am I missing?
Line 16:
if (($d+4) % 7 < 2 || ($t > 3)) {
The problem is in your elsif
} elsif (($d = 9) && ($t = 3)) {
^-----------^--------- should be ==
Because the if statement started on line 16, and the elsif is part of that statement, that's where the error got reported from. This is an unfortunate limitation of the Perl compiler.
On an unrelated note, it's much nicer to avoid C-style loops when you can:
for my $d ( 1 .. 11 ) {
...
for my $t ( 0 .. 6 ) {
...
}
}
Isn't that prettier? :)
} elsif (($d = 9) && ($t = 3)) {
This line will assign 9 to $d and 3 to $t. As the warning says, you probably want this instead:
} elsif (($d == 9) && ($t == 3)) {

Help me finish the last part of my app? It solves any Countdown Numbers game on Channel 4 by brute forcing every possibly equation

For those not familiar with the game. You're given 8 numbers and you have to reach the target by using +, -, / and *.
So if the target is 254 and your game numbers are 2, 50, 5, 2, 1, you would answer the question correctly by saying 5 * 50 = 250. Then 2+2 is four. Add that on aswell to get 254.
Some videos of the game are here:
Video 1
video 2
Basically I brute force the game using by generating all perms of all sizes for the numbers and all perms of the symbols and use a basic inflix calculator to calculate the solution.
However it contains a flaw because all the solutions are solved as following: ((((1+1)*2)*3)*4). It doesn't permutate the brackets and it's causing my a headache.
Therefore I cannot solve every equation. For example, given
A target of 16 and the numbers 1,1,1,1,1,1,1,1 it fails when it should do (1+1+1+1)*(1+1+1+1)=16.
I'd love it in someone could help finish this...in any language.
This is what I've written so far:
#!/usr/bin/env perl
use strict;
use warnings;
use Algorithm::Permute;
# GAME PARAMETERS TO FILL IN
my $target = 751;
my #numbers = ( '2', '4', '7', '9', '1', '6', '50', '25' );
my $num_numbers = scalar(#numbers);
my #symbols = ();
foreach my $n (#numbers) {
push(#symbols, ('+', '-', '/', '*'));
}
my $num_symbols = scalar(#symbols);
print "Symbol table: " . join(", ", #symbols);
my $lst = [];
my $symb_lst = [];
my $perms = '';
my #perm = ();
my $symb_perms = '';
my #symb_perm;
my $print_mark = 0;
my $progress = 0;
my $total_perms = 0;
my #closest_numbers;
my #closest_symb;
my $distance = 999999;
sub calculate {
my #oprms = #{ $_[0] };
my #ooperators = #{ $_[1] };
my #prms = #oprms;
my #operators = #ooperators;
#print "PERMS: " . join(", ", #prms) . ", OPERATORS: " . join(", ", #operators);
my $total = pop(#prms);
foreach my $operator (#operators) {
my $x = pop(#prms);
if ($operator eq '+') {
$total += $x;
}
if ($operator eq '-') {
$total -= $x;
}
if ($operator eq '*') {
$total *= $x;
}
if ($operator eq '/') {
$total /= $x;
}
}
#print "Total: $total\n";
if ($total == $target) {
#print "ABLE TO ACCURATELY SOLVE WITH THIS ALGORITHM:\n";
#print "PERMS: " . join(", ", #oprms) . ", OPERATORS: " . join(", ", #ooperators) . ", TOTAL=$total\n";
sum_print(\#oprms, \#ooperators, $total, 0);
exit(0);
}
my $own_distance = ($target - $total);
if ($own_distance < 0) {
$own_distance *= -1;
}
if ($own_distance < $distance) {
#print "found a new solution - only $own_distance from target $target\n";
#print "PERMS: " . join(", ", #oprms) . ", OPERATORS: " . join(", ", #ooperators) . ", TOTAL=$total\n";
sum_print(\#oprms, \#ooperators, $total, $own_distance);
#closest_numbers = #oprms;
#closest_symb = #ooperators;
$distance = $own_distance;
}
$progress++;
if (($progress % $print_mark) == 0) {
print "Tested $progress permutations. " . (($progress / $total_perms) * 100) . "%\n";
}
}
sub factorial {
my $f = shift;
$f == 0 ? 1 : $f*factorial($f-1);
}
sub sum_print {
my #prms = #{ $_[0] };
my #operators = #{ $_[1] };
my $total = $_[2];
my $distance = $_[3];
my $tmp = '';
my $op_len = scalar(#operators);
print "BEST SOLUTION SO FAR: ";
for (my $x = 0; $x < $op_len; $x++) {
print "(";
}
$tmp = pop(#prms);
print "$tmp";
foreach my $operator (#operators) {
$tmp = pop(#prms);
print " $operator $tmp)";
}
if ($distance == 0) {
print " = $total\n";
}
else {
print " = $total (distance from target $target is $distance)\n";
}
}
# look for straight match
foreach my $number (#numbers) {
if ($number == $target) {
print "matched!\n";
}
}
for (my $x = 1; $x < (($num_numbers*2)-1); $x++) {
$total_perms += factorial($x);
}
print "Total number of permutations: $total_perms\n";
$print_mark = $total_perms / 100;
if ($print_mark == 0) {
$print_mark = $total_perms;
}
for (my $num_size=2; $num_size <= $num_numbers; $num_size++) {
$lst = \#numbers;
$perms = new Algorithm::Permute($lst, $num_size);
print "Perms of size: $num_size.\n";
# print matching symb permutations
$symb_lst = \#symbols;
$symb_perms = new Algorithm::Permute($symb_lst, $num_size-1);
while (#perm = $perms->next) {
while (#symb_perm = $symb_perms->next) {
calculate(\#perm, \#symb_perm);
}
$symb_perms = new Algorithm::Permute($symb_lst, $num_size-1);
}
}
print "exhausted solutions";
print "CLOSEST I CAN GET: $distance\n";
sum_print(\#closest_numbers, \#closest_symb, $target-$distance, $distance);
exit(0);
Here is the example output:
[15:53: /mnt/mydocuments/git_working_dir/countdown_solver$] perl countdown_solver.pl
Symbol table: +, -, /, *, +, -, /, *, +, -, /, *, +, -, /, *, +, -, /, *, +, -, /, *, +, -, /, *, +, -, /, *Total number of permutations: 93928268313
Perms of size: 2.
BEST SOLUTION SO FAR: (2 + 4) = 6 (distance from target 751 is 745)
BEST SOLUTION SO FAR: (2 * 4) = 8 (distance from target 751 is 743)
BEST SOLUTION SO FAR: (4 + 7) = 11 (distance from target 751 is 740)
BEST SOLUTION SO FAR: (4 * 7) = 28 (distance from target 751 is 723)
BEST SOLUTION SO FAR: (4 * 9) = 36 (distance from target 751 is 715)
BEST SOLUTION SO FAR: (7 * 9) = 63 (distance from target 751 is 688)
BEST SOLUTION SO FAR: (4 * 50) = 200 (distance from target 751 is 551)
BEST SOLUTION SO FAR: (7 * 50) = 350 (distance from target 751 is 401)
BEST SOLUTION SO FAR: (9 * 50) = 450 (distance from target 751 is 301)
Perms of size: 3.
BEST SOLUTION SO FAR: ((4 + 7) * 50) = 550 (distance from target 751 is 201)
BEST SOLUTION SO FAR: ((2 * 7) * 50) = 700 (distance from target 751 is 51)
BEST SOLUTION SO FAR: ((7 + 9) * 50) = 800 (distance from target 751 is 49)
BEST SOLUTION SO FAR: ((9 + 6) * 50) = 750 (distance from target 751 is 1)
Perms of size: 4.
BEST SOLUTION SO FAR: (((9 + 6) * 50) + 1) = 751
Here is Java applet (source) and Javascript version.
The suggestion to use reverse polish notation is excellent.
If you have N=5 numbers, the template is
{num} {num} {ops} {num} {ops} {num} {ops} {num} {ops}
There can be zero to N ops in any spot, although the total number will be N-1. You just have to try different placements of numbers and ops.
The (((1+1)+1)+1)*(((1+1)+1)+1)=16 solution will be found when you try
1 1 + 1 + 1 + 1 1 + 1 + 1 + *
Update: Maybe not so good, since finding the above could take 433,701,273,600 tries. The number was obtained using the following:
use strict;
use warnings;
{
my %cache = ( 0 => 1 );
sub fact { my ($n) = #_; $cache{$n} ||= fact($n-1) * $n }
}
{
my %cache;
sub C {
my ($n,$r) = #_;
return $cache{"$n,$r"} ||= do {
my $i = $n;
my $j = $n-$r;
my $c = 1;
$c *= $i--/$j-- while $j;
$c
};
}
}
my #nums = (1,1,1,1,1,1,1,1);
my $Nn = 0+#nums; # Number of numbers.
my $No = $Nn-1; # Number of operators.
my $max_tries = do {
my $num_orderings = fact($Nn);
{
my %counts;
++$counts{$_} for #nums;
$num_orderings /= fact($_) for values(%counts);
}
my $op_orderings = 4 ** $No;
my $op_placements = 1;
$op_placements *= C($No, $_) for 1..$No-1;
$num_orderings * $op_orderings * $op_placements
};
printf "At most %.f tries needed\n", $max_tries;