Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7848 > unrolled thread
| Started by | Rex <aoirex@gmail.com> |
|---|---|
| First post | 2012-01-15 09:52 +0000 |
| Last post | 2012-01-17 08:42 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
How to simplify this code with variable number of nesting loops? Rex <aoirex@gmail.com> - 2012-01-15 09:52 +0000
Re: How to simplify this code with variable number of nesting loops? Ray Koopman <koopman@sfu.ca> - 2012-01-17 08:42 +0000
| From | Rex <aoirex@gmail.com> |
|---|---|
| Date | 2012-01-15 09:52 +0000 |
| Subject | How to simplify this code with variable number of nesting loops? |
| Message-ID | <jeu7lb$j7s$1@smc.vnet.net> |
Considering an array a[i], i=0,1,...,g, where g could be any given
number, and a[0]=1.
for a[1]=a[0]+1 to 1 do
for a[2]=a[1]+1 to 3 do
for a[3]=a[2]+1 to 5 do
......
for a[g]=a[g-1]+1 to 2g-1 do
###some computations here###
The problem is that everytime we change the value of g, we need to
modify the code, those loops above. So this is not a good code.
Any advice would be greatly appreciated.
[toc] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2012-01-17 08:42 +0000 |
| Message-ID | <jf3c9g$nhf$1@smc.vnet.net> |
| In reply to | #7848 |
On Jan 15, 1:52 am, Rex <aoi...@gmail.com> wrote:
> Considering an array a[i], i=0,1,...,g, where g could be any given
> number, and a[0]=1.
>
> for a[1]=a[0]+1 to 1 do
> for a[2]=a[1]+1 to 3 do
> for a[3]=a[2]+1 to 5 do
> ......
> for a[g]=a[g-1]+1 to 2g-1 do
> ###some computations here###
>
> The problem is that everytime we change the value of g, we need to
> modify the code, those loops above. So this is not a good code.
>
> Any advice would be greatly appreciated.
g = 4; Clear[a]; a[0] = 0 (* not 1 *);
Do[ Print[a /@ Range@g] (* or some other computations *),
Evaluate[ Sequence@@Table[{a[j],a[j-1]+1,2j-1},{j,g}] ] ]
{1,2,3,4}
{1,2,3,5}
{1,2,3,6}
{1,2,3,7}
{1,2,4,5}
{1,2,4,6}
{1,2,4,7}
{1,2,5,6}
{1,2,5,7}
{1,3,4,5}
{1,3,4,6}
{1,3,4,7}
{1,3,5,6}
{1,3,5,7}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web