Question: Geometric sequence sum code issue

Hi everyone! I meet some code issue when editing a easy sum of geometric sequence as belows, I can write another procedure sum total, it can run. But I wanna also try the sequence (the middle code) but failed, and why nops can not work? Thanks!

NULL

myfac := proc (n::nonnegint) local out, i, a, q; a := [3]; q := 2; out := a[1]; for i from 0 to n do out := out*q^i end do; out end proc; myfac(4)

3072

(1)

sum(myfac[i], i = 0 .. nops(myfac))

myfac[0]+myfac[1]

(2)

sum(myfac[i], i = 0 .. 4)

myfac[0]+myfac[1]+myfac[2]+myfac[3]+myfac[4]

(3)

values := [seq(myfac(i), i = 0 .. nops(myfac))]; sum_values := sum(myfac(i), i = 0 .. nops(myfac)); values; sum_values

[3, 6]

 

Error, invalid input: myfac expects its 1st argument, n, to be of type nonnegint, but received i

 

[3, 6]

 

sum_values

(4)

sum_myfac := proc (n::nonnegint) local i, total; total := 0; for i from 0 to n do total := total+myfac(i) end do; total end proc; sum_myfac(3)

225

(5)

NULL

Download sum_of_geometric_sequence.mw

Please Wait...