/    Sign up×
Community /Pin to ProfileBookmark

Hello everyone,

i was wondering if you guys could help me out with an exercise i have to do for school. I have been trying to do it for the past 3 weeks now and i still haven’t succeeded.

This is the exercise:

You need to take 2 numbers, a lowest number and a highest number. This could be any number, entered by the user. For example, lowest number is 23 and the highest number is 29. To get the sum, you need to do 23+24+25+26+27+28+29. If someone could help me out, that would be amazing!

Thanks,

Niels_L.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoJan 06.2015 — Three WEEKS?!? Can we see what you have come up with so far?
Copy linkTweet thisAlerts:
@Troy_IIIJan 06.2015 — This is my personal algo.:

for x=23, y=29;

x -> y = 23,24,25,26,27,28,29;

x -> y sum = 23+24+25+26+27+28+29;

sum (x -> y) = 182.

[B]function sum_range(x,y){/* b.b. Troy III p.a.e. */ return( 1 + Math.abs( x - y ) ) * ( x + y ) / 2; }[/B]

sum_range(23,29);
> 182

I never had it in mind but I think it will work with negative numbers also:

say: sum_range(-2,3);
> 3

and the order of the range specification has been neutralized therefore you can also pass the values in reverse order like:

sum_range(29,23); //and get the correct result
> 182 // instead of say -182 or get some error instead.

The problem is - you will not be able to show this to your professor without getting in trouble.
Copy linkTweet thisAlerts:
@007JulienJan 07.2015 — You can write the sum in order or in reverse order :

S = 23+24+25+26+27+28+29

S = 29+28+27+26+25+24+23

The each sum is the same 23+29 = 24+28 = 25+27 = 26+26 and the sum is (23+29) x (number of terms) / 2

Then from n to m. the sum is (n+m)x(1+Math.abs(m-n))/2 !

From 1 to 100 the sum is 101 x 50 = 5050
Copy linkTweet thisAlerts:
@Troy_IIIJan 08.2015 — oh really?
Copy linkTweet thisAlerts:
@nap0leonJan 08.2015 — If you don't want to use Gauss' formula (fully explained here - http://mathcentral.uregina.ca/QQ/database/QQ.02.06/jo1.html),

just do a simple "for" loop (explained here: http://www.w3schools.com/js/js_loop_for.asp)
Copy linkTweet thisAlerts:
@Troy_IIIJan 09.2015 — Nope, this is Troy III's original formula.

Gauss doesn't cover arbitrary segments of ranges like:

{89 — 98 ) or { 1037 — (-17) } and likewise not { ( -1037) — 784 }.

You'll need to dig more, because I didn't and I didn't have to, - because I simply wrote the formula and the js function that works in advanced and user friendly manner. So there's a great possibility that you will come back empty-handed. Because the guy asking the question has had quite some time at hand (3 months he says) and did not find this formula. Otherwise he wouldn't ask.
×

Success!

Help @Niels_L spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...