Post Reply 
[VA] Short & Sweet Math Challenge #22: April 1st, 2018 Spring Special
04-06-2018, 04:08 AM
Post: #26
[VA] Short & Sweet Math Challenge #22: April 1st, 2018 Spring Special - My Solutions
 
Hi all,

As always, thank you very much for the high degree of participation in my SSMC#22 April 1st 2018 Spring Special and most importantly the high quality of your various inputs, whether individual or "as a team" (I like the concept !), which indeed managed to find the correct solutions to all parts of it, sometimes actually producing my exact original solution and at other times producing an equivalent variation thereof.

These are my original solutions plus assorted comments:

Main Course:

This is my original version for the HP-71B, an UDF (User-Defined Function, 2 lines, 72 bytes) which accepts as argument the number whose primality or compositeness we want to know (as a string, to cater with inputs more than 12 digits long) and outputs the number's answer to that question.

      1 DEF FNS$(N$) @ S$="" @ FOR I=1 TO LEN(N$) STEP 2
      2 S$=S$&CHR$(VAL(N$[I,I+1])) @ NEXT I @ FNS$=S$ @ END DEF


Speaking of which, there are any number of powerful algorithms to check a number for primality but I think that my approach is quite novel, namely:

      "Why not ask the number itself if it's prime or composite ? Surely it should know !".

The above UDF implements just that approach. Let's see how it fares with the six test numbers:

      >FNS$("8082737769637879")

            PRIME?NO                              {correct, composite divisible by 5701}

      >FNS$("89698373657765787367698082737769")

            YESIAMANICEPRIME                      {correct, nice or not it's indeed a prime}

      >FNS$("677977807983738469788577666982")

            COMPOSITENUMBER                       {obviously composite}

      >FNS$("7365778082737769847979")

            IAMPRIMETOO                           {correct, it's a prime}

      >FNS$("677977807983738469658387697676")

            COMPOSITEASWELL                       {obviously composite}

      >FNS$("7378686969688082737769")

            INDEEDPRIME                           {correct, it's a prime}

Of course these are not the only numbers who truthfully answer when asked, there are billions and billions which will obligue as well, for instance:

      FNS$("83797769328082737769")              -> SOME PRIME         { correct, it's a prime }
      FNS$("667371328082737769")                -> BIG PRIME          { ditto }
      FNS$("65768379328082737769")              -> ALSO PRIME         { ditto }
      FNS$("73397765808273776949484837")        -> I'MAPRIME100%      { ditto }
      FNS$("73657778798480827377698379828289")  -> IAMNOTPRIMESORRY   { composite, divisible by 43^2 }
      FNS$("677977807983738469")                -> COMPOSITE          { ditto, divisible by 79 }
      FNS$("78798467797780798373846933333333")  -> NOTCOMPOSITE!!!!   { correct, it's an enthusiastic prime }
      FNS$("91808273776993")                    -> [PRIME]            { correct, it's a prime }
      FNS$("787932837989328082737779")          -> NO SOY PRIMO       { Spanish composite, div. by 2663 }
      FNS$("80827377696332787933")              -> PRIME? NO!         { composite, divisible by 3 }
      FNS$("687386738373667669668951")          -> DIVISIBLEBY3       { ditto }

Other numbers do care to answer but it seems they're not that sure about their own status:

      FNS$("7879843283858269")                  -> NOT SURE           { composite, divisible by 9601 }
      FNS$("8773837232733275786987")            -> WISH I KNEW        { ditto , divisible by 7669 }
      FNS$("8979853284697676327769")            -> YOU TELL ME        { ditto , divisible by 3 }
      FNS$("87727932676582698363")              -> WHO CARES?         { ditto , divisible by 3 }
      FNS$("6669658483327769")                  -> BEATS ME           { ditto , divisible by 17 }

Also, still others do not even care but even seem to resent being asked and reply rudely:

      FNS$("7669658669327769326576797869")      -> LEAVE ME ALONE     { composite, divisible by 3 }
      FNS$("7179326587658933")                  -> GO AWAY!           { ditto, divisible by 367651 }
      FNS$("6669658432738433")                  -> BEAT IT!           { ditto, divisible by 11 }
      FNS$("83727979443283727979")              -> SHOO, SHOO         { ditto, divisible by 3 }

And finally, the worst offenders of all, some numbers do reply but only to lie shamelessly through their teeth !

      FNS$("65787984726982677977807983738469")  -> ANOTHERCOMPOSITE   { such liar ! ... you're a prime ! }

To be honest, the vast majority seem to be under the influence or something because they reply with gibberish when asked but I won't give any examples here as they're quite common and so pretty easy to find.

All in all, I'd say my groundbreaking, novel primality check it's a great success, don't you think ? ... XD

Also, this is my RPN version for the HP42S (a program, 21 steps, 39 bytes, no numbered registers or variables)

      00 { 39-Byte Prgm }
      01 LBL "P?"
      02 "N?"
      03 AON
      04 PROMPT
      05 ALENG
      06 2
      07 /
      08 LBL 00
      09 ATOX
      10 10
      11 X
      12 ATOX
      13 +
      14 528
      15 -
      16 XTOA
      17 Rv {roll down}
      18 DSE ST X
      19 GTO 00
      20 AVIEW
      21 END

XEQ "P?"

      N?
            8082737769637879 [R/S] -> PRIME?NO
XEQ "P?"

      N?
            89698373657765787367698082737769 [R/S] -> YESIAMANICEPRIME
etc.

Dessert 1:

Shortest is (also it uses no digits, strings, or functions):

      1 DISP MAXREAL*EPS (8 bytes, 8-5 = 3 bytes for the expression itself)


Other less efficient possibilities that people might try:

      1 DISP 9.99999999999       (14 bytes, 9 bytes for the expression)
      1 DISP 10-1E-11            (12 bytes, 7 bytes for the expression)
      1 DISP RAD(DEG(-INX))-OVF  (12 bytes, 7 bytes for the expression, also no digits)
      1 DISP NEIGHBOR(10,0)      (12 bytes, 7 bytes for the expression)
      1 DISP 10/3*3              (11 bytes, 6 bytes for the expression)
      1 DISP MAXREAL/1E499       (10 bytes, 5 bytes for the expression)
      1 DISP RAD(DEG(6))+4       (10 bytes, 5 bytes for the expression)

Dessert 2:

Shortest are (all of them 6 bytes):

      >RAD(OVF*UNF*OVF)

            -3.14159265359 {-Pi}

      >RAD(OVF*UNF%OVF)

            -3.14159265359E-2 {-Pi/100}

      >RAD(OVF%UNF%OVF)

            -3.14159265359E-4 {-Pi/10000}


and their various permutations. As I said, RAD is not a trigonometric *function*, as it's merely multiplication by a conversion factor and so it's perfectly legal, while ANGLE *is* a trigonometric function (a variant of the arctangent function) and so not legal for this challenge.

Dessert 3:

Some people offered valid F(X) and G(X) but the original I had in mind is the simple pair TANH(X) and ATANH(X). For these functions we have:

- For the HP-71B:

            X      ATANH(TANH(X))    % Error
      ------------------------------------------
      10.000000      10.000037      0.000373 %
      11.000000      10.999905     -0.000867 %
      14.000000      14.162084      1.157744 %
      14.100000      14.162084      0.440313 %
      14.200000      14.162084     -0.267013 %
      14.300000      14.162084     -0.964447 %
      14.400000      14.162084     -1.652193 %
      14.500000      14.162084      2.330454 % <<< exceeds 2% absolute error

The smallest value for which % Error is greater than 2% in absolute value can be found this way:

      >FNROOT(10,14.5,ABS(100*(ATANH(TANH(FVAR))/FVAR-1))-2)

            14.4511062736

which is the correct smallest value as the relative error is:

      >100*(ATANH(TANH(14.4511062736))-14.4511062736)/14.4511062736

            -1.9999999995 (%)

i.e.: ~ 2% error, as required:

- For the HP-11C/HP-15C and other 10-digit calcs featuring hyperbolic functions, we may use this simple routine to explore the % error:

      01 LBL A
      02 ENTER
      03 TANH
      04 ATANH
      05 D% {delta %}
      06 ABS
      07 RTN

[USER] [FIX 4]

      12 [A] -> 1.1708 (%)
      12.1 [A] -> 1.9876 (%)
      12.2 [A] -> 2.7910 (%) <<< exceeds 2% absolute error

The smallest value for which % Error is greater than 2% in absolute value can be easily found using an HP-15C by solving this litte program (a variation of the above code):

      01 LBL A
      02 ENTER
      03 TANH
      04 ATANH
      05 D% {delta %}
      06 ABS
      07 2
      08 -
      09 RTN

[USER] [FIX 9]

      12 [ENTER] 12.2 [SOLVE A] -> 12.10152965


- let's check:

      [ENTER] [TANH] [ATANH] [D%] -> -1.999999975

            ~ 2% error, as required

- For the HP42S, this code allows for exploration. It's the same code as the one for the 10-digit HP-11C, say, but the results are the same as those for the 12-digit HP-71B:

      01 LBL A
      02 ENTER
      03 TANH
      04 ATANH
      05 %CH
      06 ABS
      07 END

      12 XEQ A -> 0.0274 (%)
      14 XEQ A -> 1.1577 (%)
      14.5 XEQ A -> 2.3305 (%) <<< exceeds 2%
etc.

Dessert 4:

Shortest is:

      >INX-INX;INX-UNF;INX-OVF;INX-DVZ;-INX;-UNF;-OVF;-DVZ;-IVL;-INX-UNF;-INX-OVF (74-char)

            0 1 2 3 4 5 6 7 8 9 10

and there are zillions of permutations and variations. For instance, you can get 0 by INX-INF, UNF-UNF, ..., EPS-EPS, etc. and you can get 1 by INX/INX, UNF/UNF, ..., EPS/EPS, ... and so on and so forth. None of them are less than 74-char long and I don't think a shorter solution is possible (though I'd love to be proved wrong).

Finally, as a free bonus, I'll give the factorization I discovered for the big number I gave in the prologue to the challenge, namely:

      555555555555554444444444444443333333333333332222222222222222211111111111111 =

            3063441154048486369668261625739 * 181350163955772670068231705843686316121284149


where both factors are prime, of course. To check it using my HP-71B and a very simple multiprecision multiplication UDF, just execute this:

      >FNM$("3063441154048486369668261625739","181350163955772670068231705843686316121284149")

            555555555555554444444444444443333333333333332222222222222222211111111111111

which checks Ok.

That's all. Thanks for your interest and really glad you liked it. See you in S&SMC#23 ! :-)

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
[VA] Short & Sweet Math Challenge #22: April 1st, 2018 Spring Special - My Solutions - Valentin Albillo - 04-06-2018 04:08 AM



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