Talk:First perfect square in base n with n unique digits: Difference between revisions

Optimization when no extra digit required
(→‎Trailing zero: Forgot to add that this only applies for bases > 2.)
(Optimization when no extra digit required)
Line 355:
As checking for this is very cheap, it may knock about 4 or 5% from the run time for bases >= 20.
--[[User:PureFox|PureFox]] ([[User talk:PureFox|talk]]) 16:27, 27 May 2019 (UTC)
==Optimization when no extra digit required==
Let me do this in base 10 to maintain sanity. As discussed (Space compression and proof) the Digital Root n Base10 for n from 1 to 20 gives 1 4 9 7 7 9 4 1 9 1 4 9 7 7 9 4 1 9 1 4. Digital Root 1023456789 Base10 is 9. So every third square is a possible solution. As discussed (analytically determine minimum start value) start from 31992**2. First find the first value whose Digital Root Base10 is 9. In this case it is 31992. It should then only be necessary to try every third value to find the solution.<br><pre>
> [31992..3..999999] |> List.map(fun n->n*n) |> List.find(fun n->n=1026753849);;
Real: 00:00:00.068, CPU: 00:00:00.110, GC gen0: 5, gen1: 1
val it : int = 1026753849
 
> [31992..999999] |> List.map(fun n->n*n) |> List.find(fun n->n=1026753849);;
Real: 00:00:00.204, CPU: 00:00:00.300, GC gen0: 14, gen1: 2
val it : int = 1026753849
</pre><br>which seems to save the expected two thirds time. This is a little trickier when an extra digit is required because you would have to check each extra digit separately (they would have different Digital Roots).--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 22:03, 27 May 2019 (UTC)
2,171

edits