Search
As of October, 2016, Embarcadero is offering a free release
of Delphi (Delphi
10.1 Berlin Starter Edition ). There
are a few restrictions, but it is a welcome step toward making
more programmers aware of the joys of Delphi. They do say
"Offer may be withdrawn at any time", so don't delay if you want
to check it out. Please use the
feedback
link to let me know if the link stops working.
Support DFF  Shop
If you shop at Amazon anyway, consider using
this link. We receive a few cents from each purchase.
Thanks.
Support DFF  Donate
If you benefit from the website, in terms of
knowledge, entertainment value, or something otherwise useful,
consider making a donation via PayPal to help defray the
costs. (No PayPal account necessary to donate via credit
card.) Transaction is secure.
Contact
Feedback:
Send an
email with your comments about this program (or anything else).

 
Problem Description
Runners at a marathon race are assigned consecutive numbers starting at 1.
One of the entrants with a mathematical bent noticed that the sum of the numbers less than his
own was equal to the sum of the numbers greater. If there were more than 10 runners but less than
100, what number was he and how many runners were there in the race?
What if there were more than 100 runners but less than 1000?
Source: Math and Logic Puzzles for PC Enthusiasts, Dover Books, J. J. Clessa.
Background & Techniques
Another trial and error solution. We'll just try all marathoner's
numbers between the lower and upper limits we get from the user.
For each, add up the number lower and add up the number bigger and when
they're equal, we've got a solution.
Running/Exploring the Program
Suggestions for Further Explorations

Could he have noticed that the
product of the numbers less than
his was equal to the product of the numbers greater? If you try this,
you'll need to use Delphi int64 type (long integer) to hold the trial
products. Also only try 1 to 100 runners. Well, maybe only try 1 to 45 since
the largest number in int64 is 2^{63}
which is around 10^{20}
which is about the size of the product of numbers from 1 to 45. Here's
a trick worth remembering: Since 2^{3}
is around 10, 2^{3X} which is (2^{3})^{x} is
around 10^{X}
for any X. In other words, take any power of 2 and divide the power by 3
to get an estimate of its size in base 10. So now you know, 2^{150}
is is about 50 digits long, in case anyone ever asks. 

The product of integers from 1 to 45 is called a factorial and
written as 45!. I looked on the internet and found a site that lists the
first 999 factorials and found that 45! is about 10^{20}.
The whole list is 1.5 megabytes in size so I won't link to it here.
Maybe we'll write a Delphi program one of these days to check the
list. 

From a performance point of view, it seems redundant to recalculate the 2 sums from
scratch each time. When we test to see if the marathoner's number is n,
we calculate the sum from 1 to n1 and from
n+1 to the upper limit. To check n+1, we should be able to add
n to the lower sum and subtract n+1
from the upper sum to get the next 2 sums to compare. See if you can modify the code to work this way. 
