For more than a week I have been walking on foot and riding public transport, tickets are not sold after the 5th, so I have to pay the fare every time I land. What do schoolchildren and students most often (and often even adults, watch every day) with a ticket first thing? They check it for belonging to the so-called “happy”, then to put it into their collection, and maybe even “quietly” to eat for loyalty. Or maybe just throw it out :)

In any case, this is some kind of entertainment and a reason for joy. And something my childhood in one place began to play, it became interesting, what is the probability of getting a “happy” ticket? How many happy tickets are there?
')
I remember that at school even someone did research work (or something like that, not the essence), we tried to derive formulas, draw graphs, and so on.
But, since the power of our computers often allows us to go in a non-optimal way (from the point of view of mathematics), I decided not to bother with the derivation of formulas.
The result was faster :)
I'm used to programming in Perl. Most sensible programmers, I think, can easily repeat the same thing in just a few minutes, at least in Delphi, at least in Basic, at least in JavaScript, etc.
The program simply goes through almost a million combinations (989k, the first lucky ticket 1001).
#!/usr/bin/perl
print "Content-Type: text/html\n\n" ;
$num = 1000;
$s = 0;
while ($num < 1000000) {
$ln = length($num);
$l6 = substr($num,$ln-6,1) if ($ln > 5);
$l5 = substr($num,$ln-5,1) if ($ln > 4);
$l4 = substr($num,$ln-4,1);
$l3 = substr($num,$ln-3,1);
$l2 = substr($num,$ln-2,1);
$l1 = substr($num,$ln-1,1);
if ($l6+$l5+$l4 eq $l3+$l2+$l1) {
print 0 x (6-$ln), "$num\n" ;
$s++;
}
$num++;
}
print " = $s" ;
* This source code was highlighted with Source Code Highlighter .
As a result, at the exit, I
received an amount equal to 55251 happy ticket .
Slightly rounding, we get that approximately
every 18th ticket is “happy .
”Already out of interest I decided to look for a mathematical solution, and I found a very interesting
story , I recommend reading, if you want to digress a little for 10-15 minutes from work, it's quite interesting. On the same page there are a lot of interesting things about happy tickets.
And on Wikipedia, you can see a
short hood , there is a good and briefly shown the distribution of "happy" tickets.
As in Wikipedia, the number 55252 is also indicated in the story. At the time of writing the topic, I thought that the ticket with the number 000000 did not exist. The
comments provide evidence of the existence of such a ticket.
What did I want to say to all of this? Yes, actually, nothing special. If you need to relax during the work day - you can find a lot of interesting things (except games) and things that are useful for mental activity.
Be happy!
UPD: I counted the number of “meeting” tickets (amounts differ by one) and “letter” (differ by 2). “Meeting” -
109494 tickets , “letter” -
106524 tickets. As a result, about
every fourth ticket brings a little positive to our life :)