6
$\begingroup$

How to get following loop output using pure functions such as NestList?

For[i = 0; v = 0, i < 4 && v < 4, i++; v++,
Print[x^i + b^v]]

and the output is

2

b+x

b^2+x^2

b^3+x^3
$\endgroup$
2
  • $\begingroup$ Try MapThread[Function[{i, v}, Print[x^i + b^v]], {Range[0, 3], Range[0, 3]}] $\endgroup$
    – Coolwater
    Jul 19, 2019 at 10:24
  • $\begingroup$ @CoolwaterThanks $\endgroup$
    – a019
    Jul 19, 2019 at 10:33

2 Answers 2

12
$\begingroup$
#^Range[0, 3] & /@ (x + b)

x^# + b^# &@Range[0, 3]
$\endgroup$
2
$\begingroup$

Just a generalization (of many possible):

fun[u_, n_] := Plus @@ (u^#) & /@ Range[0, n]

For example:

fun[{x,b},6]

yields:

{2, b + x, b^2 + x^2, b^3 + x^3, b^4 + x^4, b^5 + x^5, b^6 + x^6}

or

fun[{a,b,c},5]

{3, a + b + c, a^2 + b^2 + c^2, a^3 + b^3 + c^3, a^4 + b^4 + c^4, a^5 + b^5 + c^5}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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