how to get 1 to 500 odd numbers sum in matlab?

how to get 1 to 500 odd numbers sum in matlab?

2 comentarios

Jan
Jan el 7 de Mayo de 2016
Because this is a homework question and the OP did not show any own effort, I suggest to accept Azzi's answer.
Image Analyst
Image Analyst el 7 de Mayo de 2016
It wasn't tagged as homework and he didn't say it was homework, but it sounds a lot like it could be. That's always the dilemma : if it sounds and looks like homework but they didn't say so explicitly, do you (1) assume it's homework, ask them, and just give hints, or (2) assume it's not homework and give the full solution which means they would be plagiarizing if they used it as their homework answer? No clear answer. I will variously do both 1 and 2 on different questions depending on what I feel like at the time. Often times (2) happens and then panicky posters come back and obliterate their questions and comments which angers the rest of us because it makes us feel like we wasted our time.

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 7 de Mayo de 2016
Do you mean like this:
theSumOfOddNumbers = sum(1:2:500)
Roger Stafford
Roger Stafford el 7 de Mayo de 2016
sum is (1+499)*250/2
Note: Legend has it that ten-year-old Carl Friedrich Gauss was given a similar problem by his instructor. Instead of tediously adding up all the terms, he imagined that a like series in reverse order was matched up with the given series, and the matched pairs would thus always add up to the same value, so the problem was therefore transformed into one of simple multiplication and then division by two. Your problem is like that.

1 comentario

Jan
Jan el 7 de Mayo de 2016
While Image Analyst's solves the problem "in Matlab" as requested, this answer focusses on how a programmer can do the main part by thinking and Matlab has to solve a tiny calculation also. If the programmer can even multiply 500*125, he could use at least the cardboard box the Matlab DVDs are shipped in to sit on - but unfortunately modern Matlab versions are delivered by downloads.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 7 de Mayo de 2016

Comentada:

el 7 de Mayo de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by