Mini-Challenge: Zip Code - 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: Mini-Challenge: Zip Code ( /thread-172316.html) |

Mini-Challenge: Zip Code - Thomas Klemm - 09-23-2010
The following challenge is based on an arithmetic problem I found in a mathematics book for elementary school. These are the steps necessary to create the next zip code:
- Select the zip code of a Swiss location.
- Compose the maximum number of the four digits.
- Compose the smallest possible number of the four digits.
- Calculate the difference.
## Example
- Start with 4153
- The maximum number made of these four digits is: 5431
- The minimal number made of these four digits is: 1345
- The difference is: 5431 - 1345 = 4086
## Analysis
Write a program for the HP calculator of your choice that returns the next zip code.
## ProofTry to proof your assumption. Can your calculator be of any help?
## SolutionI will add my solution for the HP-15C here within a couple of days.
Re: Mini-Challenge: Zip Code - Chuck - 09-24-2010
Question: does it always require to be four digits. That is will a zip of 12 (or 0012) transition to 2100-0012 = 2088 If so, I'm getting just about nothing but cycles. Sadly I'm not using an HP, though.
CHUCK
Re: Mini-Challenge: Zip Code - Dave Shaffer (Arizona) - 09-24-2010
Quote: I pretended I had a 71B (which I don't) and wrote a BASIC program. I won't give too much away, but it does converge (fairly quickly), but not to anything I might have guessed.
If you have ZIP codes which have 4 digits of the same value, such as 5555, it converges/collapses very quickly of course, to 0000.
Re: Mini-Challenge: Zip Code - Thomas Klemm - 09-24-2010
Quote: The authors of the book don't mention this special case but I did the same as you: add leading zeros whenever needed to get a four digit number.
Quote: At least to me that was part of the fun.
Best regards Re: Mini-Challenge: Zip Code - Thomas Klemm - 09-24-2010
The only possible Swiss ZIP codes made of the same digits are:
- 4444 Rümlingen
- 8888 Heiligkreuz
Best regards Re: Mini-Challenge: Zip Code - Paul Dale - 09-24-2010
Warning a spoiler ahead....
It leads to one of my favourite mathematical results: Kaprekar's constant. Also true for three digit zip codes I believe.
Re: Mini-Challenge: Zip Code - Patrice - 09-24-2010
Quote: One of my favorite too, and already fun with just a paper and a pen !
Patrice
Re: Mini-Challenge: Zip Code - Chuck - 09-24-2010
Aack. Egg on my face. It was too late last night and I forgot the "make the smallest" and "make the largest" number part. (I was just reversing the original number, doh.) Should be any easy fix with my program.
Re: Mini-Challenge: Zip Code - Dave Shaffer (Arizona) - 09-24-2010
Quote: I didn't know that is what it is called, but my little BASIC program did converge to the right value.
(Patrice - bonjour. Did you get to Arches Park?)
Re: Mini-Challenge: Zip Code - Casio fx-5800P - Marcus von Cube, Germany - 09-24-2010
Programming the Casio isn't as funny as it could be, but finally, I've arrived: '->' is a single char, entered with STO, indentation and comments added for humans.
4->N:?N: '# of digits It's a bit clumsy to produce a nice screen display with this calculator, because 'Locate' does not position the cursor for the next input statement and the EXE key leaves an arrow symbol on the screen.
Edited for typo in code.
Re: Mini-Challenge: Zip Code - George Litauszky - 09-26-2010
Hi Thomas and hi everybody! DUP (1432, 1432) ; Save input for comparsion Prg NBUILD: Input: (1, 2, 3, 4) 1 3 The result: The Kaprekar's constant after some iterations. Hungary also uses four digit postal (ZIP) codes.
Re: Mini-Challenge: Zip Code - Mark Storkamp - 09-27-2010
And here's a 41 solution. Start with number in the x register, and returns results in the x register
01 LBL "ZIP" Re: Mini-Challenge: Zip Code - Thomas Klemm - 09-27-2010
It appears that Wikipedia and
Quote:
Quote:
The question is whether 999/0999 is considered a 3- or a 4-digit number.
9990 - 0999 = 8991 After all this your question seems fairly reasonable.
Best regards Re: Mini-Challenge: Zip Code - Thomas Klemm - 09-27-2010
Quote:
Quote: Initialization:
10 STO 0
001 - 42,21,11 LBL A 022 - 30 -
Quote: Most numbers will end up with 6174. Only numbers composed of the same digit will end up with 0.
Quote:
My observation was the same as described in Mysterious number 6174. (cf. "Which way to 6174?")
Quote: Here's a small program that produces the 55 numbers left after the first iteration: Initialization:
9 STO 1
043 - 42,21,12 LBL B 055 - 42, 6, 2 ISG 2 Now you can switch between the two programs A and B:
The data was then used to create this graph. There's a direct proof in the same article as well. (cf. "Only 6174?") Again the HP-15C can be used to solve the system of linear equations:
| 1 0 1 -1 | | a | | 10 | The solution is: [ a b c d ] = [ 7 6 4 1 ] I must admit that I wasn't aware of Kaprekar's constant. I just saw this arithmetic problem in that book which made me wonder why. So thanks a lot to Paul Dale for pointing this out. I know it wasn't a hard problem but still an occasion to use your best loved machines. Nevertheless I hope you enjoyed the contest.
Cheers and thanks for your contributions
Quote:
Re: Mini-Challenge: Zip Code - Thomas Klemm - 09-27-2010
Another solution for the HP-48:
\<< Start with a list of 4 digits, e.g. { 4 1 5 3 }.
Thomas
Re: Mini-Challenge: Zip Code - Mark Storkamp - 09-27-2010
Thomas Klemm wrote: Quote: It's the easier challenges that I'm more likely to attempt, and I always seem to learn some new trick by trying it. For example, by stealing liberally from Thomas's 15C code I was able to shorten mine considerably and also eliminate use of any registers.
01 LBL "ZIP2" Re: Mini-Challenge: Zip Code - Thomas Klemm - 09-28-2010
Hi Mark You could remove line 46 RTN as the following line 47 END does the same.
I also tested your idea of dividing the entry first by 10
001 LBL A It uses the same amount of bytes, is one line longer and might be a little faster. However entering numbers is usually rather slow. So I'm not sure.
Cheers Re: Mini-Challenge: Zip Code - Mark Storkamp - 09-28-2010
I don't have RCL arithmetic at my disposal, and in a first quick go at it I couldn't get the number decomposed without using a register unless I divided by 1E4 first. That way I was left with integers for all but the final digit.
I could probably redo it now without the divide if I ended up with 0 - 0.9 on the stack, and used 9990(d-a)+900(c-b). And the last CLX RCL L could be replaced with an X<>L. But I'm not sure if all 41s could do that, or only the CX.
Re: Mini-Challenge: Zip Code - Thomas Klemm - 09-29-2010
Quote: One of the reasons why I chose HP-15C.
Quote: Yes, it can:
01 LBL "6174" |