How to run an expression in CAS mode in program - 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: How to run an expression in CAS mode in program ( /thread-8551.html) |

How to run an expression in CAS mode in program - webmasterpdx - 06-23-2017 12:49 PM
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 n 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 RE: How to run an expression in CAS mode in program - Helge Gabert - 06-23-2017 02:01 PM
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. RE: How to run an expression in CAS mode in program - Didier Lachieze - 06-23-2017 02:45 PM
(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()` RE: How to run an expression in CAS mode in program - Arno K - 06-23-2017 05:18 PM
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. which here then can be used : powmod(a,n-1,n) Arno RE: How to run an expression in CAS mode in program - webmasterpdx - 06-23-2017 09:24 PM
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 |