(fn[n](nth(filter #(clojure.string/includes?(str(* % %))(str %))(range))n))
Try it online! (Unfortunately, TIO doesn't seem to support Clojure's standard string library)
If Clojure had shorter importing syntax, or had a includes?
method in the core library, this could actually be somewhat competitive. clojure.string/includes?
alone is longer than some answers here though :/
(defn nth-sq-subs [n]
(-> ; Filter from an infinite range of numbers the ones where the square of
; the number contains the number itself
(filter #(clojure.string/includes? (str (* % %)) (str %))
(range))
; Then grab the "nth" result. Inc(rementing) n so 0 is skipped, since apparently
; that isn't in the sequence
(nth (inc n))))
Since the TIO link is broken, here's a test run. The number on the left is the index (n
), and the result (N
) is on the right:
(mapv #(vector % (nth-sq-subs %)) (range 100))
=>
[[0 1]
[1 5]
[2 6]
[3 10]
[4 25]
[5 50]
[6 60]
[7 76]
[8 100]
[9 250]
[10 376]
[11 500]
[12 600]
[13 625]
[14 760]
[15 1000]
[16 2500]
[17 3760]
[18 3792]
[19 5000]
[20 6000]
[21 6250]
[22 7600]
[23 9376]
[24 10000]
[25 14651]
[26 25000]
[27 37600]
[28 50000]
[29 60000]
[30 62500]
[31 76000]
[32 90625]
[33 93760]
[34 100000]
[35 109376]
[36 250000]
[37 376000]
[38 495475]
[39 500000]
[40 505025]
[41 600000]
[42 625000]
[43 760000]
[44 890625]
[45 906250]
[46 937600]
[47 971582]
[48 1000000]
[49 1093760]
[50 1713526]
[51 2500000]
[52 2890625]
[53 3760000]
[54 4115964]
[55 5000000]
[56 5050250]
[57 5133355]
[58 6000000]
[59 6250000]
[60 6933808]
[61 7109376]
[62 7600000]
[63 8906250]
[64 9062500]
[65 9376000]
[66 10000000]
[67 10050125]
[68 10937600]
[69 12890625]
[70 25000000]
[71 28906250]
[72 37600000]
[73 48588526]
[74 50000000]
[75 50050025]
[76 60000000]
[77 62500000]
[78 66952741]
[79 71093760]
[80 76000000]
[81 87109376]
[82 88027284]
[83 88819024]
[84 89062500]
[85 90625000]
[86 93760000]
[87 100000000]
[88 105124922]
[89 109376000]
[90 128906250]
[91 146509717]
[92 177656344]
[93 200500625]
[94 212890625]
[95 250000000]
[96 250050005]
[97 289062500]
[98 370156212]
[99 376000000]]
This should be able to support any value of n
; providing you're willing to wait for it to finish (finding the 50th to 100th integers in the sequence took like 15 minutes). Clojure supports arbitrarily large integer arithmetic, so once numbers start getting huge, it starts using BigInt
s.
Is there any upper limit on the input? – maxb – 2018-11-28T08:58:33.117
@maxb No, for larger inputs, it should theoretically output the value. – Vedant Kandoi – 2018-11-28T09:16:21.867
1A lot of implementations will run into problems (for me its due to not being able to create arrays with more than 2^32 values), which will make most solutions bound to a maximum size by default. Should these solutions be disqualified? – maxb – 2018-11-28T09:23:15.297
1@maxb I think theoretically was meant as not necessarily practically. – Arnauld – 2018-11-28T09:24:37.257
@Arnauld the problem for my specific implementation was that I had an 8-byter which would solve until
n=9
, because create an array with 10^(input) values and filter it. – maxb – 2018-11-28T09:26:40.057@maxb Normally limitations such as that are ignored if an implementation on hardware with, say, arbitrarily large integer sizes and memory quantity would function as expected with all inputs. (posted before the response - limited to 9 is low even in this case). – Οurous – 2018-11-28T09:26:50.110
1@Ourous I know it's really low, that's why I don't like my solution. I could add a byte and have it work for much bigger inputs, so I'll add that as an alternative – maxb – 2018-11-28T09:29:10.463
1"N appears in N^2" would be better worded as something like "the decimal digits of N is a [contiguous] substring of the decimal digits of N squared" (11 does not "appear in" 121). [Strictly "contiguous" is redundant, but adding it is clear.] – Jonathan Allan – 2018-11-28T10:53:08.930
1@JonathanAllan Alternate suggested rewording: "N is lexicographically present in N^2" – Οurous – 2018-11-28T10:57:46.233