The Museum of HP Calculators

HP Forum Archive 08

[ Return to Index | Top of Index ]

Short & Sweet Math Challenges for HP fans #5
Message #1 Posted by Ex-PPC member on 30 June 2002, 7:31 p.m.

Last week's challenge dealt with multiprecision computations. It needed some amount of real programming work, so it didn't get as many replies as previous, easier ones. This week's challenge is therefore shorter & sweeter as it needs just a little empirical search, plus simple formula-fitting and evaluation programming and a pinch of theoretical work thrown in for good measure.

Foreword

Imagine that you are a Maths teacher and you are preparing tomorrow's class, which will introduce your students to Cardano's formula for solving the reduced cubic equation:

                x^3 + p*x + q = 0

Given p and q, Cardano's formula gives this value for x:

      x = CURT(-q/2+SQRT((q/2)^2+(p/3)^3)) + CURT(-q/2-SQRT((q/2)^2+(p/3)^3))

where SQRT means "square root" and CURT means "cube root". Though the formula isn't really particularly complex, you are understandably worried that your students will find it somewhat complicated and weird-looking, so you decide that giving a simple numerical example will make it seem all the more familiar.

With this noble idea in mind, you then try this example, to be developed in the class:

                x^3 + 2*x - 7 = 0

This corresponds to p = 2 and q = -7, and Cardano's formula gives:

      x = CURT(7/2 + SQRT(49/4+8/27)) + CURT(7/2 - SQRT(49/4+8/27)) 
        = CURT(7/2 + SQRT(1355/108)) + CURT(7/2 - SQRT(1355/108)) 
        = CURT(7/2 + 3.54207513984) + CURT(7/2 - 3.54207513984) 
        = CURT(7.04207513984) + CURT(-0.04207513984)
        = 1.9167562361864 - 0.3478098331340 
        = 1.5689464030524

which is indeed a root of the equation. Let's check it:

   x^3+2*x-7 = 1.5689464030524^3 + 2*1.5689464030524 - 7      
             = 3.862107193895 + 3.137892806105 - 7 
             = 0

Now, you fear understandably that such festival of floating point, many-decimal numbers will not really make the point any simpler, and will probably bore your intended audience, faced with using their calculators extensively in order to find the answer and check it as well. So, you are kept wondering:

   "What I really need is to find a simpler example, some suitable values
    for p and q which will make all intermediate and final results small
    integer or rational numbers, so that no calculator shall be necessary
    to perform the computations and the whole process will seem much simpler,
    allowing my students to focus on the formula, not on the computational
    drudgery"

So that's what S&SMC#5 is all about:

The Challenge

  1. Use your favorite HP calculator to try and find values of p and q which will make all intermediate and final results in the evaluation of Cardano's formula either exact integers or rational fractions, so that no irrational number ever appears and all computations can be carried out either mentally or simply by hand.

  2. Once you have found a sufficiently large number of solutions (p,q), use your HP calculator to find suitable formulas that will generate them all (polynomial fitting, perhaps ?) or deduce such formulas theoretically. Ideally, the formulas would take as input the desired value for the final root, and would produce p and q such that the resulting cubic equation would have that root and would be extremely simple to solve, as requested.

Recommended HP calculator

  • For finding solutions (p,q) any programmable model will do, from the HP-10C or HP-25C onwards. The faster, the merrier, but the programming itself is pretty trivial.

  • For fitting the solutions to a polynomial, I guess any model from an HP-11C onwards will be adequate.

Estimated difficulty and allotted time

Pretty easy. Really "short and sweet" this time. Also, you have two weeks to try your hand with this challenge. At the end of that period, I'll post the usual Final Remarks, including solutions and snippets of code if necessary. If the number of postings in this thread warrants it, I will then post the next challenge, S&SMC#6.

By the way, I can't resist: if any of you finally developed some multiprecision routines, you can test that the root of the above equation, to 77 decimals places, is:

x = 1.56894640305238226735233475168775140550168711365188103792946945170655431302272
      
A question to you, Valentin Albillo (Ex-PPC), regarding your "challenges"
Message #2 Posted by W. Bruce Maguire II on 3 July 2002, 2:12 p.m.,
in response to message #1 by Ex-PPC member

Dear Valentin:
(aka Ex-PPC Member)
(aka Ex-PPC #4747)
(aka HPCC #1075)

I'm curious Valentin; out of the population of regular viewers/participators in the MoHPC forum, how many do you actually believe have so much free time on their hands that they want someone to give them math/calclator-related homework?

After reading all your "challenges" up to this one (number five), I sense a strong air of condescension. I guess others who have responded in the past don't feel this way, or don't care, but I must say that I'm continually amazed. Your "challenges" are not the light-hearted, friendly kind (like "who can generate the number 100 in the fewest keystrokes without hitting a number key?"); rather, they are the instructor/pupil kind of challenges---where Valentin is the instructor and we are the pupils.

It seems to me that your "challenges" serve only you. Who knows how many weeks/months/years you spend on some of these trivial pursuits, before you issue your challenge and smugly enjoy the "inferior" efforts produced by your "pupils"?

I know that there is absolutely no way for me to voice my disgust with the tone of your "challenges" without sounding petty. And you certainly have every right to continue to post your "challenges"---I am certainly no moderator for this forum---but I wanted to let you know that there is at least one person on this list who is "smart" enough (in your eyes maybe) to realize the purpose of your tests. It is clear to this non-pupil that your "challenges" are wholly self-serving. I, personally, am far too busy to indulge your ego.

I have a question for any HP-fan who has been responding to Valentin's "challenges". I have to ask: are you not able to challenge yourself? If you enjoy this type of pursuit (I, myself, have been known to tilt at a few mathematical windmills with my HPs! ;-) ), then I would submit that there is an overwhelming amount of material available in books and on the Internet that will provide you with enough challenges for a lifetime. And you can pursue these challeges on your own timeframe and not suffer the supercilious responses of the self-satisfied Valentin Albillo.

And finally, Valentin, I would assert that your contributions to this forum are miniscule compared with what they could be. You haven't posted anything in the "Articles" section; why? I present your posts about trigonometric functions on the HP-12C as an example. You asked for information from others, posted patronizing responses to anyone who offered solutions, and refused to ever respond to multiple requests that you post the code that you developed. A subsequent issue of the HPCC Datafile exposed why you wouldn't respond to anyone asking to see your programs: you had to make sure that your work was in print before revealing it to anyone! You clearly have the time and ability to do good things with HP calculators. If you cared less about elevating your self-image, many might benefit from your work.

For any who have access to the (excellent!) HPCC Datafile publications, I invite you to read the Editor's note at the bottom of page 20 of the Jan/Feb 2002 issue (V21,N1) and the first paragraph of Wlodek's article on page 16 of the Mar/Apr 2002 issue (V21,N2). They unintentionally betray how Valentin feels about his "teachings". That is, the sharing of knowledge is not what is important; what is important is that Valentin takes center-stage!

Bruce.
(I may be wrong, but at least I have the courage to post with my NAME and my E-MAIL address!)

            
An answer
Message #3 Posted by Nenad Vulic (Croatia) on 4 July 2002, 3:23 a.m.,
in response to message #2 by W. Bruce Maguire II

Bruce wrote:

"...who can generate the number 100 in the fewest keystrokes without hitting a number key?"

CLx, 10^x, 10^x, ENTER, X

I did not feel smart enough for the previous challenges, but I love this one. It took only a few seconds of my time...

                  
and with a 12C...
Message #4 Posted by thibaut.be on 4 July 2002, 4:20 a.m.,
in response to message #3 by Nenad Vulic (Croatia)

CLX e^x ENTER e^x g INTG D %

;-) I also enjoyed this one... LOL !

                        
shorter abswer for HP 12C
Message #5 Posted by mapet on 4 July 2002, 5:05 a.m.,
in response to message #4 by thibaut.be

CLX e^x ENTER %T - 4 keystrokes

OVER

                              
Re: shorter abswer for HP 12C
Message #6 Posted by thibaut.be on 4 July 2002, 5:23 a.m.,
in response to message #5 by mapet

yes, indeed, ;-)

                              
alternative anserws
Message #7 Posted by mapet on 4 July 2002, 5:26 a.m.,
in response to message #5 by mapet

ENTER EEX ENTER %T

and my favourite but longer one:

CLX EEX ENTER % 1/x

OVER

                                    
and the best answer....
Message #8 Posted by mapet on 4 July 2002, 5:30 a.m.,
in response to message #7 by mapet

EEX ENTER %T

OVER :-) LOL

                                          
Re: and the best answer....
Message #9 Posted by Thibaut on 4 July 2002, 9:51 a.m.,
in response to message #8 by mapet

... or

S+ ENTER %T

;-)

                  
Re: A slightly-better answer
Message #10 Posted by Karl Schneider on 4 July 2002, 4:20 a.m.,
in response to message #3 by Nenad Vulic (Croatia)

Nenad --

Your technique was the same one I had in mind, except that x^2 could be substituted for ENTER, * (except if you're using a 12C):

CLx, 10^x, 10^x, x^2

4 operations; total number of keystrokes dependent on calculator model...

                        
Some shorter options, but some shortcuts too...
Message #11 Posted by Andrés C. Rodríguez (Argentina) on 4 July 2002, 7:52 a.m.,
in response to message #10 by Karl Schneider

[EEX] [10^X] (2 keystrokes)

or

[ACOS] (1 keystroke, assuming GRAD mode and a cleared machine)

or

[R/S] (1 keystroke, calling a one line program: 01 1E2)

or

[ON] (1 keystroke, on a Continuous Memory calculator which displayed 100 when turned off)

or

A wand scan of a "100" data sticker (1 button press and a handstroke)

or ...

                              
The consequence
Message #12 Posted by Nenad Vulic (Croatia) on 4 July 2002, 8:22 a.m.,
in response to message #11 by Andrés C. Rodríguez (Argentina)

... is that the number of responses in this thread proves that Bruce was obviously right.

                              
Re: Some shorter options, but some shortcuts too...
Message #13 Posted by thibaut.be on 4 July 2002, 9:50 a.m.,
in response to message #11 by Andrés C. Rodríguez (Argentina)

You're cheeting, you're not using a 12c ;-)

                                    
Sorry, no 12C here
Message #14 Posted by Andrés C. Rodríguez (Argentina) on 4 July 2002, 10:22 a.m.,
in response to message #13 by thibaut.be

Yes, I have no 12C at hand. And I was not taking it too seriously, as you surely noticed.

                              
Correction
Message #15 Posted by Andrés C. Rodríguez (Argentina) on 4 July 2002, 10:25 a.m.,
in response to message #11 by Andrés C. Rodríguez (Argentina)

First example has a [x^2] missing, so it is 3 keystrokes.

Please see: Me. Bruce's challenge didn't specified any particular model (12C or whatever)

                                    
Re: Correction
Message #16 Posted by John Ioannidis on 5 July 2002, 11:43 a.m.,
in response to message #15 by Andrés C. Rodríguez (Argentina)

I think the arccosine of zero in grad mode wins the originality contest, even though it is five keystrokes on most handheld HPs: [shift] [GRD] [CLx] [shift] [ACOS]. Congratulations Andrés!

I would count [EEX] and [.] as numeric keys.

                                          
Thank you! (GRAD ACOS)
Message #17 Posted by Andrés C. Rodríguez (Argentina) on 5 July 2002, 12:20 p.m.,
in response to message #16 by John Ioannidis

Thank you, John, for the originality nomination. Around 1977 I needed to program my HP 25 to "bounce" some values at the 100 boundary; my function needed to work this way:

[ x always had a value between 0 and 200]

If x < 100, then f(x) = x

If x > 100, then f(x) = 100 - x

By using GRAD mode (the function had nothing to do with angles), it was very convenient to execute:

SIN ASIN

and that was all about it!

I suppose the faint and very old memory of that case influenced my answer...

            
Re: A question to you, Valentin Albillo (Ex-PPC), regarding your "challenges"
Message #18 Posted by Vieira, Luiz C. (Brazil) on 4 July 2002, 8:34 a.m.,
in response to message #2 by W. Bruce Maguire II

Hi, Mr. Bruce

I intend no harm, and if I offend someone, please, believe this is not the purpose.

I´d like to express my concerns about the way people show their thoughts: maybe the way I express myself seems offensive to someone, but it may also be fairly accepted in my own community. That’s why I sometimes think a lot before writing anything in here, mostly because I’m a foreigner and I may not understand the real meaning of some idiomatic expressions, but some words speak for themselves.

I felt (my own opinion, please) sometimes that Mr. Ex-PPC Member presented the challenges in a way not compatible with this forum’s way. In some (many) cases, the single mentioning of any HP calc as a wonderful, supreme or-the-like instrument, made me suspect about the real concern: solving the problem OR pointing HP calcs as not enough for the job.

I solved one or two challenges for my own, but I didn’t think of them, the way they were shown, with the purpose to serve this forum’s primary needs. I suspect Mr. Ex-PPC member (as a super-hero with his identity revealed) may disappear for a while OR change his identity again, I don’t know.

I agree with Mr. Maguire´s post primary concerns, and I don’t want my friends in here to understand my own post as a negative criticism to someone else´s, only another view of his mention.

Cheers.

            
Re: A question to you, W. Bruce Maguire II
Message #19 Posted by Ellis Easley on 8 July 2002, 9:03 a.m.,
in response to message #2 by W. Bruce Maguire II

As they say in talk radio, "I love you, man", but while Ex-PPC might be pretentious, you are a trouble maker. There's no harm in Ex-PPC's challenges. And regarding the use of aliases, will you say unequivocally that you are not "John Smith", the author of this message:

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi?read=19556

                  
Re: A question to you, W. Bruce Maguire II
Message #20 Posted by W. Bruce Maguire II on 8 July 2002, 7:13 p.m.,
in response to message #19 by Ellis Easley

Ellis:

You wrote:

As they say in talk radio, "I love you, man", but while Ex-PPC might be pretentious, you are a trouble maker. There's no harm in Ex-PPC's challenges. And regarding the use of aliases, will you say unequivocally that you are not "John Smith", the author of this message:

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi?read=19556

Well, I guess it depends on what you mean by troublemaker... ;-) And actually, I thought there was a very small amount of harm in Valentin's (Ex-PPC's) posts---they are so downright condescending that I thought they slightly harmed everyone in the forum. You, and possibly others, may disagree with that notion. That's OK with me. The message really was to Valentin; although he never responded to my questions before, so I didn't really expect a response this time.

As to the alias and the above message, I will unequivocally state that I did NOT post that message. I'll go one better: I give you my word. I have never posted here using any alias. Mike has accused me of trying to fool people by using my TWO e-mail addresses! He caught me! ;-) I didn't think anyone would figure out that "W. Bruce Maguire II, maguire@AnalyticInvestments.com" and "W. Bruce Maguire II, maguire@ti.com" were the same... That Mike's a clever one!

I do admit to silently laughing when I saw the post, though. Believe it or not, there are other people present who don't like Mike's business practices either.

I've never felt that I had anything to gain by posting anonymously, or anything to lose by using my real name and e-mail address. I'm curious why Valentin (Ex-PPC) feels differently.

Bruce.

                        
Re: A question to you, W. Bruce Maguire II
Message #21 Posted by Ellis Easley on 9 July 2002, 3:34 a.m.,
in response to message #20 by W. Bruce Maguire II

"John Smith"'s remark about not accepting PayPal made me think of you - maybe I've got a screw loose!

                              
Re: A question to you, W. Bruce Maguire II
Message #22 Posted by W. Bruce Maguire II on 9 July 2002, 12:28 p.m.,
in response to message #21 by Ellis Easley

"John Smith"'s remark about not accepting PayPal made me think of you - maybe I've got a screw loose!

Ellis:

No, you don't have a screw loose---at least I don't think you do... ;-) That is the exact same complaint that I had about Mike's business. But, as I said, there are others around that have privately voiced to me (via e-mail) similar views regarding Mike's business.

Speaking of a screw loose, I still need to check-up on HP screws at McGuckin's for someone (I'll have to check the archives to see who I owe that to)! It's not you, is it? If it is, then I appreciate the subtle reminder! ;-) To be honest, I've had precious little time to devote to all-things-HP lately...

Bruce.

                                    
Re: A question to you, W. Bruce Maguire II
Message #23 Posted by Ellis Easley on 10 July 2002, 11:06 a.m.,
in response to message #22 by W. Bruce Maguire II

I've learned a lot about screws since that discussion started. I now have a number of different #2 and #3 screws including a #2 Plastite, none of which are quite right. Currently I believe the original screw was a "Plastite 60-1" and while that is obsolete, and the most widely made Plastite screw now is "Plastite 48-2", at least one manufacturer offers a "Plastite 48-1", which I think will fit. I need to make some microscope observations before I ask a dealer to try to get some.

Here is a link to the original thread: http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv008.cgi?read=17614

And my three updates: http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv008.cgi?read=17963

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv008.cgi?read=18432

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv008.cgi?read=19079

      
Re: Short & Sweet Math Challenges for HP fans #5
Message #24 Posted by Rupert (Northern Italy, EU) on 4 July 2002, 4:17 p.m.,
in response to message #1 by Ex-PPC member

>
> x^3 + p*x + q = 0
>
> Given p and q, Cardano's formula gives this value for x:
>
>
> x = CURT(-q/2+SQRT((q/2)^2+(p/3)^3)) + CURT(-q/2-SQRT((q/2)^2+(p/3)^3))
>


Well, if I'm not wrong, the signs in the second term are a bit
messed up. The correct version should be rather:

x = CURT(-q / 2 + SQR((q / 2) ^ 2 + (p / 3) ^ 3)) - CURT(q / 2 + SQR((q / 2) ^ 2 + (p / 3) ^ 3))


>
> x^3 + 2*x - 7 = 0
>
> This corresponds to p = 2 and q = -7, and Cardano's formula gives:
>
>
> x = CURT(7/2 + SQRT(49/4+8/27)) + CURT(7/2 - SQRT(49/4+8/27))
> = CURT(7/2 + SQRT(1355/108)) + CURT(7/2 - SQRT(1355/108))
> = CURT(7/2 + 3.54207513984) + CURT(7/2 - 3.54207513984)
> = CURT(7.04207513984) + CURT(-0.04207513984)
> = 1.9167562361864 - 0.3478098331340
> = 1.5689464030524
>

Again, the final result is right (I checked it with the HP48G+ ROOT
finder :) ) but the signs in your intermediate passages are all
messed up: CURT(-0.04207513984) would give a complex result.

Anyway, if I haven't misunderstood what is being asked, it looks
too easy: I just imposed that the fractional parts of the two
terms of the Cardano's formula and of the root must be equal to
zero. There is plenty of results: I enclose below the
quick'n'dirty :) BASIC listing I wrote to generate them:

----------------------------------------------------------------------------------

DIM d, t1, t2, x AS DOUBLE
DIM p, q, kp, kq, maxnum AS DOUBLE

maxnum = 1000

OPEN "c:\outfile.dat" FOR OUTPUT AS #1

CLS

FOR p = 1 TO maxnum
kp = (p / 3) ^ 3
FOR q = -1 TO -maxnum STEP -1
kq = q / 2
d = SQR(kq ^ 2 + kp)
t1 = -kq + d
t2 = kq + d
x = t1 ^ (1 / 3) - t2 ^ (1 / 3)

IF t1 - INT(t1) = 0 AND t2 - INT(t2) = 0 AND x - INT(x) = 0 THEN

PRINT "p="; p; "q="; q; "d="; d; "t1="; t1; "t2="; t2; "x="; x

PRINT #1, "p="; p; "q="; q; "d="; d; "t1="; t1; "t2="; t2; "x="; x

END IF

NEXT q
NEXT p

CLOSE #1

END

--------------------------------------------------------------------------------------------

I haven't had much time, so I tested the algorithm in BASIC and used my HP48G+
only to check the results. Here is a sample of the output:

p= 6 q=-7 d= 4.5 t1= 8 t2= 1 x= 1
p= 9 q=-26 d= 14 t1= 27 t2= 1 x= 2
p= 12 q=-63 d= 32.5 t1= 64 t2= 1 x= 3
p= 15 q=-124 d= 63 t1= 125 t2= 1 x= 4
p= 18 q=-19 d= 17.5 t1= 27 t2= 8 x= 1
p= 18 q=-215 d= 108.5 t1= 216 t2= 1 x= 5
p= 21 q=-342 d= 172 t1= 343 t2= 1 x= 6
p= 24 q=-56 d= 36 t1= 64 t2= 8 x= 2
p= 24 q=-511 d= 256.5 t1= 512 t2= 1 x= 7
p= 27 q=-728 d= 365 t1= 729 t2= 1 x= 8
p= 30 q=-117 d= 66.5 t1= 125 t2= 8 x= 3
p= 30 q=-999 d= 500.5 t1= 1000 t2= 1 x= 9
p= 36 q=-37 d= 45.5 t1= 64 t2= 27 x= 1
p= 36 q=-208 d= 112 t1= 216 t2= 8 x= 4
p= 42 q=-335 d= 175.5 t1= 343 t2= 8 x= 5
p= 45 q=-98 d= 76 t1= 125 t2= 27 x= 2
p= 48 q=-504 d= 260 t1= 512 t2= 8 x= 6
p= 54 q=-189 d= 121.5 t1= 216 t2= 27 x= 3
p= 54 q=-721 d= 368.5 t1= 729 t2= 8 x= 7
p= 60 q=-61 d= 94.5 t1= 125 t2= 64 x= 1
p= 60 q=-992 d= 504 t1= 1000 t2= 8 x= 8
p= 63 q=-316 d= 185 t1= 343 t2= 27 x= 4
p= 72 q=-152 d= 140 t1= 216 t2= 64 x= 2
p= 72 q=-485 d= 269.5 t1= 512 t2= 27 x= 5
p= 81 q=-702 d= 378 t1= 729 t2= 27 x= 6
p= 84 q=-279 d= 203.5 t1= 343 t2= 64 x= 3
p= 90 q=-91 d= 170.5 t1= 216 t2= 125 x= 1
p= 90 q=-973 d= 513.5 t1= 1000 t2= 27 x= 7
p= 96 q=-448 d= 288 t1= 512 t2= 64 x= 4
p= 105 q=-218 d= 234 t1= 343 t2= 125 x= 2
p= 108 q=-665 d= 396.5 t1= 729 t2= 64 x= 5
p= 120 q=-387 d= 318.5 t1= 512 t2= 125 x= 3
p= 120 q=-936 d= 532 t1= 1000 t2= 64 x= 6
p= 126 q=-127 d= 279.5 t1= 343 t2= 216 x= 1
p= 135 q=-604 d= 427 t1= 729 t2= 125 x= 4
p= 144 q=-296 d= 364 t1= 512 t2= 216 x= 2
p= 150 q=-875 d= 562.5 t1= 1000 t2= 125 x= 5
[...]

The search is limited to 1000 (maxnum) 'cause there are too much results,
anyway it's easy to modify that limit in order to go further.
Turning the line "FOR q = -1 TO -maxnum STEP -1" into "FOR q = 1 TO maxnum STEP 1",
I found that, if a couple (p,q) is a valid result and gives a root x, then (p,-q) is still a
valid result and gives a root -x, too.

If that is what it's being asked, maybe I'll find the time to go ahead with
polynomial interpolation of the results in order to have some fun with my HP48. :)

--


            
Re: Short & Sweet Math Challenges for HP fans #5
Message #25 Posted by John Ioannidis on 5 July 2002, 12:24 a.m.,
in response to message #24 by Rupert (Northern Italy, EU)

You are wrong on so many counts.

1. cbrt(-x) = -cbrt(x), so the signs are correct.

2. The cube root of a real is a real, regardless of whether that real is positive or negative. (Yes, I know about the other two complex roots, but that's really (no pun) for a complex number with a zero imaginary part. Think algebraic fields).

3. Your program missed the obvious solution p=-6, q=4 (with root 2). No wonder, since...

4. Computers (and calculators) do arithmetic with finite precision; your test "t1 - int(t1)" is bound to fail when t1 should be an integer but isn't because of rounding errors.

                  
Re: Short & Sweet Math Challenges for HP fans #5
Message #26 Posted by Rupert (Northern Italy, EU) on 6 July 2002, 8:12 p.m.,
in response to message #25 by John Ioannidis


> You are wrong on so many counts.
>
> 1. cbrt(-x) = -cbrt(x), so the signs are correct.
>
> 2. The cube root of a real is a real, regardless of whether that real is positive or negative. (Yes, I know about the other two complex roots, but that's really (no pun) for a complex number with a zero imaginary part. Think algebraic fields).
>

Here is an example: (-8)^(1/3).

MATHCAD: 1+1.732050807568877*i

HP48G+: (1, 1.73205080757)

HP32SII: x:1 y:1.732050808

etc.


> 3. Your program missed the obvious solution p=-6, q=4 (with root 2).


Well, I didn't even search for negative values of p.
Since it's the weekend :-) I took the time to look for
the right version of the Cardano's formula for the cubic equation
x^3+P*x+Q=0 and found the following:

x=((-Q+SQR(Q^2+4*P^3/27))/2)^(1/3) + ((-Q-SQR(Q^2+4*P^3/27))/2)^(1/3)

Now my new quick'n'dirty program tests all the couples (p,q), (-p,q),
(p,-q), and (-p,-q), too.

----------------------------------------------------------------------

DECLARE FUNCTION test (dummy$)

DIM p, q, r AS DOUBLE
DIM SHARED p0, q0, r0, e, eps, epsx AS DOUBLE
DIM maxnum, k, k1p, q2, dummy AS DOUBLE

k = 4 / 27
e = 1 / 3
eps = .000001
epsx = .0001

CLS
PRINT ""
INPUT "Max p, q "; maxnum

OPEN "c:\outfile.dat" FOR OUTPUT AS #1

FOR p = 1 TO maxnum
k1p = k * (p ^ 3)

FOR q = 1 TO maxnum
q2 = q ^ 2
r0 = q2 + k1p
p0 = p
q0 = q
dummy = test("")

q0 = -q
dummy = test("")

r0 = q2 - k1p
p0 = -p
q0 = q
dummy = test("")

q0 = -q
dummy = test("")

NEXT q
NEXT p

CLOSE #1

END

FUNCTION test (dummy$)
DIM sr, t1, t2, x AS DOUBLE

IF r0 >= 0 THEN
sr = SQR(r0)
IF ABS(sr - INT(sr)) <= eps THEN
sr = INT(sr + .5)
t1 = (-q0 + sr)
t2 = (-q0 - sr)

IF t1 >= 0 AND t2 >= 0 THEN
x = (t1 / 2) ^ e + (t2 / 2) ^ e
IF ABS(x - INT(x)) <= epsx THEN
x = INT(x + .5)
PRINT "p="; p0; "q="; q0; "discr.="; r0; "t1="; t1; "t2="; t2; "x="; x
PRINT #1, "p="; p0; "q="; q0; "discr.="; r0; "t1="; t1; "t2="; t2; "x="; x
END IF
END IF
END IF
END IF

test = 0
END FUNCTION

----------------------------------------------------------------------

Output with search limited to a maximum absolute value of 1000
for both p and q:

p=-3 q=-2 discr.= 0 t1= 2 t2= 2 x= 2
p=-6 q=-9 discr.= 49 t1= 16 t2= 2 x= 3
p=-9 q=-28 discr.= 676 t1= 54 t2= 2 x= 4
p=-12 q=-16 discr.= 0 t1= 16 t2= 16 x= 4
p=-12 q=-65 discr.= 3969 t1= 128 t2= 2 x= 5
p=-15 q=-126 discr.= 15376 t1= 250 t2= 2 x= 6
p=-18 q=-35 discr.= 361 t1= 54 t2= 16 x= 5
p=-18 q=-217 discr.= 46225 t1= 432 t2= 2 x= 7
p=-21 q=-344 discr.= 116964 t1= 686 t2= 2 x= 8
p=-24 q=-72 discr.= 3136 t1= 128 t2= 16 x= 6
p=-24 q=-513 discr.= 261121 t1= 1024 t2= 2 x= 9
p=-27 q=-54 discr.= 0 t1= 54 t2= 54 x= 6
p=-27 q=-730 discr.= 529984 t1= 1458 t2= 2 x= 10
p=-30 q=-133 discr.= 13689 t1= 250 t2= 16 x= 7
p=-36 q=-91 discr.= 1369 t1= 128 t2= 54 x= 7
p=-36 q=-224 discr.= 43264 t1= 432 t2= 16 x= 8
p=-42 q=-351 discr.= 112225 t1= 686 t2= 16 x= 9
p=-45 q=-152 discr.= 9604 t1= 250 t2= 54 x= 8
p=-48 q=-128 discr.= 0 t1= 128 t2= 128 x= 8
p=-48 q=-520 discr.= 254016 t1= 1024 t2= 16 x= 10
p=-54 q=-243 discr.= 35721 t1= 432 t2= 54 x= 9
p=-54 q=-737 discr.= 519841 t1= 1458 t2= 16 x= 11
p=-60 q=-189 discr.= 3721 t1= 250 t2= 128 x= 9
p=-63 q=-370 discr.= 99856 t1= 686 t2= 54 x= 10
p=-72 q=-280 discr.= 23104 t1= 432 t2= 128 x= 10
p=-72 q=-539 discr.= 235225 t1= 1024 t2= 54 x= 11
p=-75 q=-250 discr.= 0 t1= 250 t2= 250 x= 10
p=-81 q=-756 discr.= 492804 t1= 1458 t2= 54 x= 12
p=-84 q=-407 discr.= 77841 t1= 686 t2= 128 x= 11
p=-90 q=-341 discr.= 8281 t1= 432 t2= 250 x= 11
p=-96 q=-576 discr.= 200704 t1= 1024 t2= 128 x= 12
p=-105 q=-468 discr.= 47524 t1= 686 t2= 250 x= 12
p=-108 q=-432 discr.= 0 t1= 432 t2= 432 x= 12
p=-108 q=-793 discr.= 442225 t1= 1458 t2= 128 x= 13
p=-120 q=-637 discr.= 149769 t1= 1024 t2= 250 x= 13
p=-126 q=-559 discr.= 16129 t1= 686 t2= 432 x= 13
p=-135 q=-854 discr.= 364816 t1= 1458 t2= 250 x= 14
p=-144 q=-728 discr.= 87616 t1= 1024 t2= 432 x= 14
p=-147 q=-686 discr.= 0 t1= 686 t2= 686 x= 14
p=-162 q=-945 discr.= 263169 t1= 1458 t2= 432 x= 15
p=-168 q=-855 discr.= 28561 t1= 1024 t2= 686 x= 15


I got results with p>0 only doing a search with a maximum absolute
value > 1000 for both p and q, but results with p<0 and q>0
(as (-6,4)) are still missing:

p= 1 q=-1331 discr.= 1771561 t1= 2662 t2= 0 x= 11
p=-1 q=-1331 discr.= 1771561 t1= 2662 t2= 0 x= 11
p= 1 q=-2197 discr.= 4826809 t1= 4394 t2= 0 x= 13
p=-1 q=-2197 discr.= 4826809 t1= 4394 t2= 0 x= 13
p= 1 q=-2744 discr.= 7529536 t1= 5488 t2= 0 x= 14
p=-1 q=-2744 discr.= 7529536 t1= 5488 t2= 0 x= 14
p= 1 q=-3375 discr.= 1.139063E+07 t1= 6750 t2= 0 x= 15
p=-1 q=-3375 discr.= 1.139063E+07 t1= 6750 t2= 0 x= 15
p= 1 q=-4096 discr.= 1.677722E+07 t1= 8192 t2= 0 x= 16
p=-1 q=-4096 discr.= 1.677722E+07 t1= 8192 t2= 0 x= 16
p= 1 q=-4913 discr.= 2.413757E+07 t1= 9826 t2= 0 x= 17
p=-1 q=-4913 discr.= 2.413757E+07 t1= 9826 t2= 0 x= 17
p= 1 q=-5832 discr.= 3.401222E+07 t1= 11664 t2= 0 x= 18
p=-1 q=-5832 discr.= 3.401222E+07 t1= 11664 t2= 0 x= 18
p= 1 q=-6859 discr.= 4.704588E+07 t1= 13718 t2= 0 x= 19
p=-1 q=-6859 discr.= 4.704588E+07 t1= 13718 t2= 0 x= 19
p= 1 q=-8000 discr.= 6.4E+07 t1= 16000 t2= 0 x= 20
p=-1 q=-8000 discr.= 6.4E+07 t1= 16000 t2= 0 x= 20
p= 1 q=-9261 discr.= 8.576612E+07 t1= 18522 t2= 0 x= 21
p=-1 q=-9261 discr.= 8.576612E+07 t1= 18522 t2= 0 x= 21
p= 2 q=-4096 discr.= 1.677722E+07 t1= 8192 t2= 0 x= 16
p= 2 q=-4913 discr.= 2.413757E+07 t1= 9826 t2= 0 x= 17
p= 2 q=-5832 discr.= 3.401222E+07 t1= 11664 t2= 0 x= 18
p=-2 q=-5832 discr.= 3.401222E+07 t1= 11664 t2= 0 x= 18
p= 2 q=-6859 discr.= 4.704588E+07 t1= 13718 t2= 0 x= 19
p=-2 q=-6859 discr.= 4.704588E+07 t1= 13718 t2= 0 x= 19
p= 2 q=-8000 discr.= 6.4E+07 t1= 16000 t2= 0 x= 20
p=-2 q=-8000 discr.= 6.4E+07 t1= 16000 t2= 0 x= 20
p= 2 q=-9261 discr.= 8.576612E+07 t1= 18522 t2= 0 x= 21
p=-2 q=-9261 discr.= 8.576612E+07 t1= 18522 t2= 0 x= 21
p=-3 q=-2 discr.= 0 t1= 2 t2= 2 x= 2
p= 3 q=-6859 discr.= 4.704588E+07 t1= 13718 t2= 0 x= 19
p= 3 q=-9261 discr.= 8.576613E+07 t1= 18522 t2= 0 x= 21
p=-3 q=-9261 discr.= 8.576611E+07 t1= 18522 t2= 0 x= 21
p= 4 q=-9261 discr.= 8.576613E+07 t1= 18522 t2= 0 x= 21
p=-4 q=-9261 discr.= 8.576611E+07 t1= 18522 t2= 0 x= 21
p=-6 q=-9 discr.= 49 t1= 16 t2= 2 x= 3
p=-9 q=-28 discr.= 676 t1= 54 t2= 2 x= 4
[...]


> No wonder, since...
>
> 4. Computers (and calculators) do arithmetic with finite precision;
> your test "t1 - int(t1)" is bound to fail when t1 should be an integer
> but isn't because of rounding errors.

Now I have introduced a tolerance in the tests, but some results
are still missing.

Well, I' didn't resist the temptation to give it a try,
but I'm not a mathematician nor a programmer.
From a pratical point of view, any result that get the job done is good
Doing a technical job, when I run into an equation I just solve it...

--




                        
Re: Short & Sweet Math Challenges for HP fans #5
Message #27 Posted by John Ioannidis on 7 July 2002, 12:20 p.m.,
in response to message #26 by Rupert (Northern Italy, EU)

> Here is an example: (-8)^(1/3).

Is it too much to hope that people first use their brains and then their calculators? (-2)^3 = -8, therefore (-8)^(1/3) = -2.

I did say that you should only think in terms of real numbers. If you work with complex numbers, then -8 is really (-8, 0) (or -8+0j), which will indeed have three cube roots: (-2, 0), (1, sqrt(3)), and (1, -sqrt(3)). If you raise any of these three complex numbers to the third power, you get (-8, 0).

That's high school math, it's not like it's complicated!

The order in which you actually do the arithmetic in order to calcualate the roots may require you to apply the distributive property of multiplication and take the negative sign outside the second cube root to get your calculator to compute the real cube root rather than one of the complex cube roots. But this doesn't mean that the formula is incorrect.

                              
Re: Short & Sweet Math Challenges for HP fans #5
Message #28 Posted by Alex Binca on 7 July 2002, 3:02 p.m.,
in response to message #27 by John Ioannidis

> That's high school math, it's not like it's complicated!

Hey, why don't you try to be a little nicer. Being nice to other people it's taught in kindergarten. It's not like it's complicated!

: ) : )

-- Alex Binca

                        
Re: Short & Sweet Math Challenges for HP fans #5
Message #29 Posted by Rupert (Northern Italy, EU) on 12 July 2002, 6:41 p.m.,
in response to message #26 by Rupert (Northern Italy, EU)


Judging by some replies I've got since now,
it looks like I've been talking in Greek language.
The point is that, using either a CAS or a calculator,
the result for the cubic root of a negative real number
will always be a complex number, or an error message. ;-))
Just this.
Once again, as a little example here are the results
for (-8)^(1/3) :

Mathcad : 1+1.732050807568877*i

HP48G+ : (1, 1.73205080757)

HP32SII : x: 1 y: 1.732050808

HP15C : Re: 1 Im: 1.732050807

HP41CX + Math I Pac: Z^1/N, with N=3: U=1  V=1.732050808
                                      U=-2 V=0
                                      U=1  V=-1.732050808
                     Z^N, with N=1/3: U=1  V=1.732050807

Yorkem (HP49G emulator): 2*((1+i*SQRT(3))/2)=1+i*SQRT(3)= =(1, 1.73205080757).

It looks like the good ol' 41CX is the overall winner,
being the only one that gives the full result, satisfying
also those peoples who pretend (-k)^(1/3) is just
equal to -(k^(1/3)).

Well, now back on topic.
Since the cubic equation is just a particular case of
a third grade equation, then its roots can be computed
using polynomial root finding algorithms rather than
the Cardano's formula, overriding the troubles it involves.

Here is a new quick'n'dirty listing for the HP48 based
upon the PROOT (Polynomial ROOT) command
(tried also ROOT together with a deflation routine,
but it's obviously slower).
The program just discards those values of P and Q that
generate complex roots or real but non-integer roots.

----------------------------------------------------------------------

var: RUN

<< 0 FIX 1 CF 1E-5 'EPS' STO

"ENTER LIMIT" { ":MAX P,Q: " {1 0} V} INPUT OBJ-> -> MXN <<

1 MXN FOR PL 1 MXN FOR QL

PL QL TESTPQ PL QL NEG TESTPQ PL NEG QL TESTPQ PL NEG QL NEG TESTPQ

NEXT NEXT >>

1000 .5 BEEP >>

var: TESTPQ

<< 'Q' STO 'P' STO 1 0 P Q 4 ->ARRY PROOT -> ROOTS << 1 3 FOR J ROOTS J GET TESTX NEXT >> >>

var: TESTX

<< -> X << 'X' VTYPE -> VT << IF VT 0 == THEN IF X FP ABS EPS <= THEN

IF 1 FS? THEN P Q X 3 ->ARRY -> PQX << RSLT ARRY-> OBJ-> DROP DROP 1 + RSLT SWAP PQX SWAP ROW+ >> ELSE 1 SF P Q X 1 3 2 ->LIST ->ARRY END

'RSLT' STO

"P=" P + "Q=" Q + "X=" X + CLLCD 3 DISP 2 DISP 1 DISP 3 FREEZE END END >> >> >>

----------------------------------------------------------------------

Results are stored in the matrix RSLT.
Here is some results:

RSLT:

------------------
P Q X
------------------

-2 1 1
-2 -1 -1
-3 2 1
-3 2 -2
-3 -2 -1
-3 -2 2
-4 3 1
-4 -3 -1
-5 2 2
-5 -2 -2
-5 4 1
-5 -4 -1
-6 4 2
-6 -4 -2
-6 5 1
-6 -5 -1
-7 6 1
-7 6 2
-7 6 -3
-7 -6 -1
-7 -6 -2
-7 -6 3
-8 3 -3
-8 -3 3
-8 7 1
-8 -7 -1
-8 8 2
-8 -8 -2
-9 8 1
-9 -8 -1
-9 10 2
-9 -10 -2
-10 3 3
-10 -3 -3
-10 9 1
-10 -9 -1


Some couples P and Q are stored more than one time because
they generate more than one real integer root.

Well, time out for me now. I'll just be watching for
other solutions.

--



[ Return to Index | Top of Index ]

Go back to the main exhibit hall