Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

I'm sure there's a dead simple way to do this but I can't think of it.

 

I want a calculation, that given a number "x" will return the sum of all integers between 0 and x. I'm only concerned about positive integers.

 

So, Function ( 5 ) will return 5 + 4 + 3 + 2 + 1 = 15. Function ( 6 ) = 21.

 

Any ideas without resorting to a recursive custom function, because elegance.


Ah, Google is my friend. I searched for sum of all previous integers and the first hit had this gem: "1 plus N quantity times N divided by 2."

 

So my function is:

( ( 1+x ) * 2 )  / 2

So my function is:

( ( 1+x ) * 2 )  / 2

 

( x + 1 ) * ( x / 2 ) ?

  • Author

Po-tay-to, po-tah-to.

 
 

Also:  ( x + 1 ) * Floor ( x /2 ) + ( mod ( x ; 2 ) *  Ceiling ( x / 2 ) )

 

Which is a lot easier to do in your head than you may think!

Po-tay-to, po-tah-to.

 

Yes, yes, just poke fun at me …  :sad: there simply was a typo in your post: ( ( 1+x ) * 2 )  / 2 should read ( ( 1 + x ) * x )  / 2

 

… and then I just happen to like symmetrical parentheses better.

Most of those parentheses are not necessary and don't do anything because the multiplication and division are commutative, the order of operation doesn't matter.

 

All you need is any arrangement of:

 

(X+1) * X / 2 

  • Author

Crud, eos, you're right, that was a typo.

 

Parens can make thins clearer...Here's a canard:

 

6 ÷ 2 ( 1+ 2 ) = ?

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.