HP Forums
Visibility of a var outside a cycle - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Visibility of a var outside a cycle (/thread-3319.html)



Visibility of a var outside a cycle - salvomic - 03-09-2015 11:59 AM

hi,
I've a code like this one:
Code:

local j, m, tn;
...
FOR j FROM 1 TO m DO
...
tn:= tn + ∑LIST(DL(j));
END;
return tn^2;
...

now, tn is actually 1024 (i.e.), outside the FOR cycle "Return tn" give 1024, but "return tn^2) give 1449616 and not the right value (1048576); "return tn*2" give correctly 2048, "return tn*tn" give also 1449616...

How must I set the variable to give the correct value, also with tn^2 and so on, outside the cycle?

Thanks!

Salvo


RE: Visibility of a var outside a cycle - DrD - 03-09-2015 01:39 PM

(03-09-2015 11:59 AM)salvomic Wrote:  now, tn is actually 1024 (i.e.), outside the FOR cycle "Return tn" give 1024, but "return tn^2) give 1449616 and not the right value (1048576); "return tn*2" give correctly 2048, "return tn*tn" give also 1449616...

How must I set the variable to give the correct value, also with tn^2 and so on, outside the cycle?

Thanks!

Salvo

Code food for thought:

Code:

EXPORT Varb()
BEGIN
  local j, m:=10, tn:=0,DL:={};

  FOR j FROM 1 TO m DO
    DL:=CONCAT(DL,2);  //  Appending 10 two's to reach 1024 via gamma list
  END;

  tn:=ΠLIST(DL);  //  2^10 = 1024
  return tn^2;  //  1024^2 = 1,048,576.00
END;



RE: Visibility of a var outside a cycle - salvomic - 03-09-2015 01:51 PM

(03-09-2015 01:39 PM)DrD Wrote:  Code food for thought:

Code:

EXPORT Varb()
BEGIN
  local j, m:=10, tn:=0,DL:={};

  FOR j FROM 1 TO m DO
    DL:=CONCAT(DL,2);  //  Appending 10 two's to reach 1024 via gamma list
  END;

  tn:=ΠLIST(DL);  //  2^10 = 1024
  return tn^2;  //  1024^2 = 1,048,576.00
END;

always thank you Smile
you're code was ok!
However sorry for my stupid question: I had actually 1204 and I read "1024", so I was so silly confused. Too hurry... Smile

Salvo


RE: Visibility of a var outside a cycle - DrD - 03-09-2015 02:27 PM

Yes, (1024,1204), I was going to mention that, as an interesting 'twist' on the inner digits, but I thought it too obvious to mention. From your code fragment, I couldn't determine what you were actually working with, so I just made up a dummy list (DL) that equates to the coded example, returning your expected result. Within that there may be a useful hint(s) you might find useful to actually accomplish your own objectives.

Over the years, I've grown fond of using lists. In general they are much faster more efficient, and powerful, (for me), to understand than matrices, and I find them to be way easier to work with, even though I have recently completed a couple of courses in linear algebra. While fun to do at the time, trying to remember all the various rules and tools of matrices is a true case of diminishing returns, in my case!

-Dale-


RE: Visibility of a var outside a cycle - salvomic - 03-09-2015 02:40 PM

(03-09-2015 02:27 PM)DrD Wrote:  Yes, (1024,1204), I was going to mention that,...

Over the years, I've grown fond of using lists. ...

-Dale-

yes, Dale, I agree: I love "too much" Linear Algebra and matrices, but with Prime I'm finding out the lists, and they are enough powerful!

I'm using they in my last programs.

Salvo