| Bytes | Lang | Time | Link |
|---|---|---|---|
| 093 | AWK | 250314T212849Z | xrs |
| 247 | Go | 240815T144706Z | bigyihsu |
| 015 | Pyth | 240814T201012Z | Glory2Uk |
| 073 | Python 3.6 with IO | 220212T070451Z | Deera Wi |
| 020 | Charcoal | 220219T144142Z | Neil |
| 037 | Julia 1.0 | 220215T144411Z | MarcMush |
| 047 | Julia 1.0 | 220216T231713Z | amelies |
| 091 | Julia | 220213T210704Z | David Sc |
| 061 | Perl 5 nE | 220211T163845Z | Sundar R |
| 010 | Vyxal | 220213T053145Z | lyxal |
| 064 | Perl 5 ap | 220211T111139Z | Kjetil S |
| 033 | MATL | 220211T180304Z | Sundar R |
| 037 | Vyxal | 220211T161250Z | math sca |
| 068 | Python 2 | 220211T095701Z | ovs |
| 051 | Haskell | 220211T080613Z | AZTECCO |
| 008 | 05AB1E | 220211T082426Z | Kevin Cr |
| 092 | Python 3.8 prerelease | 220211T035825Z | wasif |
| 053 | JavaScript Node.js | 220210T142517Z | l4m2 |
| 125 | C gcc | 220210T230908Z | Noodle9 |
| 055 | Retina 0.8.2 | 220210T200404Z | Neil |
| 029 | Burlesque | 220210T165500Z | DeathInc |
| 042 | R | 220210T163022Z | Dominic |
| 007 | Brachylog | 220210T163654Z | Fatalize |
| 012 | Husk | 220210T160152Z | Dominic |
AWK, 93 bytes
{for(;(x=i++^2)<$1;)printf$1~"^"x&&(z=substr($1,length(x)+1))!~/^0/&&(m=sqrt(z))==int(m)?1:X}
Will print a '1' for every match. No ones, no matches.
Go, 247 bytes
import(."fmt";."math")
func f(n int)bool{s,a,b:=Sprint(n),0.0,0.0
for i:=range s{c,d:=s[:i],s[i:]
if len(c)<1||len(d)<1||(len(d)>1&&d[0]<'1'){continue}
Sscan(c+" "+d,&a,&b)
A,B:=Sqrt(a),Sqrt(b)
if A==Round(A)&&B==Round(B){return 0<1}
}
return 1<0}
Pyth, 15 bytes
}Qmjk^R2d*SsQUs
A program that takes an integer value as a string (because the trailing 0 is recognized as a a separate value) and outputs True for the 'concats of two squares', False otherwise. The calculation time for the large values exceeds 1 minute.
Explanation:
}Q # check if the input value is in the list.
m # The list is constructed by mapping:
jk # concatenation
^R2 # and squaring
d # of the argument d
* # mapped over the cartesian product
SsQ # of the range from 1 to Q
Us # with the range from 0 to Q-1
Python 3.6 (with IO), 73 bytes
n=input();l=range(int(n));print(n in[f"{i*i}{j*j}"for i in l for j in l])
You could also use,
n=input();l=range(int(n));print({n}&{f"{i*i}{j*j}"for i in l for j in l})
But unfortunately it doesnt output booleon values rather it outputs, {n} for True and set() for False
slightly longer answer (By 2 bytes)
n=int(input());print(str(n)in[f"{(k//n)**2}{(k%n)**2}"for k in range(n*n)])
and without a loop
n=int(input());m=k=0;exec('m+=f"{(k//n)**2}{(k%n)**2}"==str(n);k+=1;'*n**2);print(m>0)
Charcoal, 20 bytes
⊙…¹Iθ⊙…⁰Iθ⁼θ⪫X⟦ιλ⟧²ω
Try it online! Link is to verbose version of code. Takes input as a string and outputs a Charcoal boolean, i.e. - for a concatenation of two squares, nothing if not. Explanation:
… Range from
¹ Literal integer `1`
θ To input
I Cast to integer
⊙ Does any satisfy
… Range from
⁰ Literal integer `0`
θ To input
I Cast to integer
⊙ Does any satisfy
θ Input as a string
⁼ Equals
⟦ιλ⟧ List of loop variables
X Vectorised to power
² Literal integer 2
⪫ ω Joined
Julia 1.0, 38 47 bytes
~a=string.(a.^2);!a=any(~(0:a)'.*~(0:a).=="$a")
Based on MarcMush’s answer (tnx for pointing out the error when squaring)
Julia, 130 122 98 91 bytes
!x=√parse(Int,x)%1
f(a,s="$a",l=length(s))=0∈1:~-l.|>i->s[l-i]<'1'||!s[i+1:end]+!s[1:i]
Thanks to Sunda R we can save 8 bytes!
Thanks to MarcMush we can save 32 39 bytes!
Perl 5 -nE, 78 61 bytes
say int=~/^((.+)(?(?{$+eq(0|$+)&(0|sqrt$+)**2==$+})|^))(?1)$/
A whopping -17 bytes thanks to @kjs.
Explanation:
say int # convert input to number to remove leading 0s
=~ # and match it against the regex
/^( # match from the beginning of input
(.+) # match 1 or more digits
(? # evaluate a condition here
(?{ # the condition is the result of this code
$+ # the digits matched so far are in $+
eq # string equality check
(0|$+) # int($+) - golfy way of writing it
# if they're equal, no leading 0s here
&
(0|sqrt$+) # again, golfy form of int(sqrt($+))
**2==$+} # square that and compare to original number
) # if no leading zeroes and number is a square
# do nothing and move on
| # else
^ # add an impossible pattern here so regex match fails
)
)
(?1) # now do the same match again, for the second square
$ # and that should be the end of input
/x;
The trick is in exploiting regex "backtracking" mechanism. Any time the part of the string we're looking at doesn't match the conditions we want, we make the match fail deliberately so that the regex engine chooses a different subset of the input string for the /(.+)/, and automatically tries the match again.
Vyxal, 10 bytes
›₌ɾʁẊ²vṅ⌊c
An 05AB1E port.
Explained
›₌ɾʁẊ²vṅ⌊c
› # Increment the input
₌ɾʁ # The range [1, input + 1] and the range [0, input + 1)
Ẋ # Cartesian product of those ranges
² # Square each number in each pair
vṅ⌊ # Integer version of each pair concatenated into a single string
c # Does that contain the original input?
Perl 5 -ap, 69 64 bytes
$_=grep{1<grep/^0$|^[^0]/&sqrt!~/\./,unpack$_."A*","@F"+0}A1..A9
MATL, 33 bytes
Vf3L)"GV@:&),tUV=A3MtX^kU=*w]*?T.
Outputs 1 for truthy, nothing for falsey.
V % take in number (ignoring initial 0s) and convert to string
f % get list of its indices (starting from 1)
3L) % remove the last index
" % for 1 to end-1
GV % get input as string again, say S
@ % get the current loop index i
: % form range 1:i
&) % split S[1:i] and S[i+1:end]
, % for each split part
t % duplicate it
U % convert to numeric
V % and back to string - this gets rid of initial 0s
=A % condition 1: did that give the same string back?
3M % get the numeric form N of the current part again
tX^kU % floor(sqrt(N))^2
= % condition 2: did that get the number back
% i.e. is it a square number
* % AND those conditions
w % swap the other part in to be checked
] % end
*? % if both parts satisfied the conditions
T % save True to output
. % break
% (implicit) end
% (implicit) end
% (implicit) convert to string and display
Vyxal, 37 bytes
Probably can be golfed a lot, but this is too long for me.
So basically for every i in input n, it checks if both n[:i] and n[i:] are squares.
I think the time complexity is \$O(2*\mathrm{len}(n)) \$
Lʁƛ?fnȯṅ:h⌊[⌊|3]∆²→?fẎṅ:h⌊[⌊|3]∆²←∧;a
Explanation:
Lʁƛ?fnȯṅ:h⌊[⌊|3]∆²→?fẎṅ:h⌊[⌊|3]∆²←∧;a
Lʁ Exclusive 0 range of length(n)
ƛ Map through range
?f Push stringified n
nȯṅ Slice from loop index to end
:h⌊[⌊|3] Duplicate slice, if int(head) = 0, push 3 (non-square) else int(slice)
∆²→ Set var to "is a perfect square?"
?f Push stringified n
Ẏṅ Slice from 0 to loop index
h⌊[⌊|3] Duplicate slice, if int(head) = 0, push 3 (non-square) else int(slice)
∆² Is it a perfect square?
← Get var
∧ Are both are squares?
;a End map, check if any element is true.
Python 2, 68 bytes
Inefficient and somewhat boring approach.
lambda n:`n`in('%d'*2%(a*a,b*b)for a in range(1,n)for b in range(n))
Python 2, 77 bytes
Much more efficient and interesting.
f=lambda n,m=0,d=1:n and((m*10<d!=10)==n**.5%1+m**.5%1)|f(n/10,m+n%10*d,d*10)
Haskell, 54 51 bytes
f n|l<-show.(^2)<$>[0..n]=elem(show n)$(++)<$>l<*>l
- Thanks to @ovs for saving 3 Bytes.
So much inefficient approach:
l is all numbers up to input squared and to string.
We search trough the powerset of l if there is a pair that form the input
05AB1E, 8 bytes
ÝāsânJIå
Based on my 05AB1E answer for the Sum of two squares challenge.
Try it online or verify (almost) all test cases (151296561 is omitted since it'll timeout).
Explanation:
Ý # Push a list in the range [0, (implicit) input]
ā # Push a list in the range [1,length] (without popping the list)
s # Swap so the [0,input] list is at the top
â # Cartesian product: create all possible pairs using these two lists
n # Square each inner integer
J # Join each pair together
Iå # Check if the input is in this list
# (after which the result is output implicitly)
We can't just use the range \$[0,input]\$ twice, since it would create a false positive with for example "09" and input 9. We also can't just use the range \$[1,input]\$ twice, since it would fail with for example input 90. Hence the use of the range \$[1,input+1]\$ with \$[0,input]\$ for the creation of pairs with the cartesian product instead.
Python 3.8 (pre-release), 92 bytes
f=lambda n,x=1:x<len(n)and all(e[0]!='0'and int(e)**.5%1==0for e in[n[:x],n[x:]])or f(n,x+1)
-28 for @ophact's tip
JavaScript (Node.js), 56 53 bytes
2 byte from tsh
x=>(g=_=>x--&&x*x+'|'+g(),x+g).match(`^(${g()}){2}_`)
RegEx with many choices
C (gcc), 127 125 bytes
i;r;q(n){for(r=i=n;i;--i)r=r&&i*i-n;i=!r;}t;h;m;f(n){for(t=h=0,m=1;n>9;n/=10,m*=10)n%10|m<2?h+=n%10*m,t|=q(n/10)*q(h):0;n=t;}
Saved 2 bytes thanks to ceilingcat!!!
Retina 0.8.2, 55 bytes
^0+
\B
$'¶$`;
A`;0.
\d+
$*
m`(^1|11\1)+;(1(?(2)1\2))*$
Try it online! Link includes faster test cases (a ^ after the m` would speed it up enough to be able to check 151296561 on TIO). Outputs the count of square concatenations, so for example 144100 can be split up as 144 and 100 or alternatively as 1 and 44100, so the output would be 2 in that case. Explanation:
^0+
Delete leading zeros.
\B
$'¶$`;
Try splitting between every pair of digits.
A`;0.
Delete numbers with leading zeros.
\d+
$*
Convert to unary.
m`(^1|11\1)+;(1(?(2)1\2))*$
Match two square numbers. (1(?(2)1\2))* is a generalised match for a square number; (^1|11\1)+ is a simplified special case for a non-zero square to be matched at the start (of a line).
Burlesque, 29 bytes
Jsuf{?sXXsm}0_+)up2CB)++jup~[
J # Duplicate
su # Substrings of
f{ # Filter for
?s # Sqrt
XX # [floor, ceil]
sm # Same (i.e. is int)
} # -- Is square
0_+ # Append 0 to list
)up # Unparse each to str
2CB # Combinations of pairs
)++ # Mapped Concat
j # Reorder stack
up # Unparse input
~[ # Is in list
R, 42 bytes
function(x)x%in%outer(y<-(0:x)^2,y,paste0)
Same horribly-inefficient approach as my Husk answer.
R's %in% function to search for an element in a vector conveniently appears to cast its arguments to the same type (here the numeric input x and the character vector of pasted-together squares), which was a nice surprise to me.
Note: After reading Fatalize's comment, I realise that this answer is also very close to pajonk's answer to the "Sum of two squares" challenge. Note though that that answer wasn't the shortest in R, so a small change to the challenge does seem to favour a completely different approach, which is also nice.
Brachylog, 7 bytes
~cĊ~^₂ᵐ
Explanation
~cĊ Deconcatenate into a couple of 2 elements Ċ
Ċ~^₂ᵐ Each element of Ċ must be the square of a number
Husk, 12 bytes
€s¹Σ´Ṫ+mos□ŀ
Outputs nonzero for 'concats of two squares', zero for numbers that aren't.
Builds a list of 'concat of two squares' numbers, and then checks if the input is present. So not very efficient for large inputs...
€s¹Σ´Ṫ+
mos # get strings of
□ŀ # all squares up to input,
´Ṫ # combine all combinations
+ # by concatenation,
Σ # flatten the list-of-lists
€s¹ # and look for the input as a string