Contribution for Puzzlet #023

From:  Patrick Curteis
[p.curteis@bigpond.com]

I've just had a go at Puzzlet 79. I reached the first solution (Your house is 1189 out of 1681 houses) easily and was about to tick that one off. But the perfectionist in me wanted to search for other possible solutions. My next solution was your last (your house is 6930 out of 9800 houses). So I'm not getting your middle solution (house 4552 out of 6437).

I think it's a simple case of rounding error in your code.

The sum of integers from 1 to 4551 is 10358076. So that's the sum of the house numbers below your house.

The sum of integers from 1 to 6437 is 20720702 and the sum of integers from 1 to 4552 is 10362628.
So the sum of integers from 4553 to 6437 is 20720702 - 10362628 = 10358075. So that's the sum of the house numbers above your house.
Out by 1!

With my programme I just used a FOR loop from 1000 to 9999 as My House Number. It's easy enough to work out the sum of integers below this number. MHN(MHN-1)/2. Then I generated a cumulative sum, starting with MHN+1 and adding 1 each time then checking. If it's below the lower house number sum then just keep going. If it's above the lower house number then I've gone over and it's time to move on to the next value of MHN. Otherwise the two totals are equal and I print the results.

I've attached my code below. Don't laugh. I'm still using BASIC. I'm too busy and too old to bother learn something new, although I'm vaguely interested in Python.

All the best for 2010.
Patrick Curteis
Sydney Australia

10 rem ===========
20 rem PUZZLET 079
30 rem ===========
40 clear
50 home
60 print "PUZZLET 079"
70 print
80 print "The sum of the house numbers less than"
90 print "mine in my street is equal to the sum of"
100 print "the house numbers which are greater than"
110 print "mine. My house number happens to"
120 print "comprise four digits. What is my house"
130 print "number?"
140 print
150 for m = 1000 to 9999
160 rem
170 rem CALCULATE LOWER SUM
180 rem
190 let ls = ((m-1)*m)/2
200 rem
210 rem GENERATE UPPER SUM
220 rem
230 let us = 0
240 let k = m+1
250 let us = us+k
260 if us < ls then let k = k+1 : goto 250
270 if us > ls then goto 310
280 print "My house number is ";m;"and there"
290 print "are ";k;"houses in the street."
300 print
310 next m
320 print "That's the lot!"
330 print
340 end

PUZZLET 079

The sum of the house numbers less than
mine in my street is equal to the sum of
the house numbers which are greater than
mine. My house number happens to
comprise four digits. What is my house
number?

My house number is 1189 and there
are 1681 houses in the street.

My house number is 6930 and there
are 9800 houses in the street.

That's the lot!


Patrick,

Many thanks for your kind and interesting e-mail.

You're right!  To avoid that kind of error I made the variable myHseNr a type "float," which is a decimal number rather than an integer. Then I included the test line "if myHseNr = int(myHseNr)" to be sure the square root in my formula wouldn't be fooled by the fact that the square root wasn't exact.  However, the results are so close that the rounding-off error you mentioned occurred.

The float type is single-precision.  If I had made it double-precision, the error would have been caught.  I've run the code with double-precision, and it comes out right.  I'll be publishing your input as a contribution, and giving you the credit for spotting the error.

Thanks for your code.  It may be the old MS Basic, but it works fine.  We all started there, and it's still a good workhorse.


Dave.


 I updated the "Solutions" page, and there are further comments there.



MAIN MENU
CONTRIBUTIONS

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: January 1st, 2010.