# The next lucky ticket

Enter the current ticket number - on the output we get the number of the next lucky ticket and how many tickets are left till it

I was asked to solve a problem in computer science - to write a program that for a given ticket number calculates how much is left until the next "lucky" ticket. Everybody knows it - ticket numbers are usually six digits, and the "lucky" ticket is the one whose sum of the first three digits is equal to the sum of the last three digits. I decided to make a calculator at once.

If you were asked to just print all the numbers of the lucky tickets - it would be very simple - six nested loops, well, here we had to do a little bit of work to pull the number from the specified position in the ticket.
The formula is roughly like this:
$d=\frac{n \% 10^p - n \% 10^{p-1}}{10^{p-1}}$
Where
n is a number,
p is the digit position in the number (1 is the rightmost digit)
d - the digit to be searched
% - operation of obtaining the remainder from integer division (taking modulo), for example, 151 % 100 = 51.

Thus, to extract the digit 5 from the third position from the number 100500 would be this calculation:
$d=\frac{100500 \% 1000 - 100500 \% 100}{100}$.

Well, in javascript it would go something like this:

function extractNumberFromPosition(n, p) {
return (n%Math.pow(10,p) - i%Math.pow(10,p-1))/Math.pow(10,p-1);
}

#### The next lucky ticket

The next lucky ticket is

The next lucky ticket will be

URL copied to clipboard
PLANETCALC, The next lucky ticket