Post History
Rust, 35 bytes |n:f64|(12.*n-3.).sqrt().fract()>0. Counterintuitively, this outputs false for a hex number, and true otherwise, but this is consistent with the output requirement: One o...
#2: Post edited
- ## Rust, 35 bytes
- ```rust
- |n:f64|(12.*n-3.).sqrt().fract()>0.
- ```
- Counterintuitively, this outputs `false` for a hex number, and `true` otherwise, but this is consistent with the output requirement:
- > - One of 2 distinct values, indicating whether the input is a hex number.
- This allows using `>0` rather than `==0` to save 1 byte.
- [All test cases on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=95e6fbc442632795c89a73ed31812ef0)
- ## Breakdown of the code
- ```rust
- |n:f64|(12.*n-3.).sqrt().fract()>0.
- |n | // Anonymous function with input n
- :f64 // Input is 64 bit floating point
- (12.*n // Multiply input by 12
- -3.) // Subtract 3 ("." denotes float)
- .sqrt() // Calculate square root
- .fract()>0. // Is fractional part non-zero?
- // Implicitly return true or false
- ```
- ## Explanation
- From [Wikipedia](https://en.wikipedia.org/wiki/Centered_hexagonal_number#Formula), the following formula gives the index $n$ of a hex number $H$ (it gives $1$ for the first hex number, $2$ for the second, and so on):
- $$n=\frac{3+\sqrt{12H-3}}{6}$$
- This $n$ is an integer if and only if $H$ is a hex number. Rather than calculate the whole formula, the code only calculates the square root:
- $$R=\sqrt{12H-3}$$
- because $n$ is an integer if and only if $R$ is an integer.
- ## Reasoning
- Reasons for asserting that $n$ is an integer if and only if $R$ is an integer. Since all the test cases pass, this section is just out of interest, to show that this also holds outside the test cases.
- ### When $R$ is not an integer, $n$ is not an integer
- If the square root $R$ is not an integer, then $n=\frac{3+R}{6}$ cannot be an integer.
- ### When $R$ is an integer, $n$ is an integer
- $n=\frac{3+R}{6}$ is an integer if and only if $R$ is an odd multiple of $3$.
- Need to show that $R\in \Bbb Z\implies R\equiv 3\pmod 6$ (that is, whenever $R$ is an integer, it is also an odd multiple of $3$).
$$R\in\Bbb Z\implies R=6x+c, x\in\Bbb Z,c\in\{0,1,2,3,4,5\}$$- Need to show that $R\in\Bbb Z\implies c=3$
- $$\begin{align}R&=\sqrt{12H-3}, H\in\Bbb Z\\
- R^2&=12H-3\\
- H&=\frac{R^2+3}{12}\\
H&=\frac{(6x+c)^2+3}{12}\\- H&=\frac{36x^2+12xc+c^2+3}{12}\\
- H&=3x^2+xc+\frac{c^2+3}{12}\\
- H-3x^2-xc&=\frac{c^2+3}{12}\\
- \end{align}$$
- $$H-3x^2-xc\in\Bbb Z\therefore \frac{c^2+3}{12}\in\Bbb Z$$
- $$c\in\{0,1,2,3,4,5\}, \frac{c^2+3}{12}\in\Bbb Z\therefore c=3$$
- $$\therefore R\in\Bbb Z\implies R\equiv 3\pmod 6\implies n\in\Bbb Z$$
- ## Rust, 35 bytes
- ```rust
- |n:f64|(12.*n-3.).sqrt().fract()>0.
- ```
- Counterintuitively, this outputs `false` for a hex number, and `true` otherwise, but this is consistent with the output requirement:
- > - One of 2 distinct values, indicating whether the input is a hex number.
- This allows using `>0` rather than `==0` to save 1 byte.
- [All test cases on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=95e6fbc442632795c89a73ed31812ef0)
- ## Breakdown of the code
- ```rust
- |n:f64|(12.*n-3.).sqrt().fract()>0.
- |n | // Anonymous function with input n
- :f64 // Input is 64 bit floating point
- (12.*n // Multiply input by 12
- -3.) // Subtract 3 ("." denotes float)
- .sqrt() // Calculate square root
- .fract()>0. // Is fractional part non-zero?
- // Implicitly return true or false
- ```
- ## Explanation
- From [Wikipedia](https://en.wikipedia.org/wiki/Centered_hexagonal_number#Formula), the following formula gives the index $n$ of a hex number $H$ (it gives $1$ for the first hex number, $2$ for the second, and so on):
- $$n=\frac{3+\sqrt{12H-3}}{6}$$
- This $n$ is an integer if and only if $H$ is a hex number. Rather than calculate the whole formula, the code only calculates the square root:
- $$R=\sqrt{12H-3}$$
- because $n$ is an integer if and only if $R$ is an integer.
- ## Reasoning
- Reasons for asserting that $n$ is an integer if and only if $R$ is an integer. Since all the test cases pass, this section is just out of interest, to show that this also holds outside the test cases.
- ### When $R$ is not an integer, $n$ is not an integer
- If the square root $R$ is not an integer, then $n=\frac{3+R}{6}$ cannot be an integer.
- ### When $R$ is an integer, $n$ is an integer
- $n=\frac{3+R}{6}$ is an integer if and only if $R$ is an odd multiple of $3$.
- Need to show that $R\in \Bbb Z\implies R\equiv 3\pmod 6$ (that is, whenever $R$ is an integer, it is also an odd multiple of $3$).
- $$R\in\Bbb Z\implies R=6x+c, x\in\Bbb Z,c\in\{0,1,2,3,4,5\}\tag{1}$$
- Need to show that $R\in\Bbb Z\implies c=3$
- $$\begin{align}R&=\sqrt{12H-3}, H\in\Bbb Z\\
- R^2&=12H-3\\
- H&=\frac{R^2+3}{12}\\
- H&\stackrel{(1)}=\frac{(6x+c)^2+3}{12}\\
- H&=\frac{36x^2+12xc+c^2+3}{12}\\
- H&=3x^2+xc+\frac{c^2+3}{12}\\
- H-3x^2-xc&=\frac{c^2+3}{12}\\
- \end{align}$$
- $$H-3x^2-xc\in\Bbb Z\therefore \frac{c^2+3}{12}\in\Bbb Z$$
- $$c\in\{0,1,2,3,4,5\}, \frac{c^2+3}{12}\in\Bbb Z\therefore c=3$$
- $$\therefore R\in\Bbb Z\implies R\equiv 3\pmod 6\implies n\in\Bbb Z$$
#1: Initial revision
## Rust, 35 bytes
```rust
|n:f64|(12.*n-3.).sqrt().fract()>0.
```
Counterintuitively, this outputs `false` for a hex number, and `true` otherwise, but this is consistent with the output requirement:
> - One of 2 distinct values, indicating whether the input is a hex number.
This allows using `>0` rather than `==0` to save 1 byte.
[All test cases on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=95e6fbc442632795c89a73ed31812ef0)
## Breakdown of the code
```rust
|n:f64|(12.*n-3.).sqrt().fract()>0.
|n | // Anonymous function with input n
:f64 // Input is 64 bit floating point
(12.*n // Multiply input by 12
-3.) // Subtract 3 ("." denotes float)
.sqrt() // Calculate square root
.fract()>0. // Is fractional part non-zero?
// Implicitly return true or false
```
## Explanation
From [Wikipedia](https://en.wikipedia.org/wiki/Centered_hexagonal_number#Formula), the following formula gives the index $n$ of a hex number $H$ (it gives $1$ for the first hex number, $2$ for the second, and so on):
$$n=\frac{3+\sqrt{12H-3}}{6}$$
This $n$ is an integer if and only if $H$ is a hex number. Rather than calculate the whole formula, the code only calculates the square root:
$$R=\sqrt{12H-3}$$
because $n$ is an integer if and only if $R$ is an integer.
## Reasoning
Reasons for asserting that $n$ is an integer if and only if $R$ is an integer. Since all the test cases pass, this section is just out of interest, to show that this also holds outside the test cases.
### When $R$ is not an integer, $n$ is not an integer
If the square root $R$ is not an integer, then $n=\frac{3+R}{6}$ cannot be an integer.
### When $R$ is an integer, $n$ is an integer
$n=\frac{3+R}{6}$ is an integer if and only if $R$ is an odd multiple of $3$.
Need to show that $R\in \Bbb Z\implies R\equiv 3\pmod 6$ (that is, whenever $R$ is an integer, it is also an odd multiple of $3$).
$$R\in\Bbb Z\implies R=6x+c, x\in\Bbb Z,c\in\{0,1,2,3,4,5\}$$
Need to show that $R\in\Bbb Z\implies c=3$
$$\begin{align}R&=\sqrt{12H-3}, H\in\Bbb Z\\
R^2&=12H-3\\
H&=\frac{R^2+3}{12}\\
H&=\frac{(6x+c)^2+3}{12}\\
H&=\frac{36x^2+12xc+c^2+3}{12}\\
H&=3x^2+xc+\frac{c^2+3}{12}\\
H-3x^2-xc&=\frac{c^2+3}{12}\\
\end{align}$$
$$H-3x^2-xc\in\Bbb Z\therefore \frac{c^2+3}{12}\in\Bbb Z$$
$$c\in\{0,1,2,3,4,5\}, \frac{c^2+3}{12}\in\Bbb Z\therefore c=3$$
$$\therefore R\in\Bbb Z\implies R\equiv 3\pmod 6\implies n\in\Bbb Z$$
