Re: Mathematical models for loop calculations



Tim Frink said:

On Wed, 26 Sep 2007 13:46:20 +0000, Richard Heathfield wrote:

<snip>

Overall:

a += I;
b = pow(2, I);
c = pow(2, pow(2, I));
d = I * (I + 1) /2 + 1;
e += I;

I just see that these cases work since most of the variables
were assigned the value '2'. If you change them to '3', some
formulas like the one for 'b' will not work any more.

Correct.

Are there also any general formulas that depend on the current
value of the identifiers before entering the loop?

Sometimes, yes. For example, b = b * pow(2, I - 1); (and although I haven't
checked it out, a similar technique probably exists for c). a and e are
the same. d can be done, too, with thought and care. Hint: x + x+1 + x+2 +
x+3 + x+4 + ... + x+n = n * (n + 1) / 2 - x

Other times, no. See my other reply.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: next term
    ... Richard Heathfield ... Google users: ... me@here> cat mapping.dot | dot -Tpng -o mapping.png ...
    (rec.puzzles)
  • Re: Searching in a random list
    ... them at the outset. ... Richard Heathfield ... Google users: ...
    (comp.programming)
  • Re: Whats the position of pointers
    ... will pop into your head, ... Richard Heathfield ... Google users: ...
    (comp.lang.c)
  • Re: new random numbers function
    ... So as long as you don't mind getting a two or a three 90% of the ... Richard Heathfield ... Google users: ...
    (rec.puzzles)
  • Re: An age-old riddle
    ... inscription into its right proportions. ... Richard Heathfield ... Google users: ...
    (rec.puzzles)