众所周知,奇数素数将在帕斯卡三角形中出现两次。然而,并非所有在Pascal三角形中出现两次的数字都是素数。我们称这些数字为帕斯卡素数。
Pascal素数是在Pascal三角形中出现两次的复合数。前几个帕斯卡素数是
4, 8, 9, 12, 14, 16, 18, ...
您的挑战是将正整数n作为输入并输出true或false,这取决于n是否是Pascal素数。这是密码-高尔夫,所以最短的程序获胜!
发布于 2018-01-01 05:37:09
CompositeQ@#&&Binomial~Array~{#-1,#}~FreeQ~#&
每个复合数字n在第n行上正好出现两次,以后不能出现。所以Pascal素数的条件是它们根本不出现在第一个n-1行中。
据我所知,这比检查它在前n行中出现两次和能够使用!PrimeQ
更短。
发布于 2018-01-01 06:12:19
def f(n):l=[1];exec"(n in l)>=any(n%k<1for k in range(2,n))>q;l=map(sum,zip([0]+l,l+[0]));"*n
这是一个命名函数f,它通过出口码为Primes输出0,否则为1。
def f(n):l=[1]; # Define a function f (arg. n) and a list l = [1].
exec"..."*n # Execute n times.
(n in l) # Boolean: "n is in l?" is greater than...
>=any(n%k<1for k in range(2,n)) # the boolean: "Is n composite?"?
>q; # If the boolean comparison returns a falsy
# result, then continue on without any difference.
# Otherwise, evaluate the other part of the
# inequality, thus performing "is greater than q".
# Since we have no variable "q", this terminates
# with exit code 1, throwing a "NameError".
l=map(sum,zip([0]+l,l+[0])); # Generate the next row in Pascal's triangle,
# By zipping l prepended with a 0 with l appended
# with a 0 and mapping sum over the result.
这基本上检查n是否出现在Pascal三角形的前n行中,或者它是否为素数,如果满足这两种条件,则抛出一个错误。
由于卵子,保存了1个字节。
https://codegolf.stackexchange.com/questions/152260
复制相似问题