JavaScript(ES6),63 61字节
g=(e,i=e)=>i--<3?1:e%i?g(e,i):g(i)+1
f=e=>e>1&g(e)<2&g(e+2)<3
Test cases:<br><textarea id=i rows=6 oninput="go()">101 223 233 1</textarea><br><pre id=q></pre><script>window.onload=function go(){document.getElementById('q').innerHTML=document.getElementById('i').value.split('\n').map(e=>e+' -> '+f(+e)).join('\n')}</script>
定义一个函数f
这需要n
作为参数并返回结果。我对结果感到非常满意g
;它计算一个数字中素数的数量。
多亏了Kevin Cruijssen的&
把戏,节省了2个字节。
不打高尔夫球
Ω = (n, // Ω(n) = number of n's prime factors, n > 1.
i = n) => // Start iterating from i = n - 1. Since we'll immediately
// decrement i, n is used here.
--i // Immediately decrement i.
< 2 // If i = 0 or i = 1, n is a prime at this point.
? 1 : // Therefore Ω(n) = 1.
n % i != 0 ? // If n is not divisible by i,
Ω(n, i) // try again with i := i - 1 (immediately decremented, so use i).
: Ω(i) + 1 // n is divisible by i. Since we're counting down from n - 1
// and i is the first such number, i is n's largest non-trivial
// divisor, and thus n/i is a prime.
// Therefore Ω(n) = Ω(i) + Ω(n/i) = Ω(i) + 1.
is_chen = n => // An integer n ≥ 1 is a Chen prime if and only if:
n > 1 // n > 1,
& Ω(n) < 2 // Ω(n) = 1 < 2, i.e. n is a prime, and
& Ω(n + 2) < 3 // Ω(n + 2) < 3, i.e. n + 2 is a prime or a semiprime.
True
真假2
还是False
假假(不一致的假假值)?