DEV Community

Sum All Numbers in a Range

Mohammed Awad on May 10, 2023

DESCRIPTION: We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. T...
Collapse
Β 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’ β€’ Edited

My approach, based on the fact that the sum from 0β†’n is equal to (n/2)(n+1):

const sumAll =
  (arr, [a,b]=[...arr].sort((c,d)=>c-d))=>(b*b-a*a+b+a)/2
Enter fullscreen mode Exit fullscreen mode

(Could be made shorter by not copying the original array, but I wanted to avoid mutation)

Collapse
Β 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’ β€’ Edited

With mutation (and more code golf):

const sumAll=(a,[b,c]=a.sort((d,e)=>d-e))=>(c*c-b*b+c+b)/2
Enter fullscreen mode Exit fullscreen mode
Collapse
Β 
xmohammedawad profile image
Mohammed Awad β€’

thank you, Relly for your interest!

Collapse
Β 
donnywi profile image
donnywi β€’

This works fast too when you pass in:
sumAll(7, 80000)

function sumAll(arr) {
    return ( (arr[0]+arr[1]) * (Math.abs(arr[0]-arr[1])+1) ) /2 ;
}

Enter fullscreen mode Exit fullscreen mode
Collapse
Β 
xmohammedawad profile image
Mohammed Awad β€’

I'm suck at Math; I hope to be become good like that someday.

Collapse
Β 
donnywi profile image
donnywi β€’

I think you're awesome at Javascript, and I need to learn how to use fill(), map(), and reduce() properly. I'm new at Javascript, so your posts are very inspiring.

Thread Thread
Β 
xmohammedawad profile image
Mohammed Awad β€’

Relly, glad to hear that. you can check freecodecamp.com to learn more

Collapse
Β 
syeo66 profile image
Red Ochsenbein (he/him) β€’

Try and be amazed:

function sumAll(arr) {
  const minNumber = Math.min(...arr);
  const maxNumber = Math.max(...arr);
  return (maxNumber*(maxNumber+1)/2) - ((minNumber-1)*minNumber/2);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
Β 
xmohammedawad profile image
Mohammed Awad β€’

this is a pattern ?

Collapse
Β 
donnywi profile image
donnywi β€’

I belive that's just a basic math equation.

Thread Thread
Β 
xmohammedawad profile image
Mohammed Awad β€’

Yeah, but I mean is it design pattern.

Thread Thread
Β 
syeo66 profile image
Red Ochsenbein (he/him) β€’

Nah. It's math. The basic idea is that you can calculate the sum of all integers from 1 to n using n*(n+1)/2

Thread Thread
Β 
xmohammedawad profile image
Mohammed Awad β€’

I need to focus on math and learn how use it because better with BigO

Collapse
Β 
link2twenty profile image
Andrew Bone β€’
function sumAll(arr) {
  const [a, l] = arr.sort((a, b) => a - b);

  return (l - a + 1) * (a + l) / 2;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
Β 
xmohammedawad profile image
Mohammed Awad β€’

Any tips or edit are most welcome. share it with me on the comments. Thanks for being here!

Collapse
Β 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’ β€’ Edited

Another one (no mutation):

const sumAll = ([m,n],[a,b]=m>n?[n,m]:[m,n])=>(b*b-a*a+b+a)/2
Enter fullscreen mode Exit fullscreen mode
Collapse
Β 
xmohammedawad profile image
Mohammed Awad β€’

great but it's seemed little complex, right? I more into simple solutions

Collapse
Β 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’ β€’ Edited

It's interesting you would say that, as your solution is actually the most complex (timewise) here at O(N). The simplest I can now think of is a small reworking of @donnywi solution (which in itself is a slight mathematical simplification of others here):

const sumAll = ([a, b]) => (a+b) * (Math.abs(a-b)+1) / 2 
Enter fullscreen mode Exit fullscreen mode
Thread Thread
Β 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’

I suspect the way I wrote my code in posts here may have made it look more complex than it is, as it doesn't really explain itself. Sorry, my brain often tends to work in terse, code-golf like code.

Thread Thread
Β 
xmohammedawad profile image
Mohammed Awad β€’

yes, I know my code was the worst, but it easier to read than yours, I should study the math section again, because I didn't Relly use Math methods expect min, max and random.

Thread Thread
Β 
donnywi profile image
donnywi β€’

This is a good use of the arrow function. πŸ‘