Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Explain Q
Source Link
Anders Kaseorg
  • 40.7k
  • 3
  • 76
  • 149

Here’s a Python translationExplanation (Pyth initializes Q to the input integer):

lambda Q:-((2**Q if Q>=0 else .5*[Q,((Q+1)^Q)-Q][Q%2])+1)
_ negative of (
 (
 ?gQ0 if Q >= 0:
 ^2Q 2**Q
 else:
 *.5 half of
 @ Q element Q (modulo list length) in
 , the two element list [
 Q Q,
 hQ ((Q plus 1)
 x Q XOR Q)
 - Q minus Q
 ]
 h ) plus 1
 )

(n − 2,
−2^(n − 2)⋅1 − 1,
−2^(n − 3)⋅3 − 1,
−2^(n − 4)⋅5 − 1,
...,
−2^2⋅(2·n - 7) − 1,
−2^1⋅(2·n - 5) − 1,
−2^0⋅(2·n - 3) − 1).

Here’s a Python translation:

lambda Q:-((2**Q if Q>=0 else .5*[Q,((Q+1)^Q)-Q][Q%2])+1)

(n − 2,
−2^(n − 2)⋅1 − 1,
−2^(n − 3)⋅3 − 1,
−2^(n − 4)⋅5 − 1,
...,
−2^2⋅(2·n - 7) − 1,
−2^1⋅(2·n - 5) − 1,
−2^0⋅(2·n - 3) − 1).

Explanation (Pyth initializes Q to the input integer):

_ negative of (
 (
 ?gQ0 if Q >= 0:
 ^2Q 2**Q
 else:
 *.5 half of
 @ Q element Q (modulo list length) in
 , the two element list [
 Q Q,
 hQ ((Q plus 1)
 x Q XOR Q)
 - Q minus Q
 ]
 h ) plus 1
 )

(n − 2,
−2^(n − 2)⋅1 − 1,
−2^(n − 3)⋅3 − 1,
−2^(n − 4)⋅5 − 1,
...,
−2^2⋅(2·n 7) − 1,
−2^1⋅(2·n 5) − 1,
−2^0⋅(2·n 3) − 1).

−9 bytes, and add more explanation
Source Link
Anders Kaseorg
  • 40.7k
  • 3
  • 76
  • 149

Pyth, 27(削除) 27 (削除ここまで) 18 bytes

_@_h?<Q1,t^2_Q/Q2gQ0^2Q*.5@,/+xtQQQ2t^2Q-xh

Here’s a Python translation:

lambda Q:-([2**-Q-1,Q//2]if(2**Q Q<1if else[(Q>=0 else .5*[Q,((Q-1Q+1)^Q)+Q)//2,2**Q-1]Q][Q%2])[Q%2]+1)

This has cycles

(0−1)
(−10, 1−2)
(−31, 2−3, −2−4)
(−7, 42, −5, 3−7, −6)
(−153, 8−9, −11−13, 6−11, −4−8)
(−314, 16−17, −23−25, 12−21, −9−15, 5−10)
(−635, 32−33, −47−49, 24−41, −19−29, 10−19, −6−12)
(−1276, 64−65, −95−97, 48−81, −39−57, 20−37, −13−23, 7−14)
(−2557, 128−129, −191−193, 96−161, −79−113, 40−73, −45, −27, 14−16)
(8, −8−257, −385, −321, −225, −145, −89, −53, −31, −18)
(−5119, 256−513, −383−769, 192−641, −159−449, 80−289, −55−177, 28−105, −17−61, 9−35, −20)

The cycle of length n is given by

(n − 2,
−2^(n − 2)⋅1 − 1,
−2^(n − 3)⋅3 − 1,
−2^(n − 4)⋅5 − 1,
...,
−2^2⋅(2·n - 7) − 1,
−2^1⋅(2·n - 5) − 1,
−2^0⋅(2·n - 3) − 1).

Each integer k ≥ −1 appears as the first element of the (k + 2)-cycle. For each integer k < −1, we can uniquely write 1 − k = 2^i ⋅ (2⋅j + 1) for some i, j ≥ 0; then k appears as the (j + 2)th element of the (i + j + 2)−cycle.

Pyth, 27 bytes

_@?<Q1,t^2_Q/Q2,/+xtQQQ2t^2

Here’s a Python translation:

lambda Q:-([2**-Q-1,Q//2]if Q<1 else[(((Q-1)^Q)+Q)//2,2**Q-1])[Q%2]

This has cycles

(0)
(−1, 1)
(−3, 2, −2)
(−7, 4, −5, 3)
(−15, 8, −11, 6, −4)
(−31, 16, −23, 12, −9, 5)
(−63, 32, −47, 24, −19, 10, −6)
(−127, 64, −95, 48, −39, 20, −13, 7)
(−255, 128, −191, 96, −79, 40, −27, 14, −8)
(−511, 256, −383, 192, −159, 80, −55, 28, −17, 9)

Pyth, (削除) 27 (削除ここまで) 18 bytes

_h?gQ0^2Q*.5@,Q-xh

Here’s a Python translation:

lambda Q:-((2**Q if Q>=0 else .5*[Q,((Q+1)^Q)-Q][Q%2])+1)

This has cycles

(−1)
(0, −2)
(1, −3, −4)
(2, −5, −7, −6)
(3, −9, −13, −11, −8)
(4, −17, −25, −21, −15, −10)
(5, −33, −49, −41, −29, −19, −12)
(6, −65, −97, −81, −57, −37, −23, −14)
(7, −129, −193, −161, −113, −73, −45, −27, −16)
(8, −257, −385, −321, −225, −145, −89, −53, −31, −18)
(9, −513, −769, −641, −449, −289, −177, −105, −61, −35, −20)

The cycle of length n is given by

(n − 2,
−2^(n − 2)⋅1 − 1,
−2^(n − 3)⋅3 − 1,
−2^(n − 4)⋅5 − 1,
...,
−2^2⋅(2·n - 7) − 1,
−2^1⋅(2·n - 5) − 1,
−2^0⋅(2·n - 3) − 1).

Each integer k ≥ −1 appears as the first element of the (k + 2)-cycle. For each integer k < −1, we can uniquely write 1 − k = 2^i ⋅ (2⋅j + 1) for some i, j ≥ 0; then k appears as the (j + 2)th element of the (i + j + 2)−cycle.

−1 byte
Source Link
Anders Kaseorg
  • 40.7k
  • 3
  • 76
  • 149

Pyth, 2827 bytes

-1@_@?<Q1,^2_Qht^2_Q/Q2,+.&_QQ/Q2^2+xtQQQ2t^2

Here’s a Python translation:

lambda Q:1-([2**-Q-1,Q//2+1]if2]if Q<1 else[(((Q-Q&Q1)^Q)+Q)//2,2**Q]2**Q-1])[Q%2]

This has cycles

(0)
(−1, 1)
(−3, 2, −2)
(−7, 4, −5, 3)
(−15, 8, −11, 6, −4)
(−31, 16, −23, 12, −9, 5)
(−63, 32, −47, 24, −19, 10, −6)
(−127, 64, −95, 48, −39, 20, −13, 7)
(−255, 128, −191, 96, −79, 40, −27, 14, −8)
(−511, 256, −383, 192, −159, 80, −55, 28, −17, 9)

Pyth, 28 bytes

-1@?<Q1,^2_Qh/Q2,+.&_QQ/Q2^2

Here’s a Python translation:

lambda Q:1-([2**-Q,Q//2+1]if Q<1 else[(-Q&Q)+Q//2,2**Q])[Q%2]

This has cycles

(0)
(−1, 1)
(−3, 2, −2)
(−7, 4, −5, 3)
(−15, 8, −11, 6, −4)
(−31, 16, −23, 12, −9, 5)
(−63, 32, −47, 24, −19, 10, −6)
(−127, 64, −95, 48, −39, 20, −13, 7)
(−255, 128, −191, 96, −79, 40, −27, 14, −8)
(−511, 256, −383, 192, −159, 80, −55, 28, −17, 9)

Pyth, 27 bytes

_@?<Q1,t^2_Q/Q2,/+xtQQQ2t^2

Here’s a Python translation:

lambda Q:-([2**-Q-1,Q//2]if Q<1 else[(((Q-1)^Q)+Q)//2,2**Q-1])[Q%2]

This has cycles

(0)
(−1, 1)
(−3, 2, −2)
(−7, 4, −5, 3)
(−15, 8, −11, 6, −4)
(−31, 16, −23, 12, −9, 5)
(−63, 32, −47, 24, −19, 10, −6)
(−127, 64, −95, 48, −39, 20, −13, 7)
(−255, 128, −191, 96, −79, 40, −27, 14, −8)
(−511, 256, −383, 192, −159, 80, −55, 28, −17, 9)

Source Link
Anders Kaseorg
  • 40.7k
  • 3
  • 76
  • 149
Loading

AltStyle によって変換されたページ (->オリジナル) /