Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

50%
+0 −0
Challenges Hex ​​​detector

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...

posted 3mo ago by trichoplax‭  ·  edited 3mo ago by trichoplax‭

Answer
#2: Post edited by user avatar trichoplax‭ · 2024-08-24T00:31:03Z (3 months ago)
Label equation for clarity
  • ## 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 by user avatar trichoplax‭ · 2024-08-24T00:10:31Z (3 months ago)
## 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$$