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.