Running program from Home or Program menu
11-24-2019, 11:06 PM
Post: #1
 pinkman Member Posts: 257 Joined: Mar 2018
Running program from Home or Program menu
Hi,
I’m a bit puzzled by this behavior, I think I’m doing something wrong but I don’t know what.

I created this fabulous program to find the next prime number:

Code:
 EXPORT PRIMTST(NNN) BEGIN  LOCAL NN;  NN:=NNN;  REPEAT   NN:=NN+1;  UNTIL CAS.isprime(NN);  RETURN NN; END;

Two types of execution, two results:
- Home: ok
- Debug: it halts with error on line NN:=NN+1

Notice: when replacing isprime(NN) with NN>20 it works without any error.

Can someone explain?
Thanks,
Thibault

Attached File(s) Thumbnail(s)

11-25-2019, 01:59 AM
Post: #2
 Joe Horn Senior Member Posts: 1,628 Joined: Dec 2013
RE: Running program from Home or Program menu
Interesting! Although 7 doesn't cause that error for me, any input > 996 does error out (if the program is run using the Run soft key). Also, running it from the command line bombs out with very large inputs, e.g. PRIMTST(1E10). No idea why.

<0|ɸ|0>
-Joe-
11-25-2019, 11:24 AM
Post: #3
 pinkman Member Posts: 257 Joined: Mar 2018
RE: Running program from Home or Program menu
This other one is very strange also (still a problem with isprime):

Goal: find Prime numbers starting with 82 and ending with 1. It takes a starting point as argument to avoid counting from 821, but also from 82[any number]1.

Related to: This tweet from Fermat’s library

Expected: halt for each prime number found
Result (home): OK it halts each time a prime number is found
Result (Run softkey): KO it never halts!

Code:
 EXPORT Prime821; EXPORT PRIME82(N) BEGIN  Prime821:=IP(N);  LOCAL A:=ALOG(IP(LOG(Prime821+1)+2)), C;  LOCAL D:=A/10;  REPEAT   Prime821:=Prime821+1;   IF Prime821≥D THEN    A:=A*10;    D:=D*10;   END;   C:=82*A+Prime821*10+1;   PRINT(C);  UNTIL CAS.isprime(C); // OR ISKEYDOWN();  MSGBOX("isprime " + C + "? "+ CAS.isprime(C));  RETURN C;  END;

Platform used: HP Prime beta iOS (version 11-20-219)
I should test on my physical calc (I have a G1), did not have the time yet.
11-25-2019, 03:56 PM (This post was last modified: 11-25-2019 04:16 PM by Gene222.)
Post: #4
 Gene222 Member Posts: 99 Joined: Feb 2015
RE: Running program from Home or Program menu
(11-24-2019 11:06 PM)pinkman Wrote:  Two types of execution, two results:
- Home: ok
- Debug: it halts with error on line NN:=NN+1

Notice: when replacing isprime(NN) with NN>20 it works without any error.

Can someone explain?

I noticed that some advanced functions, like fsolve, sometimes require the variable to be a LOCAL global or EXPORT global variable. Try replacing the "LOCAL NN" with "EXPORT NN" and place the variable declaration on the first line of the code. I have no idea why this sometime works.
11-25-2019, 09:02 PM
Post: #5
 pinkman Member Posts: 257 Joined: Mar 2018
RE: Running program from Home or Program menu
(11-25-2019 03:56 PM)Gene222 Wrote:  I noticed that some advanced functions, like fsolve, sometimes require the variable to be a LOCAL global or EXPORT global variable. Try replacing the "LOCAL NN" with "EXPORT NN" and place the variable declaration on the first line of the code. I have no idea why this sometime works.

Yes it works! Many thanks.
I told Tim to take a look at this thread: even if EXPORTing the var inside the isprime() call is a solution, I think it is a bug.
11-27-2019, 05:40 PM
Post: #6
 Wes Loewer Member Posts: 239 Joined: Jan 2014
RE: Running program from Home or Program menu
(11-24-2019 11:06 PM)pinkman Wrote:  I created this fabulous program to find the next prime number:

Sorry but I have to ask, are you familiar with the nextprime() CAS command?
11-27-2019, 11:16 PM
Post: #7
 pinkman Member Posts: 257 Joined: Mar 2018
RE: Running program from Home or Program menu
(11-27-2019 05:40 PM)Wes Loewer Wrote:
(11-24-2019 11:06 PM)pinkman Wrote:  I created this fabulous program to find the next prime number:

Sorry but I have to ask, are you familiar with the nextprime() CAS command?

Yes for sure! But you are right to ask. In fact, I was looking for primes starting by 82 and ending by 1, like 8221 or 82231.
Brute force algorithm is to increase N in 82N1 and test if it is a prime number.
I noticed the above bug, so I tried to isolate the bug using isprime in this superb algorithm
12-03-2019, 08:46 PM
Post: #8
 pinkman Member Posts: 257 Joined: Mar 2018
RE: Running program from Home or Program menu
Well I think I am now understanding the way it works: because the CAS is quite completely separated from the Home environment, the only way to use a CAS function in a standard (ie. Home) program is to export the used vars to make them seen by the CAS.

I’ve thought, for a long time - and because I did not really use CAS functions in programs, that the CAS functions had access to the memory context of the program. In fact, each function works in it own environment.

It’s not a bug, it’s (unfortunately) designed like this.
Same behavior here.
12-04-2019, 06:03 AM
Post: #9
 cyrille de brébisson Senior Member Posts: 1,000 Joined: Dec 2013
RE: Running program from Home or Program menu
Hello,

There are a lot of interractions possible between Home and Cas...
You can seamlessly call from one to the other, use variables from one to the other...

Now, the devil is in the details...
Cas is fundamentally a "global" scripting system designed to do one task at a time. As such, there is no "namespace", no isolations of various parts... no local variables (or at least they are verry different than normal ones)... This all makes sense when you concider the uses that the CAS was designed for...
Cas is also, by design, symbolic, meaning that a variable there has a different meaning than a "variable" in a programming language (it does not need to be a value, it can be an unknown)...

On the other hand, Home is a separated environment with various "regions" (apps, programs), which are designed to no interfeer with each others so that a student can do (or at least save) multiple work at the same time, and work on one without the other interfeering.
It has a programming language which is much closer to "standard 3rd generations languages" (for example a local variable is only accessible in the function where it is declared, not in a sub function)...

A lot of things have been put into place to allow cross talk of the 2, but sometimes, things are at odds. For example, when should a variable be a "math variable": undefined, or at least unevaluated, or a "programming variable": defined and evaluated...

Unfortunately, it is not always possible to pick it up from the context, or it can be 100% ambiguous... this means that the user is forced to explicitly tell the system what to do, which feels like jumping through hoops :-(

Anyhow, Cas has an Ans, Home has an Ans, they are NOT the same (for various reasons, one of them being that Home and Cas object are not 100% exchangable), so when you use a Cas function in home, then the Cas Ans takes priority over the home one...

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
12-04-2019, 08:09 PM
Post: #10
 rawi Junior Member Posts: 22 Joined: Nov 2019
RE: Running program from Home or Program menu
Hi,

I think it could be that you put in the number with the keyboard. What I found out: If you type the number it is stored not exactly as that number but with a very small deviation which only is important if the function you use needs an integer. I got problems with formatting matrices by simply typing the dimensions. What I did was to add a line: IP(NN+0.5):=NN
By this you can be sure that the number is an integer.
Perhaps this helps.

Best

Raimund Wildner
 « Next Oldest | Next Newest »

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