| Bytes | Lang | Time | Link |
|---|---|---|---|
| 033 | TIBASIC TI84 Plus CE Python | 250929T145547Z | madeforl |
| 012 | Japt | 250928T154056Z | Shaggy |
| 080 | Wolfram Language Mathematica | 250928T120808Z | 138 Aspe |
| 017 | APLNARS | 250921T141753Z | Rosario |
| 043 | Python | 160401T205612Z | Anders K |
| 018 | MATL | 160401T213323Z | Suever |
| 043 | MATLAB | 160401T210608Z | Suever |
| 053 | Python | 160401T204155Z | mriklojn |
TI-BASIC (TI-84 Plus CE Python), 33 bytes
Input C
2→A
1→B
While C>A
B+A→B
B-A→A
End
C=A
simple
Wolfram Language (Mathematica), 80 bytes
Port of @Suever's MATLAB answer in Mathematica.
80 bytes, it can be golfed more.
Golfed version. Try it online!
!(Abs@Mod[Re@Sqrt[5*#^2-20],1]>10^(-10)&&Abs@Mod[Re@Sqrt[5*#^2+20],1]>10^(-10))&
Ungolfed version. Try it online!
(* More explicit version of the MATLAB function *)
checkProperty[n_] := Module[{expr1, expr2, sqrt1, sqrt2, real1, real2,
remainder1, remainder2, allNonZero},
(* Calculate the two expressions inside the square roots *)
expr1 = 5*n^2 - 20; (* 5*n^2 + (-20) *)
expr2 = 5*n^2 + 20; (* 5*n^2 + 20 *)
(* Take square roots *)
sqrt1 = Sqrt[expr1];
sqrt2 = Sqrt[expr2];
(* Get real parts (in case of complex numbers) *)
real1 = Re[sqrt1];
real2 = Re[sqrt2];
(* Check remainders when divided by 1 (like MATLAB's rem function) *)
remainder1 = Mod[real1, 1];
remainder2 = Mod[real2, 1];
(* MATLAB's all() checks if ALL remainders are non-zero *)
(* Account for floating point precision by checking if remainder > threshold *)
allNonZero = (Abs[remainder1] > 10^(-10)) && (Abs[remainder2] > 10^(-10));
(* Return the negation (NOT all non-zero remainders) *)
(* This means at least one square root IS an integer *)
!allNonZero
]
(* Test cases function *)
runTests[] := Module[{testCases, results},
testCases = {{3, True}, {4, True}, {7, True}, {8, False}, {10, False}, {3421, False}, {9349, True}};
Print["Testing the function:"];
Print["Input\tOutput\tExpected\tPass"];
Print["------------------------------------"];
results = Map[
Function[{testCase},
Module[{input, expected, actual, pass},
input = testCase[[1]];
expected = testCase[[2]];
actual = checkProperty[input];
pass = (actual === expected);
Print[input, "\t", actual, "\t", expected, "\t\t", pass];
(* Debug information for failed cases *)
If[!pass,
Print[" Debug - expr1: ", 5*input^2 - 20, ", expr2: ", 5*input^2 + 20];
Print[" Debug - sqrt1: ", N[Sqrt[5*input^2 - 20]], ", sqrt2: ", N[Sqrt[5*input^2 + 20]]];
];
pass
]
],
testCases
];
Print["\nAll tests passed: ", AllTrue[results, Identity]];
]
(* Run the tests *)
runTests[]
APL(NARS), 17 chars
0∊1∣√5×(⎕*2)+¯4,4
I tried to copy the formula from other answers...
test:
0∊1∣√5×(⎕*2)+¯4,4
⎕:
3
1
0∊1∣√5×(⎕*2)+¯4,4
⎕:
4
1
0∊1∣√5×(⎕*2)+¯4,4
⎕:
7
1
0∊1∣√5×(⎕*2)+¯4,4
⎕:
8
0
0∊1∣√5×(⎕*2)+¯4,4
⎕:
10
0
0∊1∣√5×(⎕*2)+¯4,4
⎕:
3421
0
0∊1∣√5×(⎕*2)+¯4,4
⎕:
9349
1
MATL, 19 17 16 18 bytes
2^5*20t_h+X^Xj1\~a
Explanation
2^ % Implicitly grab the input and square it
5* % Multiply the result by 5
20 % Create the literal number 20
t_ % Duplicate and negate the second 20
h % Concatenate 20 and -20 to get [20 -20]
+ % Add the array to the result of 5*n^2
X^ % Compute the square root
Xj % Ensure that the result is a real number
1\ % Compute the remainder to test for perfect squares
~a % Check to see if any had a remainder of zero (returns a boolean)
Python, 69 67 53 characters
lambda x:0in(((5*x**2+20)**.5)%1,((5*x**2-20)**.5)%1)
This works in both Python 2.x and 3.x.
See this answer to see how it works - n is a Lucas number if either 5n^2+20 or 5n^2-20 is a perfect square. My program checks to see if either of their square roots are integers and checks if 0 is in the tuple that results.