Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Smallest number such that concatenation is a square

Challenge

Write a program or function that takes a number \$n\$ and returns the smallest \$k\$ such that concatenation \$n'k\$ is a square. This sequence is described by A071176 on the OEIS.

I/O Examples

input --> output

1   -->    6 (4^2)
10  -->    0 (10^2)
35  -->  344 (188^2)
164 -->  836 (406^2)
284 --> 2596 (1686^2)

Rules

Answer*

Cancel
4
  • \$\begingroup\$ You can remove the parenthesis surrounding Math.sqrt(new Long(n+j))%1 to save 2 bytes. \$\endgroup\$ Commented Jan 29, 2020 at 12:53
  • \$\begingroup\$ @KevinCruijssen True! In an initial version I needed them, but while refactoring I didn't realise I could remove them. Thanks! \$\endgroup\$
    – GuestUser
    Commented Jan 29, 2020 at 12:59
  • \$\begingroup\$ Np. Btw, in your second version you can also save 2 bytes: 1 by changing long i=4;String a;while(...); to String a;for(long i=4;...;); to save on the semi-colon, and 1 by removing the + in your regex, since the .* already covers this. Nice answer, btw! And welcome to CGCC! :) \$\endgroup\$ Commented Jan 29, 2020 at 13:07
  • 1
    \$\begingroup\$ @KevinCruijssen Thanks again! I totally missed the long inside the for hack. And again the "+" was becuase of a previous version in which I didn't have the ".*|0" part, but whitout that part I didn't cover for the f(10) -> 0 case correctly and other solution with a 0 in them , but when I added it I didn't realise the "+" wasn't necessary any more. Really nice catch :). \$\endgroup\$
    – GuestUser
    Commented Jan 29, 2020 at 14:16