I think everybody knows this puzzle.
We just count the circles in numbers.
Let's try to solve this puzzle using math.
We need a table that contains the number of zeroes of every digit.
x | y |
0 | 1 |
1 | 0 |
2 | 0 |
3 | 0 |
4 | 0 |
5 | 0 |
6 | 1 |
7 | 0 |
8 | 2 |
9 | 1 |
y = [1, 0, 0, 0, 0, 0, 1, 0, 2, 1]
Using summation and the table above, we just sum up circles of every digit of the number.
Here's the formula:
\(f\left(x\right)=\sum_{n=0}^{\lfloor\log_{10}x\rfloor}y\left[\Bigl\lfloor\frac{x}{10^{n}}\Bigr\rfloor\operatorname{mod}10+1\right]\)
\(\left\lfloor\frac{x}{10^n}\right\rfloor\) can be written as \(\lfloor10^{-n}x\rfloor\)
\(\left\lfloor\frac{x}{10^n}\right\rfloor=\lfloor10^{-n}x\rfloor\)
Hence
\(f\left(x\right)=\sum_{n=0}^{\lfloor\log_{10}x\rfloor}y\left[\lfloor10^{-n}x\rfloor\operatorname{mod}10+1\right]\)
y of the table from the previous formula can be replaced with the if-else statement. If x=0 or 6 or 9 return 1, if x=8 return 2.
In math, the if-else statement can be written as follows:
(condition)(true-false)+false
Where the condition is 0 or 1,
true - returning value if the condition is 1,
false - returning value if the condition is 0.
To check if two numbers are equal, we can use the following formula:
\(eq(x,y)=1-|\operatorname{sign}(x-y)|\)
Here's the if-else statement that replaces y of the table
\(t\left(x\right)=\left(eq\left(x,0\right)+eq\left(x,6\right)+eq\left(x,9\right)\right)\left(1-2eq\left(x,8\right)\right)+eq\left(x,8\right)\)
As I said, y of the table can be replaced with the if-else statement, so the formula (without table) will look like the formula (using table data), but with y[...+1] replaced by t(...)
\(f\left(x\right)=\sum_{n=0}^{\lfloor\log_{10}x\rfloor}t\left(\lfloor10^{-n}x\rfloor\operatorname{mod}10\right)\)
Look at this in Desmos: https://www.desmos.com/calculator/ivopfgdddk