我们将孪生素数定义为两个自然数p,p+2
,它们都是素数。
5
和7
是孪生素数。
让我们将某些数字集的孪生数定义为该集合中的孪生素数数。
示例:{6
、7
、11
、13
、18
、29
、31
}有一个孪生数字4
,因为有四个孪生素数:11,13
和29,31
。
输入:一个数字n
输出:n
以下所有自然数集合的孪生数
这是密码-高尔夫,所以最低字节获胜。
发布于 2016-06-14 00:44:13
qZqd2=s
这个解决方案利用了这样一个事实:素数间隙 (成功素数之间的区别)总是>= 2(除了2和3之间的差距)。由于这个事实,所有的孪生素数都是连续素数。在这个解决方案中,我们只计算所有小于输入n
的素数之间的差异,并计算这些差异中有多少等于2。
在网上试试
% Implicitly grab input, N
q % Subtract one from the input (N-1)
Zq % Get an array of all primes <= (N-1)
d % Compute successive differences (prime gaps)
2= % Create a boolean array indicating which prime gaps are equal to 2
s % Count the TRUE values in this array to determine the twin number
% Implicitly display the result
如果您想要的不是对的数量,而是作为一对的一部分的素数,那么下面的内容就可以了。
qZqt!-|H=az
在网上试试
发布于 2016-06-14 01:04:19
ZlZd~Bx©{2
在这里试试!
ZlZd~Bx©{2
~B all numbers satisfying
©{ the prime condition ("the prime directive"?)
x below x
Zd (the differences between indices)
Zl 2 count the number of 2's
发布于 2016-06-14 05:18:08
-rprime
标志)= 51字节->n{Prime.each(n-1).count{|i|(i-2).prime?}}
https://codegolf.stackexchange.com/questions/82852
复制相似问题