Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Find the missing number

by oiskuu (Hermit)
on Apr 04, 2017 at 18:18 UTC ( [id://1187031]=note: print w/replies, xml ) Need Help??


in reply to Better way of writing "find the missing number(s)"

Numbers 1..n, one missing.

#! /usr/bin/perl -wl use List::Util qw( sum ); my @arr = (1..3,5..7); print +- sum -1-@arr .. -1,@arr;

Replies are listed 'Best First'.
Re^2: Find the missing number
by pryrt (Abbot) on Apr 04, 2017 at 18:31 UTC

    inspired by your use of sum, the expected sum(1..n) = n*(n+1)/2, so take the found sum from the expected sum:

    use List::Util qw(sum); my @arr=(1..3,5..7); print +(1+@arr)*(2+@arr)/2 - sum(@arr);

    update: clarification = use 1+ and 2+ here because an element is missing from @arr

Re^2: Find the missing number
by prysmatik (Sexton) on Apr 04, 2017 at 20:41 UTC

    That's a well oiled twist.

    Everyone looking at arrays and comparisons when there was a mathematical solution hiding in plain sight.

      ... a mathematical solution hiding in plain sight.

      Perhaps better to say "hiding in obscurity." From the OP (emphasis added):

      ... find a missing number if in a list of numbers. ...

      ...
      my @arr = (19,17,22,23,24,15,16,25..35);
      ...

      But the list in the code example clearly has more than one missing number. See also the (current) thread title. I think the goose chase, although wild, was justified.


      Give a man a fish:  <%-{-{-{-<

        Ahh, I stand corrected. It's funny how user requirements can hinge and swing wildly from a small misunderstanding.

Log In?
Username:
Password:

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

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

    No recent polls found