fPart((Ans-1ans−1)!²/Ans²%ans
This outputs 0 for non-prime numbers and something in the range 0 < number < 1 for prime numbers.* If you want a proper 1 for prime numbers, prepend either not(not( or 0≠ for a 12 byte solution. But this is not actually required for a "truthy" value, because for example If .5 executes the "Then" statement.
This solution is based on this Labyrinth answer, but theThe great thing about programming on"programming" in a calculator is that you can basically just enter formulas directly. The only part of
Example usage in Bash:
echo "5" | qalc -t -f - "(ans−1)! ²%ans"
Note that formula that's not a builtin is a%b. I hadadded a space after the exclamation mark only because Bash otherwise tries to use interpret "fPart(a/b)!²" as an "event". It's not actually required by qalc, which is the fractional part of the division. Newer calculator models and maybe even newer operating systemscan be seen by for example creating this onefile:
5 (ans−1)!²%ans
…and then executing it with (I'm using 2.43, 2qalc -t -f test.txt.55 is available, but horrendously laggy and counterintuitive)
Other consoles or other ways of calling qalc than Bash might not have a modulo builtin, so this program would probably be one byte shorter with thoseproblem.
The program reads from and writes to the variable "calculation can also be entered in "interactive mode" by simply running Ansqalc -t", which is basicallyentering the StdInnumber and StdOut ofthen entering the TI-84+ calculatorexpression on the next input. IfThis also works in the last instruction of aGUI program results"Qalculate".
Meta explanation:
The output is 0 for non-prime numbers and a positive integer for prime numbers. I let this run in a numberloop for all numbers up to 10000 and it was always 1 for prime numbers, thenbut I haven't checked whether that number is automatically printedguaranteed yet. But you can also
This solution is based on this Labyrinth answer. I essentially just runfound something (qalc) that linehas all the parts of the formula as an instruction inbuiltins instead of having to program all the regular calculator viewparts myself on a lower level.
As a program with the name "The P-t", argument hides the file is 20 bytes largeinterpreted calculation, of which one byte is the file name and nine bytes are metadata etc. I'm not quite sure whetherso that counts as part of the byte count foroutput of the answer, but since it can also be executed as a simple calculation statement, I would rather not count it. Writing a program is not actually required to executeexample command looks like this.:
Breakdown:
fPart((Ans-1)!²/Ans5 Ans the previous output, handled just like any variable1 Ans
…instead of like this:
5 = same5 rem( factorial(ans - 1) subtract one^2, of course ! builtin factorial function ² builtin square/second power function fPart( fractional part of a decimal (internally actually complexans) number fPart((Ans-= 1)!²/Ans
A closing bracket atThe tricky part was reading the endnumber in. It's pretty easy to insert the number into the formula two times, but that would mean modifying the "program", which is actuallysurely not needed, TIallowed. There seems to also be no way to execute multiple expressions non-Basic understands it anywayinteractively with qalc, except by reading in a file.
The optional In this case, I used not(- I mentioned returns 1 for an input of 0as a "file", which is StdIn, and 0 otherwise. Applyingused that twice leaves 0 unchanged and turns everything else into 1only to store the input in ans, which is implicitly done by qalc. You could also say that 0≠ans acts exactlyis the sameinput of the "program", in which case the program is simply the 13 bytes seen at the top. It could also be stored in any other variable, in which case the solution could be shortened by 4 bytes.
For the first few prime numbers, the result of that modulo operation is always 1. That might be random or it might be guaranteed, I haven't checked that yet.
If it's guaranteed, you can also prepend Ans for an 11 byte solution with an output of 1originally wrote this answer for prime numbers (because b·fPart(a/b) is the same as a%b).TI-Basic:
The catch
fPart((Ans-1)!²/Ans
*This program has one big issue, however: Due to imprecisionsThat's just 10 bytes, this program only correctly identifiesbut it fails on the prime numbers up to 11input 13 already, afterwards it claims everythingdue to be a non-prime number. Everything above 42 even causes an overflow error, because 42floating point imprecisions in those large numbers (12!² is bigger than 10¹⁰⁰²=229442532802560000). So the check for 9 is actually the only one within the working range that differs from a check fordifference to an even/odd numbers.
Alternatively, this formula can be used inchecker within the command line calculator "qalc" thatworking range is often preinstalled on Linux9 and init does not fulfil the GUI equivalent "Qalculate":
(42−1)!²%42 returns 0.
(13-1)!²%13 returns 1criterion to work up to 255.
(100000007-1)!²%100000007 takes a while and then returns For inputs above 42 it even crashed from an overflow 1.
But I wouldn't call inserting that number there a "programming language". Variables could be defined before the execution and then used in the calculation, which might make that a valid solution, but I haven't played around with that much yet(42!²>10¹⁰⁰).