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

Comments on Root to digits of π

Parent

Root to digits of π

+1
−1

How many digits of $\pi$ can you fit into a square root?

Rules

  • Your answer is a non-negative integer $N$.
  • Highest score wins.
  • Your score is the number of initial digits after the decimal point of $\sqrt{N}$ that match the initial digits of $\pi$.
  • No rounding is permitted of the digits of either $\pi$ or the square root.
  • For this challenge, the digits of $\pi$ include the initial 3:
3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

Examples

Zero matches

  • The integer $2$ has square root $1.4\ldots$
  • The digits after the decimal point are $4\ldots$
  • There are $0$ digits of $\pi$ matching so its score is $0$.

Two matches

  • The integer $11$ has square root $3.316\ldots$
  • The digits after the decimal point are $316\ldots$
  • There are $2$ digits of $\pi$ matching so its score is $2$.

No rounding

  • The integer $128$ has square root $11.3137\ldots$
  • The digits after the decimal point are $3137\ldots$
  • There are $2$ digits of $\pi$ matching so its score is $2$.
  • Note that $11.3137\ldots$ cannot be rounded to $11.314$ to match $3$ digits. No rounding is permitted.

Answer format

The automated leaderboard will include your answer provided you use the correct answer format:

## Language name, 3 matching digits
Optional arbitrary text.

```txt
Your competing integer in a code block
```

Optional arbitrary text.

```
The code you used to find your competing integer
```

Optional arbitrary text.

Putting your competing integer in a code block makes it easy for people to copy it for verification.

The leaderboard shows the last score present in the heading of your answer. This allows for including previous scores if you make improvements.

Verification

You can check the score of your own integer, or the integer from another answer, using the root to digits of pi validator.

Explanations in answers are optional, but I'm more likely to upvote answers that have one.

History

0 comment threads

Post
+6
−0

Note: I'm not using the leaderboard format, for reasons that will become apparent later.

Let's suppose we have a rational approximation $d = \frac a b$ to some real number that we want to be, modulo 1, close to the square root of a natural number - in this case, $.314159...$ or $\frac\pi{10}$. Then we want $N := (x + d)^2$ to be as close to an integer as possible, and in fact

$$ N = (x + d)^2 = (x + \frac a b)^2 = x^2 + 2 x \frac a b + {\frac a b}^2 = x^2 + \frac{2 x a b + a^2}{b^2} $$

Since we know $x^2$ is an integer and we want $N$ to approximate one, we can ignore that and deal with $N' := \frac{2 x a b + a^2}{b^2}$. For this to be an integer, we need

$$ b^2 \text{ divides } 2xab + a^2 \iff -a^2 = 2 x a b \text{ mod } b^2 \iff x = -a^2 \cdot \left(2 a b\right)^{-1} \text{ mod } b^2 $$

except this can't quite work: $2 a b$ isn't coprime to $b^2$, so $\left(2 a b\right)^{-1}$ won't exist modulo $b^2$ - and indeed, if this did work, we would be able to find integer square roots with arbitrary finite decimal expansions, which can't happen - $(x + .5)^2$ is never an integer, for instance. What we can do, however, is cheat slightly and add a small perturbation - take $\left(2 a b - 1\right)^{-1}$, for instance (which could also share a factor with $b^2$ and not be divisible, but sufficiently nice choices of $a$ and $b$ will prevent this)

Note 2: I don't actually know why this works - upon further thought, due to how modular arithmetic works, a small change in $2 a b$ could result in a large change in the resulting value of $x$? But for some reason it in fact works very well, as will become apparent later.

This gives us a value for $x$, and from that we can compute $N = x^2 + \left\lfloor\frac{2 x a b + a^2}{b^2}\right\rfloor$, where the floor division is necessary to fix our approximations.

For an example of this, let's approximate $\pi$ as $\frac{22}7$, i.e. $d = \frac{\frac{22}7}{10} = \frac {11}{ 35}$, i.e. $a = 11, b=35$. Then we get $ x = 11^2 \cdot (2 \cdot 11 \cdot 35 - 1)^{-1} \text{ mod } 35^2 $, which comes out to $1034$, after which $N$ comes out as $1069806$ and $\sqrt N$ as $1034.314265$ - pretty good!

Note 3: My original calculation used $-a^2$, but for some reason $a^2$ works better - not entirely sure why.

Next, for a larger example, let's try plugging in the first 200 decimal places of $\pi$, and after a bit my code spits out

937523293002658776348704769842539305648144222902391041804198307023876611679007339563904576704517047026597642317245013563261350766724787513251817694301543229194444616131823525148446741635864093056342088369126394578418147892002004186052415814273659670054499001041125811242784584390954000298648838953612272499808590813639231701499889493344119553570944163927982067912689004203386142800764496807779136844467819936800975933272481669347865507385952118302992371399189910547547628198410940202325281436034655796414880027985007133712574299608836521224536654484339688199950743647254454272876064422303005862463315882536889249083787990355940641001191524146159193403485502061273254506726884093838731770801055842611988398619589116686583248837626948667051332081102169659142504795515415306915108033323241905620107674

whose square root's decimal expansion looks like

[integer part].314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038189999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999848068430337174906334649751655113636032420403882399880832538599596915793497396586679196815633320326311365700306099279385136990931183996110519484150914211076274575736266077990160878090363392867830509955

which is 199 digits of $\pi$. Interestingly, we actually get $400$ digits of precision out of this - the $200$ digits following the digits of $\pi$ are all $9$s, which sorta makes sense considering that the term we introduce an error into, $2 a b$, has 400 digits of precision. In any case, this is accurate to 199 digits (the final digit is rounded down, so isn't technically correct), and also makes the online verifier hang.

So, let's take this to its logical conclusion: I downloaded 4 million digits of pi and plugged (most of) them into my script. My code had to be optimised a fair bit - in particular, modulo operations are absurdly slow so I had to use string slices (later made redundant), and computing the modular inverse of something that large is effectively impossible, so I instead simply divide $(b^2)^2$, roughly, by the value that needs to be inverted, which works almost as well. I later ended up switching to gmpy2, which was a massive speedup and allowed me to actually verify things.

The result is a 12566368-digit number that has the first 3141592 digits after its square root's decimal point equal to the corresponding digits of pi.


As for how far this can go, there isn't really any practical limit. With gmpy2, my laptop can process and verify the 3141592-digit case in about 3 seconds, and a 10-million-digit case in about 13. Algorithmically, I can produce a 4N-digit number that gives N decimal places of accuracy, and it's absolutely possible to improve on that constant factor (although a simple counting argument shows that you can't do any better asymptotically). It's difficult to get an idea of the algorithm's speed since most of it comes down to the underlying bigint representations, but it does seem relatively close to linear in the number of digits. Just for fun, I let my computer loose on a 100-million-digit testcase and it took 3 minutes, so with enough memory, scores of a billion and beyond definitely seem feasible.

History

1 comment thread

Wow! (6 comments)
Wow!
trichoplax‭ wrote 6 months ago · edited 6 months ago

Thanks for breaking my challenge! And for the interesting explanation of how you did it.

Part way through reading I thought "I must go and make the validator more efficient so it doesn't hang". Then I continued reading and realised you've found solutions far too big to fit in a Codidact post, so nothing I change in the validator is going to help.

Having seen what you did to this one, I'd welcome any advice on how to make future challenges more robust. Maybe I should have made the number of digits in $N$ part of the score rather than just the number of matching $\pi$ digits in $\sqrt{N}$. Your observation that the number of digits in $N$ is $4$ times the number of matching $\pi$ digits makes me wonder if the score should have been something like:

Your score is the number of digits $D$ in $N$ provided at least $D-1$ digits of $\pi$ match.

Small examples exist, and I suspect there are better ways than brute force to find them, but are there challenge-breakingly good ways?

trichoplax‭ wrote 6 months ago

In case this isn't obvious, I won't be changing the scoring mechanism of this challenge now that an answer has arrived. I'm just wondering what I can learn for future challenges.

wizzwizz4‭ wrote 6 months ago

If you did change the scoring mechanism, I'd suggest adding the efficiency (here, improving the number that's 4; although in other challenges, it might be possible to get sub-linear behaviour) as a tie-breaker if an infinite family of solutions is found.

trichoplax‭ wrote 6 months ago · edited 6 months ago

I was wondering if there's a scoring mechanism that puts no explicit upper limit on score but prevents such infinite families (so each new solution needs to be found independently). This won't help if it's still possible to find solutions too big to fit in an answer post though.

Alternatively, I could make a challenge where the number of digits that match $\pi$ (or $e$ or whatever) is fixed and the score is the integer whose square root matches this many (smallest integer wins). That would ensure a solution always fits in an answer post, but would require carefully choosing the problem size to avoid an early optimal solution.

emanresu A‭ wrote 6 months ago

Thanks! From my experience, for this sort of challenge, unless there's an implicit bound on the size/score of a solution, it tends to be a good idea to place an explicit upper bound and allow people to optimise from there - this challenge from Stack Exchange is a good example. Here, I'm not sure that would work as well because of the inherent linearity, but unbounded problems tend to have unbounded solutions more often than not.

trichoplax‭ wrote 6 months ago

Thank you. That's an interesting challenge - I'll bear that approach in mind for future challenges.