| Bytes | Lang | Time | Link |
|---|---|---|---|
| 016 | APLNARS | 250616T174651Z | Rosario |
| 043 | GNU sed E + coreutils | 250613T134813Z | Toby Spe |
| 059 | Bash or any Bourne shell + coreutils + grep | 250520T172831Z | David G. |
| 054 | JavaScript Node.js | 250519T063752Z | l4m2 |
| 004 | Brachylog | 250610T125246Z | Fatalize |
| 122 | Python 3 | 250521T081344Z | Stef |
| 011 | Husk | 250524T102004Z | Glory2Uk |
| 105 | Racket | 250520T070123Z | Galen Iv |
| 022 | Wolfram Language Mathematica | 250519T073705Z | att |
| 019 | J | 250520T075657Z | Galen Iv |
| 042 | Maple | 250520T005618Z | dharr |
| 007 | Vyxal 3 l | 250519T045836Z | lyxal |
| 040 | Ruby | 250519T145646Z | Jordan |
| 006 | 05AB1E legacy | 250519T132324Z | Neil |
| 025 | Charcoal | 250519T132010Z | Neil |
| 042 | Retina 0.8.2 | 250519T074509Z | Neil |
| 009 | Uiua 0.17.0dev.1 | 250519T050515Z | Tbw |
APL(NARS), 16 chars
{(4=≢m)∧m≡∪m←π⍵}
Here 1 means true, 0 means false. It would say "the list of factors are of different elements, and them have to be 4 elements only", test:
{(4=≢m)∧m≡∪m←π⍵}1974
1
{(4=≢m)∧m≡∪m←π⍵}1975
0
GNU sed -E + coreutils, 48 43 bytes
Outputs the number and its factors for truthy; no output for falsey. It requires a recent version of factor with the -h option to "print repeated factors in form p^e unless e is 1" (TIO does not have a new enough version).
s/.*/factor -h &/e
/\^/d
/^(\S* ){4}\S*$/!d
First line does the factoring. Second line checks for repeated factors. Third line checks for exactly four spaces in the line.
Bash (or any Bourne shell) + coreutils + grep, 59 bytes
t(){ factor $1|egrep ':( [^ ]*){4}$'|egrep -v '( .*)\1\>';}
The function outputs the number and its factors like: 210: 2 3 5 7 for truthy, and empty for falsy. It also returns a standard shell truth value.
My test driver (see TIO) outputs the input, runs the function, and then outputs either "truthy" or "falsy" for good measure.
Some added recommended truthy tests: 1000000000254000000023512000000939666000013638807 (a 160 bit number, to overflow any native int), 1590 (2 * 3 * 5 * 53, tests that finding " 5 5" and making it falsy is wrong.)
JavaScript (Node.js), 54 bytes
f=(n,i=4,p=2)=>n<2?!i:n%p?f(n,i,p+1):n/p%p&&f(n/p,i-1)
Brachylog, 4 bytes
ḋ≠l4
Explanation
Another situation where having a built-in for "all-different" (which is useful for constraint/logic based programming in general) is valuable.
ḋ The prime decomposition of the input…
≠ …is a list of all distinct numbers…
l4 …and its length is 4
Python 3, 122 bytes
lambda x:len(l:=[p for p in f(x)if not f(p)])==4and x==math.prod(l)
f=lambda x:[p for p in range(2,x)if x%p<1]
import math
Shaved 5 bytes thanks to Rhaixer: lambda instead of def and <1 instead of ==0
Husk, 11 bytes
=+4§-LoΠup¹
If the input number is a product of 4 unique primes, output is 1, otherwise 0. Removing the first and the last symbols (9 bytes) will change the output either to the input value (truthy, except for 0) or to something else (falsy).
Commented
up -- get the unique prime factors
oΠ -- calculate the product
L up -- calculate the length = number of the unique factors
§- -- substract the length from the product
+4 -- add 4
= ¹ -- is the output the same as the input?
Husk, 7 bytes
€mΠṖ4İp
If the result is truthy - outputs a whole number, otherwise the program will loop infinitely.
Commented
İp -- generate a list of primes
Ṗ4 -- make a list of 4 element permutations
mΠ -- calculate the products
€ -- check whether the input number belong to the output list
Racket, 105 bytes
(require math/number-theory)(define(f n)(let((q(prime-divisors n)))(and(= 4(length q))(= n(apply * q)))))
Wolfram Language (Mathematica), 24 22 bytes
-2 thanks to LegionMammal278
8#2&@@√#==PrimeNu@#&
Returns an indefinite (falsy) form 0 == PrimeNu[0] when given 0. Use === (+1 byte) instead for False.
==PrimeNu@# #distinct prime factors equals...
8#2&@@√# (input = a*b^2, b maximal)
a=1: input
b=1: 8*1/2
else 8*√a
J, 22 19 bytes
1(=4*/@{.~.@q:)@>.]
1(=4*/@{.~.@q:)@>.]
1 >.] NB. max of 1 and the argument
( )@ NB. do (the inside is a hook)
~.@q: NB. unique prime factors
4 {. NB. take the first 4 factorsfactors row
NB. (padded with zeroes if needed)
*/@ NB. and find their product
= NB. is it equal to the argument of the hook?
Maple, 42 bytes
n->evalb(map2(op,2,ifactors(n)[2])=[1$4]);
Vyxal 3 -l, 7 (literate) bytes
prime-exponents
1 4 list-repeat
===
1 4 list-repeat is a 3 byte way of writing [1, 1, 1, 1]
Ruby, 40 bytes
Port of Neil’s 05AB1E solution; give him an upvote.
->n{n>0&&n.prime_division.sum{_2**3}==4}
05AB1E (legacy), 6 bytes
Ó3mO4Q
Try it online! Explanation: Checks that the sum of the cubes of the prime exponents equals 4.
Charcoal, 25 bytes
Nθ≔Φ…²θ¬﹪θιυ∧⁼¹⁴Lυ⁼υ⁻υXυ²
Try it online! Link is to verbose version of code. Explanation:
Nθ
Input n.
≔Φ…²θ¬﹪θιυ
Find the nontrivial proper factors of n.
∧⁼¹⁴Lυ⁼υ⁻υXυ²
Check that there are 14 and none of them are square numbers.
Retina 0.8.2, 42 bytes
.+
$*_
(.+)(\1)*(?=\1$)
$#2¶
D`
^(.+¶){4}_
Try it online! Link includes smaller test cases. Explanation:
.+
$*_
Convert to unary using _s.
(.+)(\1)*(?=\1$)
$#2¶
Decompose into prime factors (except subtract 2 from each factor), leaving a trailing _.
D`
Replace duplicate prime factors with empty lines.
^(.+¶){4}_
Check that there are exactly four non-empty lines before the _.
Uiua 0.17.0-dev.1, 9 bytes SBCS
≍⇡4⊛°/×↥1
Takes input on stack and outputs 1 for true and 0 for false.
Explanation
≍⇡4⊛°/×↥1
↥1 # round up to 1
°/× # prime factorization
⊛ # unique index for each unique factor
≍⇡4 # is it == range(4)?