Answers:
±(t=x_?PrimeQ)=NextPrime@x
±n_:=Abs[n-1]/.t:>x-1
定义以下双射:
n 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ...
±n 1, 0, 3, 5, 2, 7, 4, 11, 6, 8, 9, 13, 10, 17, 12, 14, 15, 19, ...
基本思想是将每个素数映射到下一个素数,以确保将它们映射到适当的子集。这导致2处出现“缺口” 。为了填补这个空白,我们想将4映射到2,然后将每个复合数字映射到先前的复合数字,以“填补”空白。由于2和3是仅有的两个相邻素数,我们可以将这两个映射都表示为“ n-1,或者如果那是素数则为n-2 ”。最后,这种映射方式结束了发送1至0,我们让它发送0回1通过取绝对值,N-1 。
0
吗?
感谢Emigna发现错误,现已更正
tZp?_Yq}q:Zp~fX>sG~E+
这实现了以下双射。连续写素数和下面的非素数:
2 3 5 7 11 13 17 ...
0 1 4 6 8 9 10 ...
然后通过遵循输入中的箭头获得输出:
2 > 3 > 5 > 7 > 11 > 13 > 17 ...
^
0 < 1 < 4 < 6 < 8 < 9 < 10 ...
t % Implicit input. Duplicate
Zp % Is it a prime? Gives true / false
? % If so
_Yq % Next prime
} % Else
q % Subtract 1
: % Range from 1 to that
Zp~ % Is each entry not a prime? Gives an array of true / false
f % Find non-zero entries, i.e. non-primes. Will be empty for input 1
X> % Maximum. This gives the greatest non-prime less than the input.
% Will be empty for input 1
s % Sum. This is to transform empty into 0
G~E % Push input, negate, times 2. This gives 2 for input 0, or 0 otherwise
E % Add. This handles the case of input 0, so that it outputs 2
% End (implicit). Display (implicit)
实现与路易斯·门多(Luis Mendo)的答案相同的逻辑。
f=(n,i=(P=(n,x=n)=>n%--x?P(n,x):x==1||-1)(x=n))=>x?x==n|P(n)-i?f(n+i,i):n:2
f = ( // given:
n, // - n = input
i = // - i = 'direction' to move towards
(P = (n, x = n) => // - P = function that returns:
n % --x ? // - 1 when given a prime
P(n, x) // - -1 when given a composite number
: //
x == 1 || -1 //
)(x = n) // - x = copy of the original input
) => //
x ? // if the input is not zero:
x == n | P(n) - i ? // if n equals the input or doesn't match its primality:
f(n + i, i) // do a recursive call in the chosen direction
: // else:
n // return n
: // else:
2 // return 2
Æn_ḍ@¡ÆP?2»0
Æn_ḍ@¡ÆP?2»0 Main link. Argument: n (non-negative integer)
ÆP? If the input is prime:
Æn Compute the next prime after n.
Else:
ḍ@¡ 2 Do once if n is divisible by 2, zero times if not.
_ 2 Subtract 2.
So far, we've mapped all primes to the next prime, all even integers
(except 2) to the previous even integer, and all composite, odd,
positive integers to themselves. In particular, 0 -> 2, but 0 doesn't
have a preimage, so we need 0 -> 0.
»0 Take the maximum of the result and 0, mapping 0 -> max(-2, 0) = 0.