Post Reply 
[VA] Short & Sweet Math Challenges #23: "May the 4th Be With You !" Special
05-09-2018, 10:53 PM
Post: #7
RE: [VA] Short & Sweet Math Challenges #23: "May the 4th Be With You !" ...
      
Hi all, and hi J-F:

To all, just a brief reminder that there's still 24 hours left to submit any further solutions to some or all of the 6 Steps of this challenge before I post my original solutions tomorrow, as stated in my original post.

As I did in previous challenges, I'll post my original solutions and comments only for those Steps for which code for one or more solutions (or attempted solutions) has been posted by the readers. The ones which receive no inputs will best be left for future use.

To J-F:

J-F Garnier Wrote:Here is my solution in two command lines and about 100 characters:

The first time I tried it, it gave me a "Subscript" error because your first line assigns a value to A(0) and I had OPTION BASE 1 set at the time.

This must be addressed, which is why I (nearly) always include DESTROY ALL as the first statement in any program or command line, followed by OPTION BASE if I use arrays. Adding those two statements to your first command line makes your solution about 15 characters longer or so. No big deal.

Your final >X to display the computed root can be included at the end of your second command line, it still fits, so you don't need a third mini-command-line just for it.

J-F Garnier Wrote:It is very difficult (or impossible) to make a loop from the keyboard command line and include tests in it.

It's difficult but not impossible, it can be done as long as it all fits in a 96-char line. For instance, to search for and display the very first 4-digit square that includes "444", you could issue a command line like this:

>FOR X=32 TO 99 @ X=X+100*(POS(STR$(X*X),"444")#0) @ NEXT X @ DISP X-101;RES^2

      38      1444

As you can see, it doesn't loop all the way to 99\(^2\) but stops looping at the first square which fulfills the test condition and displays both the number and its square.

In a similar fashion, to search for the very first 4-digit "square plus one" that happens to be a prime number, this command line would do it:

>FOR X=32 TO 99 @ X=X+100*(PRIM(X*X+1)=0) @ NEXT X @ DISP X-101,RES^2+1

      36      1297

This last example uses the PRIM function from the JPC ROM, which also includes structures that can be used to implement this functionality. For instance, the last example could be rewritten like this:

>X=31 @ REPEAT @ X=X+1 @ UNTIL PRIM(X*X+1)=0 OR X>99 @ DISP X,X*X+1

      36      1297

which works as well except for the fact that if no X value would produce a prime then the last value (100) would be output instead , while the first version would output "-1", perhaps better indicating the not-found condition.

Best regards.
V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] Short & Sweet Math Challenges #23: "May the 4th Be With You !" ... - Valentin Albillo - 05-09-2018 10:53 PM



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