| Bytes | Lang | Time | Link |
|---|---|---|---|
| 084 | PowerShell | 240322T145419Z | James Fl |
| 004 | Vyxal 3 | 240321T141711Z | pacman25 |
| 105 | Swift 5.9 | 230712T022801Z | macOSist |
| 087 | Desmos | 230722T050953Z | Aiden Ch |
| 067 | C gcc | 230702T090917Z | Noodle9 |
| 035 | Wolfram Language Mathematica | 230706T231929Z | Domenico |
| 009 | Pyth | 230705T181306Z | CursorCo |
| 023 | Raku | 230705T185215Z | Sean |
| 008 | Japt E! | 230705T145101Z | Shaggy |
| 011 | Japt | 230705T140146Z | Luis fel |
| 054 | R | 230704T075307Z | Dominic |
| nan | Scala | 230702T011434Z | 138 Aspe |
| 155 | Racket | 230704T111055Z | Ed The & |
| 042 | Ruby | 230704T065920Z | G B |
| 040 | JavaScript V8 | 230703T064235Z | l4m2 |
| 4342 | Javascript | 230701T232118Z | Dadsdy |
| 026 | Lua | 230703T215519Z | bluswimm |
| 007 | Brachylog | 230703T123607Z | Fatalize |
| 006 | Jelly | 230703T083712Z | Unrelate |
| 005 | MathGolf | 230703T074449Z | Kevin Cr |
| 080 | Excel | 230703T062406Z | Jos Wool |
| 004 | Thunno 2 M | 230702T032926Z | noodle m |
| 011 | Extended Dyalog APL | 230702T123737Z | RubenVer |
| 058 | R | 230702T134357Z | pajonk |
| 083 | Java 8 | 230702T144631Z | Jack Amm |
| 046 | Factor + math.unicode projecteuler.037.private | 230702T035851Z | noodle m |
| 011 | Charcoal | 230702T110819Z | Neil |
| 042 | Arturo | 230702T090628Z | chunes |
| nan | Fig | 230702T050716Z | tybocopp |
| 006 | Nekomata + e | 230702T005904Z | alephalp |
| 039 | Retina 0.8.2 | 230701T235625Z | Neil |
| 039 | Python 2 | 230701T231104Z | Albert.L |
| 005 | 05AB1E | 230701T230312Z | Luis Men |
| 3375 | Vyxal g | 230701T221203Z | lyxal |
PowerShell, 84 bytes
$m=[math];if($m::sqrt($_)%1-eq0){return $m::sqrt($_[-1..-$_.length]-join'')%1-eq0}!1
Swift 5.9, 113 101 105 bytes
import Foundation
let x={Int(exactly:sqrt($0))!},e={x(.init($0+0))
x(.init(String("\($0)".reversed()))!)}
e(_:) is the closure you're looking for. Throws a fatal error when the check fails; does nothing when it succeeds.
Desmos, 87 bytes
h=.5
d=floor(logn)+1
f(n)=0^{mod(n^h+(∑_{k=0}^d.1mod(floor(n/10^k),10)10^{d-k})^h,1)}
Try It On Desmos! - Prettified
As you can tell, reversing the digits of a number in Desmos is a pain to do.
C (gcc), 75 73 67 bytes
b;f(n){b=sqrt(n);for(b=b*b-n;n;n/=10)b+=9*b+n%10;n=sqrt(b);b-=n*n;}
Saved 2 8 bytes thanks to c--!!!
Pyth, 10 9 bytes
-1 byte by realizing that v automatically vectorizes
sMI@R2v_B
Explanation
sMI@R2v_BQ # implicitly add Q
# implicitly assign Q = eval(input())
_BQ # bifurcate Q over reversal
v # eval (automatically vectorizes)
@R2 # map each to their square roots
I # check for invariance over
sM # converting the entire list into integers
Raku, 23 bytes
{sqrt($_|.flip)!~~/\./}
$_ | .flip is an or-junction of the input number and its flipped string representation, which Raku treats as a number in nearly all contexts. sqrt applied to that or-junction produces another or-junction that contains the square roots of the two numbers above. Then the function returns whether it is not (!) the case that either of those square roots match (~~) the regular expression containing a period (/\./).
R, 56 55 54 bytes
Edit: -1 byte after looking at pajonk's answer, and -1 byte by rearranging
\(x)(x^.5+(x%/%rev(z<-10^(0:log10(x)))%%10%*%z)^.5)%%1
Outputs zero (falsy) for reversed-squares, non-zero (truthy) for other numbers.
Based on the fact that there does not exist any pair of integers x and y that are not both squares, but for which sqrt(x)+sqrt(y) is an integer: see here.
Scala, 63 60 58 bytes
Saved 5 bytes thanks to the comments of @Shaggy and @Dominic van Essen
Golfed version. Try it online!
n=>(math.sqrt(n)+math.sqrt(n.toString.reverse.toLong))%1>0
Ungolfed version. Try it online!
object Main {
def main(args: Array[String]): Unit = {
def f(n: Long): Boolean = {
val sqrtOriginal = math.sqrt(n)
val sqrtReversed = math.sqrt(n.toString.reverse.toLong)
sqrtOriginal % 1 != 0 || sqrtReversed % 1 != 0
}
(0 until 10000).foreach(i => {
if (!f(i)) println(i)
})
val bigNumber = 1234567654321L
if (!f(bigNumber)) println(bigNumber)
}
}
Racket - 155 bytes
#lang racket
(define(m v)(let([r(string->number(list->string(reverse(string->list(number->string v)))))][? integer?])(and(?(sqrt v))(?(sqrt r)))))(m(read))
Explanation
On line one, we have Racket's required language statement, #lang racket, This just imports all of the statements/functions used in the language (like define and read, as well as wrapping our program into a module.
The second line is where the main program lies. We define a function named m. This function received an integer v as an argument. We then reverse the integer to obtain an integer named r. We take the square roots of both v and r and see whether they are integers or not. If both checks return true, then the number is a Reversed Perfect Square.
Here's the program in all its glory. In the shortened form, I embedded reverse-number as part of main's local scope. But to make it easier to read and understand, I made it its own function:
#lang racket
(define (reverse-number value)
(string->number (list->string (reverse (string->list (number->string value))))))
(define (main value)
(let ([reversed-value (reverse-number value)])
(and (integer? (sqrt value))
(integer? (sqrt reversed-value)))))
(main (read))
As of right now, Racket doesn't have a reverse-string or string-reverse function. So, the only way I could reverse the number was to first convert the number to a string, then convert the string to a list, reverse the list, convert the list to a string, then convert the string to a number. But if we did have a string-reverse function, I would've written:
(string->number (string-reverse (number->string value)))
Also, while I could've used quotient and remainder, it ended up bumping the number of bytes up by a tad bit (I got 180).
Example usages of main:
;; An infinite loop that loops over all natural numbers [1..] and finds all RPSs
(for ([i (in-naturals)] #:when (main i))
(displayln i))
JavaScript (V8), 40 bytes
g=x=>x**.5%1||g([...x].reverse().join``)
Throws as true and not throw as false.
Here assumes a reasonable stack(actually <1KB is enough, number is stored in heap) so it's allowed
C (gcc), 66 bytes
a;g(n){a=sqrt(n);a=a*a-n;}f(n){for(g(n);n;n/=10)a+=9*a+n%10;g(a);}
Strongly pushed Noodle9's
Javascript, 43 40 42 Bytes
Thanks to @Arnauld for -3 Bytes (-2 from backticks (which I forgot), -1 from inverse output)
n=>[...n].reverse().join``**.5%1*n**.5%1>0
Lua, 26 bytes
n=...a=n^.5&n:reverse()^.5
Outputs using the exit code (0 is true, 1 is false).
This works because Lua can't do bitwise operators on non-integer numbers. If either n^.5 or n:reverse()^.5 is not an integer, the program will error.
Brachylog, 7 bytes
↔;?~^₂ᵐ
Explanation
↔;? The list [reversed(Input), Input]
~^₂ᵐ Each element must be the square of some number
Jelly, 6 bytes
ṚƬḌ½ḞƑ
ṚƬ Reverse while unique.
Ḍ Vectorizing convert from decimal.
½ Vectorizing square root.
Ƒ Is the entire list of results unchanged by
Ḟ vectorizing floor?
MathGolf, 5 bytes
‼°x°╓
Or alternatively:
xαm°╓
Explanation:
‼ # Apply the next two operators separately on the current stack:
# (which will use the implicit input-integer)
° # Check if it's a perfect square
x # Reverse it
° # Check if this top reversed integer is a perfect square as well
╓ # Pop both, and get the minimum of the two
# (which will be truthy if both were truthy; or falsey if either/both were falsey)
# (after which the entire stack is output implicitly as result)
x # Reverse the (implicit) input-integer
α # Pair the top two values, which is the (implicit) input and the reversed input
m # Map over this pair:
° # Check for both whether it's a perfect square
╓ # Pop the square, and push its minimum
# (which will be truthy if both were truthy; or falsey if either/both were falsey)
# (after which the entire stack is output implicitly as result)
Excel, 80 bytes
=LET(
a,LEN(A1),
b,HSTACK(A1,CONCAT(MID(A1,1+a-SEQUENCE(a),1)))^0.5,
AND(INT(b)=b)
)
Thunno 2 M, 4 bytes
Requires v2.2.2: Try it
Ḳ,Ʋ
Thunno 2 G!, 6 bytes
Works on old version of Thunno 2.
ḲNKƭ1%
ḲBifurcate - duplicate and push reverseNCast reversed to integerKThe stack as a listƭThe square root of each1%Each mod 1 (0 if integer, nonzero else)Gflag - maximum, 0 iff both are integers!flag - logical not
This could be 4 bytes with m flag if we had an is_square built-in, or 5 bytes with same flag if we had an is_int built-in.
Extended Dyalog APL, 18 13 11 bytes
Thanks Adám for -5 bytes!
Takes input as a string.
≡∘⌊⍨∘√⍎,⍎⍤⌽
Explanation:
≡∘⌊⍨⍤√⍎,⍎⍤⌽
, ⍝ make a list of
⍎ ⍝ the input parsed as a number
⍎⍤⌽ ⍝ and the reversed input parsed as a number,
√ ⍝ take the square root of both,
≡∘⌊⍨ ⍝ is that list equal to itself after you floor it?
R, 59 58 bytes
\(n,t=10^(1:nchar(n)-1))n^.5%%1|(n%/%t%%10%*%rev(t))^.5%%1
\(n,t=10^(1:nchar(n)-1))any(c(n,n%/%t%%10%*%rev(t))^.5%%1)
Outputs flipped TRUE/FALSE.
Java 8, 83 bytes
n->Math.sqrt(n)%1==0&Math.sqrt(new Long(new StringBuilder(""+n).reverse()+""))%1==0
this lambda can be used for a functional interface such as IntPredicate
Factor + math.unicode project-euler.037.private, 46 bytes
-5 bytes thanks to @chunes
[ dup reverse-digits [ √ dup ⌊ = ] both? ]
[ ... ]Quotation (anonymous function) taking a numberdupDuplicate so number is on the stack twicereverse-digitsReverse the digits of the copy[ ... ] both?Apply the quotation to both and check if both returnt:√Square rootdupPush a copy⌊Round down=Are this and the original equal?
√ dup ⌊ = seems to be the shortest way to check if a number is a perfect square, since √ integer? doesn't work.
Charcoal, 11 bytes
¬Σ﹪₂I⟦θ⮌θ⟧¹
Attempt This Online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if a member of A061457, nothing if not. Explanation: Uses floating-point arithmetic, so will go wrong on large enough inputs.
θ Input as a string
θ Input as a string
⮌ Reversed
⟦ ⟧ Make into a list
I Cast to integer
₂ Take the square root
﹪ Modulo
¹ Literal integer `1`
Σ Take the sum
¬ Logical Not
Implicitly print
15 bytes for a theoretically correct version which actually has worse performance because it creates a list of length of the input:
Nθ¬⁻⟦θ⮌θ⟧X…·⁰θ²
Try it online! Link is to verbose version of code. Explanation:
Nθ First input as a number
θ First input
θ First input
⮌ Reversed
⟦ ⟧ Make into a list
⁻ Set difference with
…· Inclusive range from
⁰ Literal integer `0` to
θ First input
X Vectorised raise to power
² Literal integer `2`
¬ Logical Not
Implicitly print
23 bytes for a version that imports Python's math.isqrt:
⬤I⟦θ⮌θ⟧⁼ιX▷math.isqrtι²
Attempt This Online! Link is to verbose version of code. Explanation:
θ Input as a string
θ Input as a string
⮌ Reversed
⟦ ⟧ Make into a list
I Cast to integer
⬤ All values satisfy
ι Current value
▷math.isqrt Integer square root
X Raised to power
² Literal integer `2`
⁼ Equals
ι Current value
Implicitly print
37 bytes for a version that uses the Babylonian method to find the integer square root and so will work with arbitrarily large integers in linear time on the number of digits:
⊞υI⟦θ⮌θ⟧W⁼¹№υ⌊υ⊞υE⌊υ÷⁺κ÷§⌈υλκ²⁼⌈υX⌊υ²
Try it online! Link is to verbose version of code. Explanation:
⊞υI⟦θ⮌θ⟧
Start with the input and its reversal both as the target and the initial estimate.
W⁼¹№υ⌊υ
Repeat until the estimate converges.
⊞υE⌊υ÷⁺κ÷§⌈υλκ²
Get the next estimate.
⁼⌈υX⌊υ²
Check that the square of the final estimate gives the target.
Arturo, 42 bytes
$->n[0=+%sqrt do reverse~"|n|"1(sqrt n)%1]
$->n[ ; a function taking an argument n
0= ; is zero equal to
+ ; the sum between
%sqrt do reverse~"|n|"1 ; the square root of n reversed modulo one
(sqrt n)%1 ; and the square root of n modulo one
] ; end function
Fig, \$8\log_{256}(96)\approx\$ 6.585 bytes
!a%1mqw$
$ # Inupt reversed
w # Pair with the input (we now have [reversed(n), n])
mq # Square root of each one
%1 # Modulo 1
a # Any truthy (i.e. more than 0)
! # Logical NOT
Nekomata + -e, 6 bytes
¢B¢bÐ√
¢B¢bÐ√
¢B Integer to decimal digits in the reverse order
¢b Decimal digits to integer
Ð Pair
√ Square root; fails if not a perfect square
-e checks if there is a solution.
Retina 0.8.2, 39 bytes
$
¶$`
O$^`\G.
.+
$*
%A`(^1|11\1)+$
^¶$
Try it online! Link includes faster test cases. Explanation:
$
¶$`
Duplicate the input.
O$^`\G.
Reverse the first copy.
.+
$*
Convert to unary.
%A`(^1|11\1)+$
Delete square numbers.
^¶$
Check that both numbers were deleted.
Python 2, 39 bytes
lambda n:n**.5%1+int(`n`[::-1])**.5%1>0
Reversed squares, reversed output. Returns False if input is a square and reversed square, True if not.
05AB1E, 5 bytes
Â)ŲP
Prints 1 if the condition is met, 0 otherwise.
Try it online! Or verify all test cases.
How it works
 # Implicit input: n. Push n and (digit-)reversed n
) # Concatenate stack into an array
Ų # Is square? Element-wise
P # Product of array. Implicit print
Vyxal g, 30 27 bitsv2, 3.75 3.375 bytes
Ṙ"∆²
Explained
Ṙ"∆²
Ṙ" # the list [input, input reversed]
∆² # vectorise is square
# g flag takes the minimum which in this case is equivalent to checking if both items are truthy