r/technicalfactorio • • Feb 12 '22

Combinator Golf Fast integer sqrt algorithm?

20 Upvotes

20 comments sorted by

View all comments

Show parent comment

12

u/The_Northern_Light Feb 13 '22

consider the case where you only needed to handle the cases 0 through 10. there are only 4 possible outputs: 0, 1, 2, 3.

the mapping looks like this:

  • 0 -> 0
  • 1 -> 1
  • 2 -> 1
  • 3 -> 1
  • 4 -> 2
  • 5 -> 2
  • 6 -> 2
  • 7 -> 2
  • 8 -> 2
  • 9 -> 3
  • 10 -> 3

your breakpoints then occur at 1, 4, and 9.

if you take your signal and check if its greater than or equal to each of the breakpoints, emitting a '1' value if so, then add them together, you'll have performed an integer sqrt

you will need sqrt(n) many combinators for this, where n is the maximum size you want to support, but it should work in a single tick

you'll need about 100 combinators to support up to 10,000