Programming exercise - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Programming exercise (/thread-229760.html) |
Programming exercise - Gerson W. Barbosa - 08-22-2012 What is my licence plate number? Hint #1: The four-digit number is the smallest number expressible as the sum of two cubes in two different ways, that is, n = a^3 + b^3 = c^3 + d^3; a, b, c and d are positive integers; Hint #2: n < 2000; Hint #3: the three-letter pefix is CAB backwards :-) My straightforward approach (not to call it 'brute-force' :-) could be used on the HP-41CV, but I have preferred to do it on my HP 50g. My RPL program is somewhat clumsy because I wrote a QBASIC program first and then converted it to RPL, but it does find n, a, b, c and d in 70 seconds on the real calculator when the input is 1999. When the input is 9999, another larger solution is found, but it takes about 10 minutes before the solutions are shown. The ten solutions less than 100,000 are found in about 9 minutes on the emulator. There is much room for improvement as no mathematical approaches have been tried-- neither could I. (I would have chosen API-3142, but it wasn't available anymore. On second thought I should have chosen something as simple as ABC-1234 :-) Have fun!
Re: Programming exercise - Don Shepherd - 08-22-2012 So how long have you been driving a taxicab in a suburb of London?
Re: Programming exercise - Gerson W. Barbosa - 08-22-2012 Ah, you know the story! Well, most everyone here does, I think :-)
Re: Programming exercise - Valentin Albillo - 08-23-2012 Quote: Ramanujan, perchance ?
Best regards from V. Re: Programming exercise - LHH - 08-23-2012 This is just the kind of problem I like to try to solve in my head when I go to bed. Usually it's calculating the time to a star at various speeds and distances (or something along those lines). I may have mentioned before my (young) kids and I once tried to estimate the number of atoms in the universe and came up with 10^75. Recently I saw an estimate of 10^80 elemental particles so we actually came pretty close! This would have been more difficult just remembering all the possible cubes but I'm sure it would have put me to sleep quickly (which is the desired effect)! Anyway, just doodling I wrote down all the possible cubes and have an answer (BAC-1729) but I did cheat by using a biological computer (I will accept my disqualification in a manly fashion).
Re: Programming exercise - Valentin Albillo - 08-23-2012
. Quote: Hehe, just in case you're too sleepy to give it a try, this short HP-71B code will find the numeric part of the answer in negligible time:
>CAT It's a trivial brute-force approach and it certainly could be made faster by simply pre-computing a cubes table up to the required limit so that N=I*I*I+J*J*J would be computed instead as N=C(I)+C(J) and other such tricks but it's pretty pointless as the running time is already completely negligible. Other trivial optimizations would include: - taking out of the J loop the invariant computation of I*I*I or C(I) if using the cubes table - using a string S$ of the correct length instead of a vector filling it up initially to all "0" characters, afterwards storing a "1" to mark each achieved sum in the correct position within the string after previously checking if the position is already marked so that a minimum solution has been found. This would save lots of memory as the string would take much less space than the array (which could have been declared INTEGER to save about 50% memory, by the way). - if your HP model has the equivalent of flag or bit arrays, this would be ideal, as the S array or the S$ string are just mimicking them etc, etc, etc, but as it's so trivial and runs instantly I didn't bother, too lazy today ... XD
Best regards from V.
Edited: 23 Aug 2012, 6:11 a.m.
Re: Programming exercise - Thomas Klemm - 08-23-2012 The program is for the HP-42S but can be adapted easily to other models:
STO 00 As I don't have the real calculater at hand I can't tell you how fast it is to find the smallest solution. However the result is displayed immediately on my iPhone. Only for the next solutions it takes some time to consider.
Usage: 1 R/S Thanks for the nice challenge: I had some fun.
Kind regards
Edited: 23 Aug 2012, 4:13 p.m. after one or more responses were posted
Re: Programming exercise - Bill (Smithville, NJ) - 08-23-2012 There's a nice BBC program on this number (part of their Five Numbers series):
Bill
Re: Programming exercise - Gilles Carpentier - 08-23-2012 Hi gerson
Here is a program for HP50 wich use GoferList Library
«
Just type
1999 or 9999 takes no time with emu48
All solutions are displayed sorted
Edited: 23 Aug 2012, 10:29 a.m.
Re: Programming exercise - Gilles Carpentier - 08-23-2012 HI
10 DESTROY ALL @ INPUT "Max=";M @ R=INT(M^(1/3)) @ DIM S(2*R*R*R) Interesting. But how are sure that N is the minimum ? or:
20 FOR I=1 TO R-1 @ FOR J=I+1 TO R @ N=I*I*I+J*J*J Re: Programming exercise - C.Ret - 08-23-2012 Hi.
I used the same algorithm as you on my Commodore C128D.
LISTNote that it takes a few seconds on a Commodore C128/C128D so that pre-computing the cubes (line 30) makes no significant changes. Except perhaps better arithmetic precision, the Commodore 8bits are well known for really bad math! Max limit is 32000 due to conventional memory limit (128 ko). Max limit may be extend using memory add on such as graphic memory or unconventional RAM banks (but also need PEEK/POKE bad programming practice – current practices at C64 times!).
Edited: 23 Aug 2012, 11:40 a.m.
Re: Programming exercise - Gerson W. Barbosa - 08-23-2012 When I see this and other approaches here, mine looks quite silly! It could be done within two loops only once, instead I used two nested loops twice. Anyway, here it is, just for the records: %%HP: T(3)A(R)F(,); Re: Programming exercise - Gerson W. Barbosa - 08-23-2012 Quote: Your superior carbon-based computer is absolutely right! I ought to have tried using mine as well :-)
Re: Programming exercise - Gerson W. Barbosa - 08-23-2012 Hello Valentin,
Quote: Yes, as we are aware of: http://en.wikipedia.org/wiki/Taxicab_number http://mathworld.wolfram.com/TaxicabNumber.html Best regards,
Gerson.
Re: Programming exercise - C.Ret - 08-23-2012 Gilles you use a list to organize the seed of solutions. This gives me the basements of my HP-39gII implementation.
The HP39gII is easy to program and have a really large memory. So I use two list (L0 and L1) to store n=a3+b3 and a respectively.
EXPORT GERSON(m) Please note the elegant and powerful NTHROOT function of this HP-39gII !
EDIT : Sorry for this confusing PRINTing : PRINT(n+"="+a+"³+"+b+"³="+c+"³+"+d+"³");And you get a readable output full screen:
Edited: 23 Aug 2012, 1:38 p.m.
Re: Programming exercise - Gerson W. Barbosa - 08-23-2012 Hello Thomas,
Quote: I'm glad you have liked it. This is not a challenge however, this is just an exercise. I would pose a challenge only if I had a hard to beat solution, which would be very unlikely here :-) I tried your program in my HP-42S but I can't get it to work. It's a 107-byte program. I'll double-check the listing later. Cheers,
Gerson.
Re: Programming exercise - Gerson W. Barbosa - 08-23-2012 Bill, thanks for the link. Although I can listen to BBC live and some recorded programs, to that particular one I cannot. Any other link?
Gerson.
Re: Programming exercise - Tim Wessman - 08-23-2012 Your program running on the real hardware 39gII takes ~7.3s for an input of 100000 returning 10 results. Was that time for emu48 running real calc speed?
TW Edited: 23 Aug 2012, 2:06 p.m.
Re: Programming exercise - Thomas Klemm - 08-23-2012 Hi Gerson After adding an END to the program I get the following listing of the labels:
00 { 111-Byte Prgm } This might help to cross-check with what you have already entered. It appears the usage I gave was wrong. Instead of 0 start with 1. Apologies for that lapsus. However you should get:
1729 Though I've double checked the listing I might still have made some typos. Please forgive me as I'm typing all this on an iPhone which is a pain. Then once more thanks for the exercise and for taking the time to test my solution. For me it was still a challenge. Part of it was the limitation of the available memory.
Cheers Re: Programming exercise - Thomas Klemm - 08-23-2012 Just for the record: a solution for the HP-48GX without additional libraries:
\<< 3 XROOT IP \-> n It's reasonable fast for 1999 or 9999 but gets rather slow for bigger values like 105. My emulator m48 just tells me: DON'T PANIC. But I wasn't patient enough to wait for the result.
Cheers Re: Programming exercise - Gilles Carpentier - 08-23-2012 Hi Tim, the algoritm is not the same but on real 50G hdw (the EMU48 is not accurate at all for 50G program) i get for all solutions :
1999 : 3.3 s So the 39gII is ~200 times faster here !. I will try to translate the CRET prog on my 50G for an accurate benchmark I estimate in general the 39gII language 40 to 80 times faster than the UserRPL/50 depending what you do (and even far more quick with recursive program wich are incredibly slow in user RPL) For sure the 39gII speed is impressive (and use almost no battery !)
Edited: 23 Aug 2012, 5:14 p.m.
Re: Programming exercise - Gilles Carpentier - 08-23-2012 Another way, use the Valentin Albillo algorithm but all with stack and modify to show all solutions :
« 9999 takes 7,3 s on emulator at real speed (probably less with a real 50G cal) 99999 results 'out of memory' Use a string instead the stack could be interesting
Edited: 23 Aug 2012, 6:07 p.m.
Re: Programming exercise - Les Koller - 08-23-2012 Shades of Ramanujan and Hardy?
Re: Programming exercise - Gerson W. Barbosa - 08-23-2012 Hello Thomas,
Quote: The lapsus was mine, sorry! I had forgotten to insert the concatenation characters (shame on me!). Starting with 0 also works. The first solution is found after 32 seconds. The second one is shown about one minute later. Very nice!
Quote: Doing it fast and with little memory is really challenging. When I said it was not a challenge I meant I didn't intend to challenge no one here, rather invite you do do a programming exercise and find better solutions than the clumsy one I had found. My naive method consisted of finding all the possible sums of two cubes and storing them in a list, a second list would store a. When this was done I would search in the first list for repeated sums and take the respective a and c in the second list; b and d would be simply calculated. Because I intended to port the program to the HP-41CV, I replaced the second array with a scheme to keep track of a and b. The QBASIC program below is easier to follow than the RPL version elsewhere, but it still looks awkward :-)
? 100000 The problem in using QBASIC is it much faster than our calculators, real or emulated ones. The ten solutions above are found in about three seconds in my notebook, which disguises a bad algorithm. Cheers, Gerson.
Re: Programming exercise - Gerson W. Barbosa - 08-23-2012 Yes! :-) http://en.wikipedia.org/wiki/Taxicab_number
http://mathworld.wolfram.com/TaxicabNumber.html
Re: Programming exercise - C.Ret - 08-24-2012 I like Thomas Klemm's version. I have to admit that I am a bit lost with Gille's version despite they are both written in user RPL and look to follow relatively close algorithms.
Edited: 24 Aug 2012, 4:11 a.m.
Re: Programming exercise - C.Ret - 08-24-2012 Here is a direct translation of the HP39gII version for HP28S. As for the HP39gII, the two large lists are store in main (global) memory. The local variables are exactly the same { m r a b n p c d } but are only set in separate nested sub-structures:
« DUP 2 / 3 INV ^ -> m r
Edited: 24 Aug 2012, 4:03 a.m.
Re: Programming exercise - Bill (Smithville, NJ) - 08-24-2012 Hi Gerson, I couldn't get the program to play either. I have the program in MP3 format somewhere.
Send me an email and I'll see if I can find it for you.
Bill
Re: Programming exercise - Gilles Carpentier - 08-24-2012 Cret, Some comments :
@ Get the cubic rooth of the max numbre I use a lot of this kind of programmation with Eulers projects.
in fact with global variables and few change, it s just 3 simple lines of code and few bytes 3 XROOT IP 'Nm' STO and i think this 3 lines could work :
1 3 XROOT IP 1 Seq 3 ^ 'Li' STO
Edited: 24 Aug 2012, 12:46 p.m.
Re: Programming exercise - Thomas Klemm - 08-24-2012
Quote: Not exactly a string but the HP-42S allows for bit operations. So we can squeeze 36 bits into one register which is enough to find the smallest solutions.
00 { 65 Byte Prgm } Since Free42 allows to set the size to 9999 it's possible to find even more. But of course it's still rather limited compared to other calculators.
Cheers Re: Programming exercise - Gerson W. Barbosa - 08-27-2012 Thomas, I tried another approach on Emu71 using no matrices at all. Is it similar to what you have used on Free42? The timing is not accurate (sometimes I get about twice the actual seconds). Thanks! Cheers, Gerson.
>LIST Re: Programming exercise - Thomas Klemm - 08-28-2012 Hi Gerson
Quote: I don't think so. I'm using the following conditions:
These give additional restrictions on c and d. Here's a Python program that does (more or less) the same:
n = 20 And here the HP-42S program with some additional comments:
STO 00 ; input a
HTH Re: Programming exercise - Gerson W. Barbosa - 08-28-2012 Hello Thomas,
Quote: Yes, the only similarity might be both algorithms don't need a large amount of memory and thus can be implemented on many programmable calculators. Also, larger numbers can be found given enough time. I estimate I would need about two or three minutes to find the first number on the HP-42S using my algorithm. Way better than my first solution but at least four times slower than yours. Well, the problem is more difficult than I initially though. No wonder it's associated with Hardy, Ramanujan and the like.
It is stated here that 1729 is the smallest number which can be represented in two different ways as the sum of two cubes:I think they mean "the largest known primitive solution" (http://oeis.org/A018850), as larger solutions can be found by multiplying the primitive ones by cubes (8, 27, 64, 125...). Thanks for Python code and the comment to your HP-42S program.
Gerson.
|