Square Square

April 10, 2018

We have today an opportunity for some code golf:

Write a program to find a four-digit positive integer that, when multiplied by itself, contains the original four-digit number in the last four digits of the product.

Your task is to write a program to find the desired number. If you wish, you can take it as a challenge to use the minumum number of symbols in your programming language. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.


Pages: 1 2

14 Responses to “Square Square”

  1. Perl being the true golfing language!
    perl -E’say grep{$_**2=~/$_$/}1000..9999′

  2. Perl being the true golfing language! – just realised 2 bytes too long!

    perl -E’say grep{$_**2=~/$_$/}1e3..1e4′
  3. Milbrae said

    Python 3

    for i in range(10**3, 10**4):
        if i**2 % 10**4 == i:
            print (i**2, i)

    87909376 9376

  4. Zack said

    Julia implementation (comprehensive & extensible version):

    ContainsOriginal(x::Int64, X::Int64) = string(x) == string(X)[end-3:end]
    for x = 1001:9999
    if ContainsOriginal(x, x^2); println(x); break; end

    result: 9376 (after about 0.001 sec, with about 42K memory allocations)

  5. John Cowan said

    Here’s a version in Basic that is well-suited to that language:

    10 PRINT 87909376

  6. Steve said

    Klong version

            f::{[l];l::[];{:[((x*x)!10000)=x;l::x,l;""]}'x; l}
  7. Steve said

    Mumps version

    f n=3164:1:9999 w:n**2#10000=n !,n


  8. Steve said

    Hats off 2 Perl. Your golfing skills are beyond compare!

  9. Globules said

    Here’s a Haskell version in the ghci REPL. Not much golfing; only the removal of spaces.

    > [n|n<-[1000..9999],n*n`rem`10000==n]
  10. matthew said

    Didn’t we do something like this recently? https://programmingpraxis.com/2016/06/28/curious-numbers/

    Anyway, here’s an anti-golf solution to the present problem, using TensorFlow (I’m just learning so this might be a terrible way to solve the problem):

    import tensorflow as tf
    def fn(previous, current):
        return tf.cond(tf.equal(current*current%10000,current),
                       lambda: current,
                       lambda: previous)
    elems = tf.Variable(list(range(1000,10000)))
    initializer = tf.constant(0)
    out = tf.foldl(fn,elems,initializer=initializer)
    with tf.Session() as sess:
  11. Alex B said

    Python 3 version in 46 characters:

    [x for x in range(1000,10000) if x*x%10000==x]

    Add `[0]’ for 49 characters if it needs to return an int instead of a list.

  12. Flaming Dorito said

    Python 3:

    I see some people have found the solution by finding the modulus with 10**4, thats probably more efficient than what I did, I found the last 4 digs of the square by converting to a list and indexing.

    for i in range(103, 104):
    sqr = list(str(int(i**2)))
    lis = list(str(i))
    if lis[:] == sqr[len(sqr) – 4:len(sqr)]:

  13. Flaming Dorito said

    For the range I meant 103, 104, it changed it for some reason and made it bold.

  14. Flaming Dorito said

    ugh, I mean 10 ^ 3, 10 ^ 4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: