Post Reply 
How to run an expression in CAS mode in program
06-23-2017, 12:49 PM
Post: #1
How to run an expression in CAS mode in program
I have a program I'm running and I'm having a problem getting a line in the program to run in CAS mode. Breaking the line down to it's simplest form is like this...
t:=(a^(n-1)) MOD n;
...where a = 503, n = 29.
When I single step through my program, I get that t = 6 after this line is executed (wrong answer).
Now, when I just go to the home view and run the following, (503^28) MOD 29;
...the result is 6.

When I press the CAS button to put it in CAS view and execute the same line, I get a result of 1 (correct result).

I know WHAT the problem is. It's that in CAS mode, I have a resolution of 2500 digits or so, but in home view it limits it to 13 digits or whatever it is....giving me the wrong answer.
How do I force it to run the line (t:=(a^(n-1)) MOD nWink in CAS mode while in a program?
I tried pressing the CAS button before entering the program debug and single stepping, but it gives me 6 every time.
Is it simply the matter of entering CAS.MOD instead of MOD or something like that?
I guess I could create a separate CAS program (with the CAS checkbox checked) and put a function in there that executes that line, and call it from the other program...would that work?
The problem is that I am unsure if it's wise to just check the CAS box in my program as I have multiple programs under the one name and some of these need to run in home view.
Another thought is that I can create a #cas #end block in the same program...without checking the CAS box....is that allowed?
I know I can try all of these options, but that will require my rewriting the code multiple times for each of these options. If someone knows the answer, they could save me a lot of time....I'd be grateful for anyone who knows the solution to this problem.
Thanks in advance.
-Donald
Find all posts by this user
Quote this message in a reply
06-23-2017, 02:01 PM
Post: #2
RE: How to run an expression in CAS mode in program
I'd write the whole program as a CAS program (personal preference), but try powmod(a,n-1,n) instead of MOD. Seems to work fine in Home also.
Find all posts by this user
Quote this message in a reply
06-23-2017, 02:45 PM
Post: #3
RE: How to run an expression in CAS mode in program
(06-23-2017 12:49 PM)webmasterpdx Wrote:  How do I force it to run the line ( t:=(a^(n-1)) MOD n; ) in CAS mode while in a program?

You can pass the expression as a string to the CAS like that:
Code:
EXPORT test()
BEGIN
  LOCAL a,n,t;
  a:=503; n:=29;
  t:=CAS("(a^(n-1)) MOD n");
END;
Find all posts by this user
Quote this message in a reply
06-23-2017, 05:18 PM
Post: #4
RE: How to run an expression in CAS mode in program
There once was a thread concerning MOD, you perhaos should look at :
(08-21-2015 11:09 AM)parisse Wrote:  MOD was implemented inside the CAS for the first version of the 39GII, it was not meant to be used with exact data, the CAS function for integer remainder is irem, and here the right function is powmod(7,23,143) because computing 7^23 before doing remaindering is much much less efficient.
I will add integer checking for arguments inside MOD.

which here then can be used : powmod(a,n-1,n)
Arno
Find all posts by this user
Quote this message in a reply
06-23-2017, 09:24 PM
Post: #5
RE: How to run an expression in CAS mode in program
Some great suggestions here. BTW, FYI on how g^aMODp is normally calculated (is used a lot in key exchange in encryption algorithms like Diffie Hellman). The algorithm implementation uses the property that
axbMODc = (aMODc x bMODc)MOD c
and
g^2MODp = (gMODp)^2 MOD p
This way, the g^a term never has to fit in a number greater than p. Is a clever trick used in such implementations.
Thanks for all the help.
-Donald
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)