I've been looking at molly's code for about 5-10 mins and I seem to understand some most of it (maybe it's just me being tired and I hope tommorow I'll think clearer), but maybe because my logic isn't the shiniest of all and I can't understand some things which may sound hard for me. Mind explaining what you did in the 2nd count?
Hmz. i could, but then you would not learn anything (in fact i've written down my whole analyses and wanted to post until i realized that's your job ;-) ).
If you really don't fully understand how routine1 evolved into routine2, then please ask me anything specific what i can explain to you, but please don't expect me to analyze routine1 which should be your first goal.
If you analyze routine1 properly (please use a clear head for that) then information can be extracted that leads to the solution used in routine2. Asking questions and recognizing the pattern in routine1 automatically leads to something as shown in routine2.
As a hint, when not fully understanding what happens, then you can always create small example routines that mimics things. for example change the while loop inside routine2 into something that reads:
while i <= max do
begin
writeln('The value of i = ', i);
i := i + 1;
end;
And see for yourself what this outputs.
Using such small examples make you understand what the code actually does. In this case you will see that the loop increases variable i until it has reached a particular value. The variable i also started at a particular value. The loop itself print out all the values while being inside the loop. Hopefully that will let you understand how a while loop works.
The fact that i do some calculation inside the loop is secondary (even while in the end it is used to produce the result), but could just as easily be analyzed by yourself ;-)
FWIW: in case you have been given a clear assignment, then don't hesitate to literally post the assignment in the forum so that we know for sure how to advise you. But please don't expect users to give you a full example that meets the requirements of your assignment. In case someone would post a fully working example that meets the requirements then please ignore that, as it won't help you learn anything.
Programming can be hard to learn, but becomes even harder when just copy-pasting someone else's code without fully understanding the code. Analyzing presented working code in hindsight is always more easy then doing things yourself. Doing things yourself has the benefit of sticking into memory better (at least that goes for most people, i am aware there are exceptions).
So, in the end i wanted to ask you if you could explain what happens exactly in routine2, because if you can tell me that then you figured out how to solve the repeating div problem from your original question ;-) (well, at least you should be able todo so then).