Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: max of N numbers?

by TedPride (Priest)
on May 27, 2006 at 20:04 UTC ( [id://552029]=note: print w/replies, xml ) Need Help??


in reply to max of N numbers?

There is no way to find the max of N numbers without using comparison operators. However, you can just add a short function to your code for finding max:
use strict; use warnings; my @arr = (2,7,12,5,17,9); print max(@arr); sub max { my $max = $_[0]; do { $max = $_[$_] if $max < $_[$_]; } for 1..$#_; $max; }
This takes linear time. Or you can use a sort, which takes O(n lg n) time:

my $max = (sort { $b <=> $a } @arr)[0];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-24 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found