Hi all,
-
Where I am right now, April 1st has just begun, the very start of a brand-new month, so time for a new S&SMC, this time #15, an "April 1st Spring Special".
What's so "special" about it ? Well, for one, you'll have
5 individual, affordable mini-challenges to test your smarts and
programming abilities against instead of a single,
harder challenge.
And secondly, this time you won't only be fighting to find
your way through the challenges but you'll also experience
the agony of a deadline, i.e., delivering the goods in a fixed,
non-expandable amount of time. Namely this means you won't have
a whole week before I post the solutions but just this very weekend.
Next Monday I'll post my original solutions and comments,
so if you want to try your hand at it before I spill them out,
you'd better hurry up this one time, no excuses. :-)
In all five cases, you must write a program for your chosen
HP handheld (preferably, but other brands also acceptable as long
as they're handheld but no PDAs!) to help you answer the challenge.
Any language (RPN/RPL/BASIC/FORTH/C...) is acceptable as long as it
actually runs in your handheld.
Now for the Spring Special:
Take 1
-
Given the coefficients a, b, c, d of the general
4th-degree polynomial equation:
x4 + a*x3 + b*x2 + c*x + d = 0you must write a program to compute the sum of the cubes
of all its four roots (real and/or complex).
If in RPN/RPL, your program must assume the coefficients have
previously been entered into the stack as follows:
T: dand upon running, it must stop with the sum of the cubes of the
Z: c
Y: b
X: a
four roots in the display. You should try to make your program
as short and fast as possible.
For instance, given the equation:
x4 - 2.006*x3 - 4*x2 - x + 4.44 = 0your program must output 35.144216216, which is the sum of
the cubes of its four roots, namely:
x1 = 0.848975027766As this is S&SMC #15, I'll post my 15-step RPN solution for the HP-15C
x2 = 3.21371823523
x3 = -1.0283466315 +0.754884455486*i
x4 = -1.0283466315 -0.754884455486*i
(which will also run in the HP-34C and other RPN models as well),
plus a couple of solutions for the HP-71B, for good measure.
Take 2:
-
Write a program to find all positive integers up to 10,000 such that
they're simultaneously equal to the sum of the 1st powers of two
consecutive integers and also to the positive difference of the
2nd powers of those same integers.
For instance, 153 is a solution because:
153 = 761 + 771where 76 and 77 are consecutive integers.
= 772 - 762
Your program must find out and output all solutions, and be as short and fast as possible.
Take 3:
A cable is intended to tightly surround the Earth (assumed to be a perfect
sphere) by the equator so that it touches the ground at all its points.
Unfortunately, due to a slight manufacturing error, the cable, which should
be some 40 million meters long, is actually a trifle 1 meter longer, so that
regrettably, instead of fitting tightly as intended, there's some slack.
To fix the problem, a man sits comfortably on a small cushion at ground level,
picks the cable between his fingers, and raises his arm vertically upwards
from the ground till the cable is perfectly tight again, with no slack
whatsoever.
Assuming that the Earth is a perfect sphere of radius exactly equal to
6,400 Km, and that the cable doesn't stretch at all and has negligible radius,
what's the name of the man's brother-in law ?
Note:
-
In order to help you find the answer, you are required to write
a program to compute the required height to remove the slack for
any given extra cable length (measurements in meters), and then run
your program using the data for this particular case (1 extra meter).
Take 4:
-
The subject of self-reproducing programs, i.e., programs that upon
running do produce their own source code as their output, is highly
fascinating and has tons of actual instances in every programming
language known to man.
For instance, in standard C you have:
main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}in RPL you have:
\<<in Java you have:
\<< "\<<" SWAP +
"DUP EVAL" + OBJ\->
\>>
DUP EVAL
\>>
class S{public static void main(String[]a){String s="classand in Pascal you have:
S{public static void main(String[]a){String s=;char ='';
System.out.println(s.substring(0,52)+c+s+c+s.substring
(52,61)+c+s.substring(61));}}";char c='"';System.out.println
(s.substring(0,52)+c+s+c+s.substring(52,61)+c+s.substring(61));}}
program s;const p='program s;const p=';a='a';aa=''';';aaa='a=''';aaaaNow you must try your hand at writing a self-reproducing program for the
='''';aaaaa='begin write(p,aaaa,p,aa,aaa,a,aa,a,aaa,aaaa,aa,aa,a,a,
aaa,aaa,aaaa,aa,a,a,a,aaa,aaaa,aaaa,aa,a,a,a,a,aaa,aaaaa,aa,aaaaa)
end.';begin write(p,aaaa,p,aa,aaa,a,aa,a,aaa,aaaa,aa,aa,a,a,aaa,aaa,
aaaa,aa,a,a,a,aaa,aaaa,aaaa,aa,a,a,a,a,aaa,aaaaa,aa,aaaaa)end.
HP-71B such that upon running produces as output its own source code.
You may use not only statements and functions in the System ROMs but also
in external ROMs, such as the Math ROM, the FORTH/Assembler ROM, or even
the JPC ROM, to name a few. You should try to optimize for program size,
the shorter the better.
Take 5:
-
We're given a function f(x,y) recursively defined as follows:
f(0,y) = y+1by applying this definition, it's straightforward to compute
f(x,0) = f(x-1,1)
f(x,y) = f(x-1,f(x,y-1))
the function's exact value for various arguments, for instance
we readily find the values:
f(0,4) = 5you're now asked to write a program that computes and outputs
f(1,2) = 4
f(2,3) = 9
f(3,1) = 13
the exact value of f(4,2).
I'll post my original 9-line solution for the HP-71B.
Caveats:
-
That's all, the usual caveats apply, mainly:
- Please refrain from posting just the solutions, actual code
is mandatory. Googling solutions and posting them
is pretty lame and only serves to spoil the challenge
for others and makes blatantly public your unability
to comply and your serious attitude problem and
disrespect for rules.
That said, let's see your answers before I post my solutions
next Monday. You have more than 48 hours, so hurry up, you
can do it ! :-)
Post-Edited 3 Apr 2006 solely to correct improper formatting (extremely long lines).
Edited: 3 Apr 2006, 5:48 a.m. after one or more responses were posted