English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Algorithm-Mystery(n)
//Input:A nonnegative integer n
S<-0
for i<-1 to n do
S<-S+i*i
return S
a. What does this algorithm compute?

2006-08-27 08:18:15 · 4 answers · asked by gurusamy 1 in Computers & Internet Programming & Design

4 answers

Square pyramidal numbers.

2006-08-27 08:38:09 · answer #1 · answered by Seb 2 · 0 0

What Does This Algorithm Compute

2017-01-16 17:13:52 · answer #2 · answered by virgen 4 · 0 0

Well that's just a simple numerical "For Loop" for one.
For i equals less than -1 to n which is a none negative number as already is stated above.

When finished the loop is finished the function will return the calculated value of S.

I don't see S as being valid by less than -0 except for the fact S is defined as a negative number. Assuming n to be a negative number instead for each time the loop decrements (a reversing loop integer count as we're counting down backwards not incrementing upward...) for each pass the value of S should be less than -S + i multiplied by i.

In any event the supplied code sample looks to be incorrect/incomplete in the way it's formatted as it just doesn't look like standard For Loop usage to me. I mean why is there a do at the end of the For Loop syntax?

What programming language is this in btw?

Example:
If S = -1 the first for loop pass, on the second for loop pass the value returned by the function would be a value less than the current S result minus S + minus S which would be now S = -2 multiplied by the current loop integer aka i this loop pass should have a result of -2 as 2 x 1 = 2.

Correct me if I'm wrong.

2006-08-27 08:33:52 · answer #3 · answered by CHINA DOLL GIRL'S HUSBAND {=_=} 2 · 0 0

It's clear: sum of squares of all numbers from -1 to N.
It does not seem to be an "algorithm": i is just a fragment (in pseudocode) of some algorithm.

2006-08-27 09:14:11 · answer #4 · answered by alakit013 5 · 0 0

fedest.com, questions and answers