Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.4K Views
|
9 Replies
|
5 Total Likes
View groups...
Share
Share this post:
GROUPS:

How can I obtain the same result without using any loop?

I have a piece of code as below:

For[i = 1; t = x, i^2 < 10, i++,
 t = t^2 + i; Print[t]
 ]

and the result is:

1+x^2
2+(1+x^2)^2
3+(2+(1+x^2)^2)^2

How can I convert it into function program type? That means obtaining the same result without any loop in your code. Could you help me? Thanks a lot!

POSTED BY: Kui Chen
9 Replies

@Sander Huisman sorry I am confused about the issue, not sure I understand what happened. I will run this by our team to see if they can help, if you can please give a bit more details.

POSTED BY: Vitaliy Kaurov

Posts on Community appear with some delay relatively to email notification. But any reply will be there in a mater of minutes, rarely within a bit longer time if site is loaded. We are trying to improve this. But posts eventually will appear always.

POSTED BY: Vitaliy Kaurov

@Vitaliy Kaurov And again! I got two emails, for his replies. Now somehow it sets THIS post to a copy of his post, and ignores my message.... (this is an edit)

POSTED BY: Sander Huisman

Thank you very much! I am now studying Mathematica and want to use it in my mathematics research (especially machine learning and numerical analysis). Thank you again!

POSTED BY: Kui Chen

Thank you very much! I am now studying Mathematica and want to use it in my mathematics research (especially machine learning and numerical analysis). Thank you again!

POSTED BY: Kui Chen

@Erik Mahieu @Vitaliy Kaurov

I got an email from your reply, but don't see it here yet, also in an incognito tab, there seems to be something wrong with the Wolfram Community...

EDIT: now that I posted this, yours becomes visible!

POSTED BY: Sander Huisman
Posted 10 years ago

No problem, Sander. I just opened the community site this morning and saw this easy question. I admire and follow your valuable contributions to this site and I am learning a lot this way. Thanks...

POSTED BY: Erik Mahieu

Hi Kui Chen,

There are probably multiple ways one can do it, using NestList or other functions. I guess FoldList is the easiest in this case:

FoldList[#1^2 + #2 &, x, Range[1, Sqrt[10]]]

this will however not print it, but rather store it in a list. You could add Print/@% on the next line to print each of them.

aagh! I didn't see Erik's answer.

POSTED BY: Sander Huisman
Posted 10 years ago

One of the many ways to do this with Mathematica, is using FoldList:

FoldList[#1^2 + #2 &, x, Range[3]] 

Add Rest and Column to get your layout:

FoldList[#1^2 + #2 &, x, Range[3]] // Rest // Column
POSTED BY: Erik Mahieu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard