1
$\begingroup$

How do I use the Sum function for adding and squaring consecutive terms? It's an exercise from Wolfram Challenges.

I want to write my own function that uses the Sum function to get the sum of (1+2)^2 if the user inputs a two. Or if the user inputs a three, then ((1+2)^2+3)^2).

Don't tell me the answer! A hint would suffice for the sake of a learning experience.

Also what kind of math should I look at to understand the soul of the exercise??

update!

Last@FoldList[Plus, 1, {a}]^2 

func[b_] := Last@FoldList[Plus, b, {a + 1}]^2 

Nest[func, Last@FoldList[Plus, 1, {a}]^2, 2] 

(1 + a + (1 + a + (1 + a)^2)^2)^2 

I wanted that leftmost one to be a 2.

$\endgroup$
  • 1
    $\begingroup$ I think Sum[] is not going to work. Use the functions Fold[] and Range[] instead. $\endgroup$ – Somos Apr 28 at 0:55
  • $\begingroup$ Does the exercise actually require the use of Sum? $\endgroup$ – m_goldberg Apr 28 at 4:49
  • $\begingroup$ @m_goldberg no the excercise does not require Sum. that was my first guess at solving it via wolfram language. however now i think fold or foldlist or nest is the way. $\endgroup$ – Jonathan Silverman Apr 28 at 4:56
  • $\begingroup$ @Somos Thanks for your advice. I have made an update to my question using different functions. $\endgroup$ – Jonathan Silverman Apr 28 at 5:03
2
$\begingroup$

You say you want hints rather than an answer, so I will try give some. It would actually be easier to give the answer to such a simple problem.

Hints

  • Use Fold with two arguments, a pure function as its 1st argument and a list as its 2nd.
  • The list should be generated by Range; e.g., Range[3] to get the result when func is given 3.
  • The pure function should be written in the form (previous_result + item_from_list)^2.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.