20
\$\begingroup\$

An Automorphic number is a number which is a suffix of its square in base 10. This is sequence A003226 in the OEIS.

Your Task:

Write a program or function to determine whether an input is an Automorphic number.

Input:

An integer between 0 and 10^12 (inclusive), that may or may not be an Automorphic number.

Output:

A truthy/falsy value indicating whether or not the input is an Automorphic number.

Examples:

0           -> truthy
1           -> truthy
2           -> falsy
9376        -> truthy
8212890625  -> truthy

Scoring:

This is , lowest score in bytes wins.

\$\endgroup\$
  • 9
    \$\begingroup\$ Btw the limit of 1e12 means submissions will need to handle numbers up to 1e24, which is an 80-bit number. If handling numbers that large is a hard requirement, many of the exiting answers are invalid. \$\endgroup\$ – Dennis Jun 24 '17 at 19:59
  • \$\begingroup\$ Need we handle numbers that would lead to precision issues in our chosen language? \$\endgroup\$ – Shaggy Jun 24 '17 at 21:50
  • \$\begingroup\$ Provided that you don't abuse the standard loophole about that, then that would be fine. \$\endgroup\$ – Gryphon Jun 24 '17 at 22:49
  • \$\begingroup\$ Specifically, this loophole \$\endgroup\$ – Gryphon Jun 24 '17 at 22:53
  • \$\begingroup\$ It's been a loooong day and I am very, very tired but your comments read to me as validating my JS solution. Could you confirm that? (No issue deleting if not) \$\endgroup\$ – Shaggy Jun 24 '17 at 23:40

33 Answers 33

1
2
0
\$\begingroup\$

Java 8, 36 bytes

n->(n.multiply(n)+"").endsWith(n+"")

Input is a java.math.BigInteger, output a boolean.

Explanation:

Try it here.

n->                   // Method with BigInteger parameter and boolean return-type 
  (n.multiply(n)+"")  //  Return if the String representation of n*n
   .endsWith(n+"")    //  ends with the String representation of n
                      // End of method (implicit / single-line body)
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3, 40 35 bytes

-@Nick A saved 5 bytes: use of endswith()

lambda x:str(x*x).endswith(str(x)) 

Try it online!

Python 2, 31 bytes (Does not work for large numbers)

lambda x:`x*x`[-len(`x`):]==`x`

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Doesn't work on 8212890625 because x*x will end with "L". You can however save two bytes by removing the f= I think. \$\endgroup\$ – nore Jun 24 '17 at 19:07
  • \$\begingroup\$ It would work in python 3 though, right? \$\endgroup\$ – officialaimm Jun 24 '17 at 19:15
  • 1
    \$\begingroup\$ You must use f=\ not f=/... \$\endgroup\$ – Erik the Outgolfer Jun 24 '17 at 19:15
  • 1
    \$\begingroup\$ Yes that will work then. \$\endgroup\$ – nore Jun 24 '17 at 19:19
  • 1
    \$\begingroup\$ Would using endswith and removing s=str not be shorter for python3?: lambda x:str(x*x).endswith(str(x)) \$\endgroup\$ – user52452 Jun 26 '17 at 10:20
0
\$\begingroup\$

Haskell, 45 bytes

f=reverse.show
g a=and$zipWith(==)(f a)$f$a^2

Try it online!

Haskell + Data.Function, 42 bytes

g a=and$on(zipWith(==))(reverse.show)a$a^2

Try it online!

Haskell + Data.Function + Control.Monad, 40 bytes

and.ap(on(zipWith(==))$reverse.show)(^2)

Try it online!

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.