Hi,
In a motivational session we were once asked to write down all numbers from 1 to 100 in 20 seconds (with pen and paper).
Needless to say I cheated by writing a simpple Pascal program with a for loop from 1 to 100.
Later I cheated some more with "π" (pi) as my solution and some time later with "e" (Eulers number), as both of them contain all numbers from 1..100.
I then asked myself: "what is the smallest possible string that contains all numbers from 0 to 99"?
Surely it is not all numbers 0..99 concatenated.
For starters single 0..9 can be omitted to begin with, as they will appear in e.g. 20, 21, 22 etc.
Even the concatenation of 11 and 12 can be shortened to 112.
So, the challenge now is to write a program that constructs a string which contains all numbers from 0 up to and including 99.
I managed (wiht a trivial and naive implementation) to get this:
1011213141516171819202232425262728293033435363738394044546474849505565758596066768697077879808899091It's 100 characters long (still wouldn't be able to write that down with pen and paper in 20 seconds though...).
Note: the correct answer to this puzzle may very be known, I'm certainly not the first one to ask myself this question.
The challenge however is to come up with an algorithm to produce such a string, as short as you possibly can.
So your code cannot simply be:
program challenge;
begin
writeln(TheAnswerChatGptSuggested);
end.
Some rules:
- The program must be pure Pascal
- The program cannot use any unit (uses clause must be empty, and please don't cheat using "-Fa" compiler switch)
- The program cannot link in any external library
- The resulting string must contain all nunbers from 0 up to and including 99 (*)
- The numbers need not be in consecutive order
- The program should not rely on CPU architecture, bitness, Endian-ness or underlying OS
- The algorithm should not have things like "switch characters 1,2,4 with 9,5,6 and then remove characters 6,7,8" because by hand you figured out that would give a better result
(*) Below a simple check function which the resulting string must pass:
function Check(const S: String; Verbose: Boolean = False): Boolean;
var
i: Integer;
begin
for i := 10 to 99 do //this implies 0..9 will be included as well
if (Pos(IntToStr(i), S) = 0) then
begin
if Verbose then
writeln('Check: missing ',i);
Exit(False);
end;
Result := True;
end;
Notice that this is
not a speed contest.
We want the shortest possible solution.
That being said: you can certainly brute force all strings with length 99 and less and check any of them to see if they might check OK, but you program must be able to return before the heath death of the universe.
For now let's say it should return the answer in less than 1 hour on a moderately new laptop (like mine

).
What's in it for the winner?
That's easy: eternal fame.
So to you all: surprise me!
Bart
PS. I wasn't able to find the answer, but is it somewhere actually described what the shortest answer is?