18 from __future__
import print_function
21 VERSION =
'tnp1 --- 2010 April 12'
127 """Renvoie la valeur de C(n) := n/2 si n pair
135 assert isinstance(n, numbers.Integral), n
137 return (n*3 + 1
if n&1
143 """Renvoie la valeur de C^(k)(n)
151 assert isinstance(n, numbers.Integral), n
158 s = natural.scan1(abs(n))
166 return (1, 4, 2)[k%3]
170 n = natural.pow3(s) * ((n >> s) + 1) - 1
174 return natural.pow3(k>>1) * ((n >> (k>>1)) + 1) - 1
176 return (natural.pow3(k>>1) * ((n >> (k>>1)) + 1) - 1) * 3 + 1
189 """Renvoie la valeur de C*(n) == C^(n)(n)
203 """Renvoie la valeur de C*^(k)(n)
216 return ((0, 4, 4)[n]
if k%2
230 Renvoie la valeur de F(n) := -------------
232 avec k = nombre de fois pour lequel n est divisible par 2
233 l = nombre de fois pour lequel le numérateur est divisible par 2
240 assert isinstance(n, numbers.Integral), n
242 if not (0 <= n <= 2):
244 n >>= natural.scan1(abs(n))
247 else n >> natural.scan1(abs(n)))
254 """Renvoie la valeur de F^(k)(n)
262 assert isinstance(n, numbers.Integral), n
265 while (k > 0)
and (n != 1):
273 """Renvoie la valeur de F*(n) == F^(n)(n)
287 """Renvoie la valeur de F*^(k)(n)
298 while (k > 0)
and (n != 1):
308 """Renvoie la valeur L(n) := 2n /3 si n mod 3 = 0
309 (4n - 1)/3 si n mod 3 = 1
310 (4n + 1)/3 si n mod 3 = 2
317 assert isinstance(n, numbers.Integral), n
320 return (n << 1
if m == 0
321 else (n << 2) + (1
if m == 1
327 """Renvoie la valeur L^(-1)(n) = 3n /2 si n pair
328 (3n + 1)/4 si n mod 4 = 1
329 (3n - 1)/4 si n mod 4 = 3
336 assert isinstance(n, numbers.Integral), n
338 return ((n >> 1) * 3
if n&1 == 0
339 else ((n * 3 - 1)
if n&2
340 else (n * 3 + 1)) >> 2)
345 """Renvoie la valeur L^(k)(n)
353 assert isinstance(n, numbers.Integral), n
354 assert isinstance(k, numbers.Integral), k
369 """Renvoie la valeur de L*(n) == L^(n)(n)
376 assert isinstance(n, numbers.Integral), n
383 """Renvoie la valeur de L*^(k)(n)
391 assert isinstance(n, numbers.Integral), n
404 """Renvoie la valeur de T(n) := n/2 si n pair
405 (3n + 1)/2 si n impair
412 assert isinstance(n, numbers.Integral), n
414 return (n*3 + 1
if n&1
420 """Renvoie la valeur de T^(k)(n)
428 assert isinstance(n, numbers.Integral), n
435 s = natural.scan1(abs(n))
447 n = natural.pow3(s) * ((n >> s) + 1) - 1
451 return natural.pow3(k) * ((n >> k) + 1) - 1
460 """Renvoie la valeur de T*(n) == T^(n)(n)
474 """Renvoie la valeur de T*^(k)(n)
486 while (k > 0)
and (n != 2):
496 """Renvoie la valeur de T'(n) := L o T o L^(-1) (n) = L( T( L^(-1)(n) ) )
503 assert isinstance(n, numbers.Integral), n
510 """Renvoie la valeur de T'^(k)(n)
518 assert isinstance(n, numbers.Integral), n
526 """Renvoie la valeur de T'*(n) == T'^(n)(n)
540 """Renvoie la valeur de T'*^(k)(n)
561 """Renvoie la valeur de T"(n) := L^(-1) o T o L (n) = L^(-1)( T( L(n) ) )
568 assert isinstance(n, numbers.Integral), n
575 """Renvoie la valeur de T"^(k)(n)
583 assert isinstance(n, numbers.Integral), n
591 """Renvoie la valeur de T"*(n) == T"^(n)(n)
605 """Renvoie la valeur de T"*^(k)(n)
626 """Renvoie la valeur de U(n) := -1 si n == -1
629 avec k naturel tel que
630 n, T(n), T^(2)(n), T^(3)(n)... et T^(k-1)(n) soient de même parité
631 et T^(k)(n) de parité contraire
638 assert isinstance(n, numbers.Integral), n
641 return (n >> natural.scan1(abs(n))
if n != 0
646 return natural.pow3(s) * ((n >> s) + 1) - 1
656 """Renvoie la valeur de U^(k)(n)
664 assert isinstance(n, numbers.Integral), n
667 while (k > 0)
and not(0 <= n <= 2):
676 """Renvoie la valeur de U*(n) == U^(n)(n)
690 """Renvoie la valeur de U*^(k)(n)
714 if __name__ ==
'__main__':
720 if not 'profile' in dir():
728 debug.test_begin(VERSION, __debug__)
731 print(
'C()...', end=
''); sys.stdout.flush()
732 assert C(-5) == -14,
C(-5)
733 assert C(-4) == -2,
C(-4)
734 assert C(-3) == -8,
C(-3)
735 assert C(-2) == -1,
C(-2)
736 assert C(-1) == -2,
C(-1)
737 assert C( 0) == 0,
C( 0)
738 assert C( 1) == 4,
C( 1)
739 assert C( 2) == 1,
C( 2)
740 assert C( 3) == 10,
C( 3)
741 assert C( 4) == 2,
C( 4)
742 assert C( 5) == 16,
C( 5)
743 print(
'ok'); sys.stdout.flush()
746 print(
'Ck()...', end=
''); sys.stdout.flush()
747 assert Ck(-5, 1) == -14,
Ck(-5, 1)
748 assert Ck(-4, 1) == -2,
Ck(-4, 1)
749 assert Ck(-3, 1) == -8,
Ck(-3, 1)
750 assert Ck(-2, 1) == -1,
Ck(-2, 1)
751 assert Ck(-1, 1) == -2,
Ck(-1, 1)
752 assert Ck( 0, 1) == 0,
Ck( 0, 1)
753 assert Ck( 1, 1) == 4,
Ck( 1, 1)
754 assert Ck( 2, 1) == 1,
Ck( 2, 1)
755 assert Ck( 3, 1) == 10,
Ck( 3, 1)
756 assert Ck( 4, 1) == 2,
Ck( 4, 1)
757 assert Ck( 5, 1) == 16,
Ck( 5, 1)
758 for n
in range(-100, 100):
759 assert Ck(n, 0) == n, (n,
Ck(n))
760 assert Ck(n, 1) ==
C(n), (n,
Ck(n, 1),
C(n))
761 assert Ck(n, 2) ==
C(
C(n)), (n,
Ck(n, 2),
C(
C(n)))
762 for k
in range(50
if debug.assertspeed >= debug.ASSERT_NORMAL
else 20):
763 assert Ck(n, k+1) ==
C(
Ck(n, k)) ==
Ck(
C(n), k), \
764 (n, k,
Ck(n, k+1),
C(
Ck(n, k)),
Ck(
C(n), k))
765 if debug.assertspeed < debug.ASSERT_NORMAL:
766 print(debug.assertspeed_str(), end=
'')
767 print(
'ok'); sys.stdout.flush()
770 print(
'CS()...', end=
''); sys.stdout.flush()
772 assert CS(n) ==
Ck(n, n), (n,
CS(n),
Ck(n, n))
773 print(
'ok'); sys.stdout.flush()
776 print(
'CSk()...', end=
''); sys.stdout.flush()
778 assert CSk(n, 0) == n, (n,
CSk(n, 0))
779 assert CSk(n, 1) ==
CS(n), (n,
CSk(n, 1),
CS(n))
783 print(
'ok'); sys.stdout.flush()
787 print(
'F()...', end=
''); sys.stdout.flush()
788 assert F(-5) == -7,
F(-5)
789 assert F(-4) == -1,
F(-4)
790 assert F(-3) == -1,
F(-3)
791 assert F(-2) == -1,
F(-2)
792 assert F(-1) == -1,
F(-1)
793 assert F( 0) == 1,
F( 0)
794 assert F( 1) == 1,
F( 1)
795 assert F( 2) == 1,
F( 2)
796 assert F( 3) == 5,
F( 3)
797 assert F( 4) == 1,
F( 4)
798 assert F( 5) == 1,
F( 5)
799 print(
'ok'); sys.stdout.flush()
802 print(
'Fk()...', end=
''); sys.stdout.flush()
803 assert Fk(-5, 1) == -7,
Fk(-5, 1)
804 assert Fk(-4, 1) == -1,
Fk(-4, 1)
805 assert Fk(-3, 1) == -1,
Fk(-3, 1)
806 assert Fk(-2, 1) == -1,
Fk(-2, 1)
807 assert Fk(-1, 1) == -1,
Fk(-1, 1)
808 assert Fk( 0, 1) == 1,
Fk( 0, 1)
809 assert Fk( 1, 1) == 1,
Fk( 1, 1)
810 assert Fk( 2, 1) == 1,
Fk( 2, 1)
811 assert Fk( 3, 1) == 5,
Fk( 3, 1)
812 assert Fk( 4, 1) == 1,
Fk( 4, 1)
813 assert Fk( 5, 1) == 1,
Fk( 5, 1)
814 for n
in range(-50, 50):
815 assert Fk(n, 0) == n, (n,
F(n))
816 assert Fk(n, 1) ==
F(n), (n,
Fk(n, 1),
F(n))
817 assert Fk(n, 2) ==
F(
F(n)), (n,
Fk(n, 2),
F(
F(n)))
819 assert Fk(n, k+1) ==
F(
Fk(n, k)) ==
Fk(
F(n), k), \
820 (n, k,
Fk(n, k+1),
F(
Fk(n, k)),
Fk(
F(n), k))
821 print(
'ok'); sys.stdout.flush()
824 print(
'FS()...', end=
''); sys.stdout.flush()
826 assert FS(n) ==
Fk(n, n), (n,
FS(n),
Fk(n, n))
827 print(
'ok'); sys.stdout.flush()
830 print(
'FSk()...', end=
''); sys.stdout.flush()
832 assert FSk(n, 0) == n, (n,
FSk(n, 0))
833 assert FSk(n, 1) ==
FS(n), (n,
FSk(n, 1),
FS(n))
837 print(
'ok'); sys.stdout.flush()
841 print(
'L()...', end=
''); sys.stdout.flush()
842 assert L(-5) == -7,
L(-5)
843 assert L(-4) == -5,
L(-4)
844 assert L(-3) == -2,
L(-3)
845 assert L(-2) == -3,
L(-2)
846 assert L(-1) == -1,
L(-1)
847 assert L( 0) == 0,
L( 0)
848 assert L( 1) == 1,
L( 1)
849 assert L( 2) == 3,
L( 2)
850 assert L( 3) == 2,
L( 3)
851 assert L( 4) == 5,
L( 4)
852 assert L( 5) == 7,
L( 5)
853 print(
'ok'); sys.stdout.flush()
856 print(
'L_rec()...', end=
''); sys.stdout.flush()
868 print(
'ok'); sys.stdout.flush()
871 print(
'Lk()...', end=
''); sys.stdout.flush()
872 assert Lk(-5, 1) == -7,
Lk(-5, 1)
873 assert Lk(-4, 1) == -5,
Lk(-4, 1)
874 assert Lk(-3, 1) == -2,
Lk(-3, 1)
875 assert Lk(-2, 1) == -3,
Lk(-2, 1)
876 assert Lk(-1, 1) == -1,
Lk(-1, 1)
877 assert Lk( 0, 1) == 0,
Lk( 0, 1)
878 assert Lk( 1, 1) == 1,
Lk( 1, 1)
879 assert Lk( 2, 1) == 3,
Lk( 2, 1)
880 assert Lk( 3, 1) == 2,
Lk( 3, 1)
881 assert Lk( 4, 1) == 5,
Lk( 4, 1)
882 assert Lk( 5, 1) == 7,
Lk( 5, 1)
883 for n
in range(-50, 50):
884 assert Lk(n, 0) == n, (n,
L(n))
885 assert Lk(n, 1) ==
L(n), (n,
Lk(n, 1),
L(n))
886 assert Lk(n, 2) ==
L(
L(n)), (n,
Lk(n, 2),
L(
L(n)))
890 assert Lk(n, k+1) ==
L(
Lk(n, k)) ==
Lk(
L(n), k), \
891 (n, k,
Lk(n, k+1),
L(
Lk(n, k)),
Lk(
L(n), k))
894 print(
'ok'); sys.stdout.flush()
897 print(
'LS()...', end=
''); sys.stdout.flush()
898 for n
in range(-100, 100):
899 assert LS(n) ==
Lk(n, n), (n,
LS(n),
Lk(n, n))
900 print(
'ok'); sys.stdout.flush()
903 print(
'LSk()...', end=
''); sys.stdout.flush()
904 for n
in range(30
if debug.assertspeed >= debug.ASSERT_NORMAL
else 10):
905 assert LSk(n, 0) == n, (n,
LSk(n, 0))
906 assert LSk(n, 1) ==
LS(n), (n,
LSk(n, 1),
LS(n))
911 and ((k == 3)
or ((n >= 17)
and (k == 2)))):
914 if debug.assertspeed < debug.ASSERT_NORMAL:
915 print(debug.assertspeed_str(), end=
'')
916 print(
'ok'); sys.stdout.flush()
920 print(
'T()...', end=
''); sys.stdout.flush()
921 assert T(-5) == -7,
T(-5)
922 assert T(-4) == -2,
T(-4)
923 assert T(-3) == -4,
T(-3)
924 assert T(-2) == -1,
T(-2)
925 assert T(-1) == -1,
T(-1)
926 assert T( 0) == 0,
T( 0)
927 assert T( 1) == 2,
T( 1)
928 assert T( 2) == 1,
T( 2)
929 assert T( 3) == 5,
T( 3)
930 assert T( 4) == 2,
T( 4)
931 assert T( 5) == 8,
T( 5)
932 for n
in range(-100, 100):
934 assert T(n) ==
C(n), (n,
T(n),
C(n))
936 assert T(n) ==
Ck(n, 2), (n,
T(n),
Ck(n, 2))
937 print(
'ok'); sys.stdout.flush()
940 print(
'Tk()...', end=
''); sys.stdout.flush()
941 assert Tk(-5, 1) == -7,
Tk(-5, 1)
942 assert Tk(-4, 1) == -2,
Tk(-4, 1)
943 assert Tk(-3, 1) == -4,
Tk(-3, 1)
944 assert Tk(-2, 1) == -1,
Tk(-2, 1)
945 assert Tk(-1, 1) == -1,
Tk(-1, 1)
946 assert Tk( 0, 1) == 0,
Tk( 0, 1)
947 assert Tk( 1, 1) == 2,
Tk( 1, 1)
948 assert Tk( 2, 1) == 1,
Tk( 2, 1)
949 assert Tk( 3, 1) == 5,
Tk( 3, 1)
950 assert Tk( 4, 1) == 2,
Tk( 4, 1)
951 assert Tk( 5, 1) == 8,
Tk( 5, 1)
952 for n
in range(-100, 100):
953 assert Tk(n, 0) == n, (n,
Tk(n, 0))
954 assert Tk(n, 1) ==
T(n), (n,
Tk(n, 1),
T(n))
955 assert Tk(n, 2) ==
T(
T(n)), (n,
Tk(n, 2),
T(
T(n)))
956 for k
in range(50
if debug.assertspeed >= debug.ASSERT_NORMAL
else 20):
957 assert Tk(n, k+1) ==
T(
Tk(n, k)) ==
Tk(
T(n), k), \
958 (n, k,
Tk(n, k+1),
T(
Tk(n, k)),
Tk(
T(n), k))
959 if debug.assertspeed < debug.ASSERT_NORMAL:
960 print(debug.assertspeed_str(), end=
'')
961 print(
'ok'); sys.stdout.flush()
964 print(
'TS()...', end=
''); sys.stdout.flush()
966 assert TS(n) ==
Tk(n, n), (n,
TS(n),
Tk(n, n))
967 print(
'ok'); sys.stdout.flush()
970 print(
'TSk()...', end=
''); sys.stdout.flush()
972 assert TSk(n, 0) == n, (n,
TSk(n, 0))
973 assert TSk(n, 1) ==
TS(n), (n,
TSk(n, 1),
TS(n))
977 print(
'ok'); sys.stdout.flush()
981 print(
'TP()...', end=
''); sys.stdout.flush()
982 assert TP(-5) == -3,
TP(-5)
983 assert TP(-4) == -2,
TP(-4)
984 assert TP(-3) == -1,
TP(-3)
985 assert TP(-2) == -5,
TP(-2)
986 assert TP(-1) == -1,
TP(-1)
987 assert TP( 0) == 0,
TP( 0)
988 assert TP( 1) == 3,
TP( 1)
989 assert TP( 2) == 7,
TP( 2)
990 assert TP( 3) == 1,
TP( 3)
991 assert TP( 4) == 2,
TP( 4)
992 assert TP( 5) == 3,
TP( 5)
993 for n
in range(-100, 100):
995 print(
'ok'); sys.stdout.flush()
998 print(
'TPk()...', end=
''); sys.stdout.flush()
999 assert TPk(-5, 1) == -3,
TPk(-5, 1)
1000 assert TPk(-4, 1) == -2,
TPk(-4, 1)
1001 assert TPk(-3, 1) == -1,
TPk(-3, 1)
1002 assert TPk(-2, 1) == -5,
TPk(-2, 1)
1003 assert TPk(-1, 1) == -1,
TPk(-1, 1)
1004 assert TPk( 0, 1) == 0,
TPk( 0, 1)
1005 assert TPk( 1, 1) == 3,
TPk( 1, 1)
1006 assert TPk( 2, 1) == 7,
TPk( 2, 1)
1007 assert TPk( 3, 1) == 1,
TPk( 3, 1)
1008 assert TPk( 4, 1) == 2,
TPk( 4, 1)
1009 assert TPk( 5, 1) == 3,
TPk( 5, 1)
1010 for n
in range(-100, 100):
1011 assert TPk(n, 0) == n, (n,
TPk(n, 0))
1012 assert TPk(n, 1) ==
TP(n), (n,
TPk(n, 1),
TP(n))
1014 for k
in range(50
if debug.assertspeed >= debug.ASSERT_NORMAL
else 25):
1017 if debug.assertspeed < debug.ASSERT_NORMAL:
1018 print(debug.assertspeed_str(), end=
'')
1019 print(
'ok'); sys.stdout.flush()
1022 print(
'TPS()...', end=
''); sys.stdout.flush()
1023 for n
in range(100):
1025 print(
'ok'); sys.stdout.flush()
1028 print(
'TPSk()...', end=
''); sys.stdout.flush()
1029 for n
in range(50
if debug.assertspeed >= debug.ASSERT_NORMAL
else 25):
1030 assert TPSk(n, 0) == n, (n,
TPSk(n, 0))
1035 if debug.assertspeed < debug.ASSERT_NORMAL:
1036 print(debug.assertspeed_str(), end=
'')
1037 print(
'ok'); sys.stdout.flush()
1041 print(
'TP2()...', end=
''); sys.stdout.flush()
1042 assert TP2(-5) == -15,
TP2(-5)
1043 assert TP2(-4) == -5,
TP2(-4)
1044 assert TP2(-3) == -1,
TP2(-3)
1045 assert TP2(-2) == -6,
TP2(-2)
1046 assert TP2(-1) == -1,
TP2(-1)
1047 assert TP2( 0) == 0,
TP2( 0)
1048 assert TP2( 1) == 3,
TP2( 1)
1049 assert TP2( 2) == 4,
TP2( 2)
1050 assert TP2( 3) == 1,
TP2( 3)
1051 assert TP2( 4) == 12,
TP2( 4)
1052 assert TP2( 5) == 8,
TP2( 5)
1053 for n
in range(-100, 100):
1055 print(
'ok'); sys.stdout.flush()
1058 print(
'TP2k()...', end=
''); sys.stdout.flush()
1059 assert TP2k(-5, 1) == -15,
TP2k(-5, 1)
1060 assert TP2k(-4, 1) == -5,
TP2k(-4, 1)
1061 assert TP2k(-3, 1) == -1,
TP2k(-3, 1)
1062 assert TP2k(-2, 1) == -6,
TP2k(-2, 1)
1063 assert TP2k(-1, 1) == -1,
TP2k(-1, 1)
1064 assert TP2k( 0, 1) == 0,
TP2k( 0, 1)
1065 assert TP2k( 1, 1) == 3,
TP2k( 1, 1)
1066 assert TP2k( 2, 1) == 4,
TP2k( 2, 1)
1067 assert TP2k( 3, 1) == 1,
TP2k( 3, 1)
1068 assert TP2k( 4, 1) == 12,
TP2k( 4, 1)
1069 assert TP2k( 5, 1) == 8,
TP2k( 5, 1)
1070 for n
in range(-100, 100):
1071 assert TP2k(n, 0) == n, (n,
TP2k(n, 0))
1077 print(
'ok'); sys.stdout.flush()
1080 print(
'TP2S()...', end=
''); sys.stdout.flush()
1081 for n
in range(100):
1083 print(
'ok'); sys.stdout.flush()
1086 print(
'TP2Sk()...', end=
''); sys.stdout.flush()
1087 for n
in range(50
if debug.assertspeed >= debug.ASSERT_NORMAL
else 25):
1093 if debug.assertspeed < debug.ASSERT_NORMAL:
1094 print(debug.assertspeed_str(), end=
'')
1095 print(
'ok'); sys.stdout.flush()
1098 print(
'U()...', end=
''); sys.stdout.flush()
1099 assert U(-5) == -10,
U(-5)
1100 assert U(-4) == -1,
U(-4)
1101 assert U(-3) == -4,
U(-3)
1102 assert U(-2) == -1,
U(-2)
1103 assert U(-1) == -1,
U(-1)
1104 assert U( 0) == 0,
U( 0)
1105 assert U( 1) == 2,
U( 1)
1106 assert U( 2) == 1,
U( 2)
1107 assert U( 3) == 8,
U( 3)
1108 assert U( 4) == 1,
U( 4)
1109 assert U( 5) == 8,
U( 5)
1110 for n
in range(-100, -1):
1112 while t & 1 == n & 1:
1114 assert U(n) == t, (n,
U(n), t)
1115 for n
in range(1, 100):
1117 while t & 1 == n & 1:
1119 assert U(n) == t, (n,
U(n), t)
1120 print(
'ok'); sys.stdout.flush()
1123 print(
'Uk()...', end=
''); sys.stdout.flush()
1124 assert Uk(-5, 1) == -10,
Uk(-5, 1)
1125 assert Uk(-4, 1) == -1,
Uk(-4, 1)
1126 assert Uk(-3, 1) == -4,
Uk(-3, 1)
1127 assert Uk(-2, 1) == -1,
Uk(-2, 1)
1128 assert Uk(-1, 1) == -1,
Uk(-1, 1)
1129 assert Uk( 0, 1) == 0,
Uk( 0, 1)
1130 assert Uk( 1, 1) == 2,
Uk( 1, 1)
1131 assert Uk( 2, 1) == 1,
Uk( 2, 1)
1132 assert Uk( 3, 1) == 8,
Uk( 3, 1)
1133 assert Uk( 4, 1) == 1,
Uk( 4, 1)
1134 assert Uk( 5, 1) == 8,
Uk( 5, 1)
1135 for n
in range(-50, 50):
1136 assert Uk(n, 0) == n, (n,
U(n))
1137 assert Uk(n, 1) ==
U(n), (n,
Uk(n, 1),
U(n))
1138 assert Uk(n, 2) ==
U(
U(n)), (n,
Uk(n, 2),
U(
U(n)))
1140 assert Uk(n, k+1) ==
U(
Uk(n, k)) ==
Uk(
U(n), k), \
1141 (n, k,
Uk(n, k+1),
U(
Uk(n, k)),
Uk(
U(n), k))
1142 print(
'ok'); sys.stdout.flush()
1145 print(
'US()...', end=
''); sys.stdout.flush()
1146 for n
in range(100):
1147 assert US(n) ==
Uk(n, n), (n,
US(n),
Uk(n, n))
1148 print(
'ok'); sys.stdout.flush()
1151 print(
'USk()...', end=
''); sys.stdout.flush()
1153 assert USk(n, 0) == n, (n,
USk(n, 0))
1154 assert USk(n, 1) ==
US(n), (n,
USk(n, 1),
US(n))
1158 print(
'ok'); sys.stdout.flush()