12 from __future__
import division
13 from __future__
import print_function
16 VERSION =
'natural --- 2010 April 12'
18 import fractions, math, types
31 """Renvoie un string représentant n en binaire
32 (Si n == 0 alors renvoie '0')
41 if n <= bit32.MERSENNE32:
44 l = [bit32.bin(n & bit32.MERSENNE32)]
45 while n > bit32.MERSENNE32:
47 l.insert(0,
'0' * (32 - len(l[0])))
49 l.insert(0, bit32.bin(n & bit32.MERSENNE32))
55 """Renvoie le coefficient binomial (n)
77 """Renvoie True si le bit d'indice i de n vaut 1,
89 return bool(n & (1 << i))
94 """Si b alors renvoie n avec son bit d'indice i à 1,
95 sinon renvoie n avec son bit d'indice i à 0
107 return (n | (1 << i)
if b
113 """Renvoie n avec son bit d'indice i à 0
129 """Renvoie n avec son bit d'indice i à 1
145 """Renvoie True si m et n sont premiers entre eux,
147 (Si m == n == 0 alors renvoie False)
156 return (
not ((m&1 == 0)
and (n&1 == 0)))
and (fractions.gcd(m, n) == 1)
166 """Renvoie la distance de Dominici entre a et b
181 return factors.distance_dominici(factors.primaries(a), factors.primaries(b))
186 """Renvoie la liste des diviseurs de n
187 (dans l'ordre strictement croissant)
188 (Si n == 1 alors renvoie [1])
192 Result: séquence strictement ordonnée non vide de naturels >= 1
199 for d
in range(1, n):
208 """Renvoie la liste des diviseurs de n vérifiant la condition cond
209 (dans l'ordre strictement croissant)
211 cond: fonction à une variable naturelle et renvoyant un bool
212 Result: séquence strictement ordonnée non vide de naturels >= 1
216 assert isinstance(cond, types.FunctionType), type(cond)
219 for d
in range(1, n + 1):
220 if (n%d == 0)
and cond(d):
227 """Renvoie n!, la factorielle de n
228 == 1 * 2 * 3 * ... * n
229 (Si n == 0 alors renvoie 1)
238 return math.factorial(n)
243 """Renvoie la kème puissance factorielle descendante de n
244 == n * (n - 1) * (n - 2) * ... * (n - k + 1)
257 return nat32.FACTORIAL_TUPLE[n] // nat32.FACTORIAL_TUPLE[n - k]
272 elif (n - 1 == k)
or (n == k):
273 return math.factorial(n)
296 """Renvoie F_k, le kième nombre de Fibonacci
310 """Renvoie (F_(k-1), F_k), les (k-1)ième et kième nombres de Fibonacci
314 Result: couple de naturels
325 return (Fq*Fq + Fq_1S, Fq1S - Fq_1S)
327 return (Fq1S - Fq_1*Fq_1, Fq1S + Fq*Fq)
329 return nat32.fibonacci2(k)
334 """Renvoie True si n est un nombre de Fibonacci,
344 if n <= bit32.MERSENNE32:
345 return nat32.fibonacci_is(n)
349 Fk_1, Fk = Fk, Fk+Fk_1
355 """Si n == F_k alors renvoie k
356 (si n == 1 alors renvoie toujours 1, jamais 2),
359 Pre: n naturel ou None
364 if n <= bit32.MERSENNE32:
365 return nat32.fibonacci_to_index(n)
370 Fk_1, Fk = Fk, Fk+Fk_1
378 """Renvoie le PGCD de m et n
379 (Si m == n == 0 alors renvoie 0)
380 (Plus Grand Commun Diviseur/ Greatest Common Divisor)
391 return fractions.gcd(m, n)
396 """Renvoie le PPCM de m et n
397 (Si m == 0 ou n == 0 alors renvoie 0)
398 (Plus Petit Commun Multiple/ Least Common Multiple)
409 return (m*n//fractions.gcd(m, n)
if (m != 0)
and (n != 0)
416 """Renvoie lg(n) == log_2(n) == nbbits(n) - 1
417 == l'indice du dernier bit à 1 de n
443 """Renvoie L_k, le kième nombre de Lucas
453 return (Fk_1 << 1) + Fk
458 """Renvoie (L_(k-1), L_k), les (k-1)ième et kième nombres de Lucas
462 Result: (-1, 2) si k == 0, sinon couple de naturels
470 return (Fk + Fk_2, (Fk_1 << 1) + Fk)
477 """Renvoie True si n est un nombre de Lucas,
490 Lk_1, Lk = Lk, Lk + Lk_1
498 """Si n == L_k alors renvoie k
503 Result: naturel ou None
512 Lk_1, Lk = Lk, Lk + Lk_1
522 """Renvoie le kième nombre de Mersenne Mk == 2**k - 1
531 return (bit32.MERSENNE_TUPLE[k]
if k <= 32
532 else (2 << (k - 1)) - 1)
538 """Si n == M_k alors renvoie k
541 Pre: n: naturel ou None
553 """Renvoie la fonction de Mertens en n
554 (Si n == 0 alors renvoie 0)
566 for i
in range(1, n + 1):
567 r += factors.mobius(factors.primaries(i))
573 """Renvoie le nombre de bits à 0 de n
574 (sans tenir compte des 0 après le dernier 1)
575 (Si n == 0 alors renvoie 0)
586 n32 = n & bit32.MERSENNE32
589 nb += bit32.nbbits0(n32)
591 nb += 31 - bit32.rscan1(n32)
599 """Renvoie le nombre de bits à 1 de n
600 (Si n == 0 alors renvoie 0)
611 nb += bit32.nbbits1(n & bit32.MERSENNE32)
618 """Renvoie le nombre de bits de n
619 == nbbits0(n) + nbbits1(n)
620 (sans tenir compte des 0 après le dernier 1)
621 (Si n == 0 alors renvoie 0)
631 while n > bit32.MERSENNE32:
634 return (k<<5) + bit32.nbbits(n)
639 """Renvoie le produit des facteurs non communs de m et n
640 (Si m == 0 ou n == 0 alors renvoie 0)
641 (Not common Factors Product)
652 return (m*n//(fractions.gcd(m, n)**2)
if (m != 0)
and (n != 0)
658 """Renvoie la liste des nontotatives de n
659 (dans l'ordre strictement croissant)
660 càd les naturels non nuls <= n et non premiers avec n
661 (Si n == 0 ou 1 alors renvoie [])
665 Result: séquence strictement ordonnée de naturels
672 for d
in range(2, n):
683 """Renvoie le nombre de nontotatives de n
684 (Si n == 0 ou 1 alors renvoie 0)
695 return (n - factors.totient(factors.primaries(n))
if n > 1
701 """Nombre polygonal à k côtés de n
713 return ((n*(n - 1)) >> 1) * (k - 2) + n
718 """Renvoie la kième puissance de 2 : 2**k
727 return (bit32.POW2_TUPLE[k]
if k < 32
733 """Renvoie la kième puissance de 3 : 3**k
743 return (nat32.POW3_TUPLE[20]**k) * nat32.POW3_TUPLE[rem]
748 """Renvoie True si n est premier,
759 return nat32.prime_is(n)
760 elif n <= 2**32 + 14:
763 raise NotImplementedError
768 """Nombre pyramidal à k côtés de n
781 return (s*n*(k - 2) + s*3 - n*(k - 5)) // 6
786 """Renvoie la kème puissance factorielle montante de n
787 == n * (n + 1) * (n + 2) * ... * (n + k - 1)
798 if (n > 0)
and (k > 0):
800 return nat32.FACTORIAL_TUPLE[n + k - 1] // nat32.FACTORIAL_TUPLE[n - 1]
823 """Renvoie l'indice du dernier bit à 1 de n
828 Result: naturel ou None
833 return (
nbbits(n) - 1
if n > 0
839 """Renvoie l'indice du premier bit à 0 de n
840 (si n == 0 alors renvoie 0)
850 while n & bit32.MERSENNE32 == bit32.MERSENNE32:
853 return (k << 5) + bit32.scan0(n & bit32.MERSENNE32)
858 """Renvoie l'indice du premier bit à 1 de n
863 Result: naturel ou None
870 while n&bit32.MERSENNE32 == 0:
873 return (k << 5) + bit32.scan1(n & bit32.MERSENNE32)
880 """Renvoie la liste des totatives de n
881 (dans l'ordre strictement croissant)
882 càd les naturels non nuls <= n et premiers avec n
883 (Si n == 0 alors renvoie []
884 Si n == 1 alors renvoie [1])
888 Result: séquence strictement ordonnée de naturels
895 for d
in range(2, n):
905 """Si n > 0 alors renvoie le nombre de totatives de n
907 (Si n == 0 ou 1 alors renvoie 1)
918 return (factors.totient(factors.primaries(n))
if n > 1
924 """Renvoie la liste des diviseurs unitaires de n
925 (dans l'ordre strictement croissant)
926 (Si n == 1 alors renvoie [1])
930 Result: séquence strictement ordonnée non vide de naturels >= 1
937 for d
in range(2, n + 1):
948 if __name__ ==
'__main__':
954 if not 'profile' in dir():
964 debug.test_begin(VERSION, __debug__)
966 print(
'bin()...', end=
''); sys.stdout.flush()
967 assert bin(0) ==
'0',
bin(0)
968 assert bin(1) ==
'1',
bin(1)
969 for i
in range(1, 50):
970 assert bin(2**i - 1) ==
'1'*i, (i,
bin(2**i - 1))
971 assert bin(2**i) ==
'1' +
'0'*i, (i,
bin(2**i))
972 assert bin(2**i + 1) ==
'1' +
'0'*(i - 1) +
'1', (i,
bin(2**i + 1))
973 for n
in range(1, 16384):
974 assert int(
bin(n), 2) == n, (n,
bin(n))
976 if debug.assertspeed >= debug.ASSERT_NORMAL:
977 for n
in range(1, 2**40, 1000000000):
978 assert int(
bin(n), 2) == n, (n,
bin(n))
980 for n
in range(2**32 - 16384, 2**32 + 16384):
981 assert int(
bin(n), 2) == n, (n,
bin(n))
984 print(debug.assertspeed_str(), end=
'')
985 print(
'ok'); sys.stdout.flush()
988 print(
'binom()...', end=
''); sys.stdout.flush()
992 for k
in range(1, 10):
993 assert binom(n, n+k) == 0, (n, k,
binom(n, n+k))
995 for k
in range(n + 1):
997 assert s == 2**n, (n, s, 2**n)
998 for n
in range(1, 50):
999 for k
in range(1, n + 1):
1001 assert binom(n, k) ==
binom(n - 1, k - 1) * n // k, \
1003 assert binom(n, k) ==
binom(n, k - 1) * (n - k + 1) // k, \
1004 (n, k,
binom(n, k),
binom(n, k - 1) * (n - k + 1))
1005 for n
in range(3, 100):
1006 assert binom(n, 2) == ((n - 1)*n)//2, (n,
binom(n, 2), ((n - 1)*n)//2)
1007 print(
'ok'); sys.stdout.flush()
1010 print(
'bit()...', end=
''); sys.stdout.flush()
1012 assert bit(2**i, i) ==
True, (i, 2**i,
bit(2**i, i))
1014 assert bit(0, i) ==
False, (i,
bit(0, i))
1017 assert bit(2**i, j) ==
False, (i, j, 2**i,
bit(2**i, j))
1018 print(
'ok'); sys.stdout.flush()
1021 print(
'bitset()...', end=
''); sys.stdout.flush()
1022 for n
in range(2048):
1025 assert bitset(n, i,
False) == n - (1<<i), (n, n - (1<<i),
bitset(n, i,
False))
1026 assert bitset(n, i,
True) == n, (n,
bitset(n, i,
True))
1028 assert bitset(n, i,
False) == n, (n,
bitset(n, i,
False))
1029 assert bitset(n, i,
True) == n + (1<<i), (n, n + (1<<i),
bitset(n, i,
True))
1030 if debug.assertspeed >= debug.ASSERT_NORMAL:
1031 for n
in range(1, 2**32, 10000000):
1034 assert bitset(n, i,
False) == n - (1<<i), (n, n - (1<<i),
1036 assert bitset(n, i,
True) == n, (n,
bitset(n, i,
True))
1038 assert bitset(n, i,
False) == n, (n,
bitset(n, i,
False))
1039 assert bitset(n, i,
True) == n + (1<<i), (n, n + (1<<i),
bitset(n, i,
True))
1040 for n
in range(2**32 - 2048, 2**32 + 5000):
1043 assert bitset(n, i,
False) == n - (1<<i), (n, n - (1<<i),
1045 assert bitset(n, i,
True) == n, (n,
bitset(n, i,
True))
1047 assert bitset(n, i,
False) == n, (n,
bitset(n, i,
False))
1048 assert bitset(n, i,
True) == n + (1<<i), (n, n + (1<<i),
bitset(n, i,
True))
1050 print(debug.assertspeed_str(), end=
'')
1051 print(
'ok'); sys.stdout.flush()
1054 print(
'bitset0()...', end=
''); sys.stdout.flush()
1055 for n
in range(2048):
1058 assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i),
bitset0(n, i))
1062 if debug.assertspeed >= debug.ASSERT_NORMAL:
1063 for n
in range(1, 2**32, 10000000):
1066 assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i),
bitset0(n, i))
1070 for n
in range(2**32 - 2048, 2**32 + 5000):
1073 assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i),
bitset0(n, i))
1078 print(debug.assertspeed_str(), end=
'')
1079 print(
'ok'); sys.stdout.flush()
1082 print(
'bitset1()...', end=
''); sys.stdout.flush()
1083 for n
in range(2048):
1088 assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i),
bitset1(n, i))
1090 if debug.assertspeed >= debug.ASSERT_NORMAL:
1091 for n
in range(1, 2**32, 10000000):
1096 assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i),
bitset1(n, i))
1098 for n
in range(2**32 - 2048, 2**32 + 5000):
1103 assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i),
bitset1(n, i))
1106 print(debug.assertspeed_str(), end=
'')
1107 print(
'ok'); sys.stdout.flush()
1110 print(
'coprime_is()...', end=
''); sys.stdout.flush()
1119 for p
in nat32.PRIME_16_ARRAY[:100]:
1128 fractions.gcd(n, m))
1129 for m
in range(2**32 - 4096, 2**32 + 4096):
1131 print(
'ok'); sys.stdout.flush()
1134 print(
'distance_dominici()...', end=
'') ; sys.stdout.flush()
1136 for a
in range(1, 50):
1138 for b
in range(1, 50):
1141 print(
'???', end=
'')
1142 print(
'ok') ; sys.stdout.flush()
1145 print(
'divisors()...', end=
''); sys.stdout.flush()
1152 for n
in range(1, 1000):
1154 assert d > 0, (n, d)
1155 assert n%d == 0, (n, d, n%d)
1156 print(
'ok'); sys.stdout.flush()
1159 print(
'divisors_cond()...', end=
''); sys.stdout.flush()
1167 f =
lambda d: d%2 == 0
1174 for n
in range(1, 1000):
1178 assert d > 0, (n, d)
1179 assert n%d == 0, (n, d, n%d)
1181 assert d > 0, (n, d)
1182 assert n%d == 0
and d%3 == 0, (n, d, n%d)
1184 assert d > 0, (n, d)
1185 assert n%d == 0
and d%3 == 1, (n, d, n%d)
1187 assert d > 0, (n, d)
1188 assert n%d == 0
and d%3 == 2, (n, d, n%d)
1196 print(
'ok'); sys.stdout.flush()
1199 print(
'factorial()...', end=
''); sys.stdout.flush()
1202 for n
in range(1, 1000):
1206 print(
'ok'); sys.stdout.flush()
1209 print(
'falling_factorial_pow()...', end=
''); sys.stdout.flush()
1211 for n
in range(2000):
1224 for k
in range(1, 100):
1226 for k
in range(1, 20):
1227 for n
in range(k, min(100, int(pow(bit32.MERSENNE32, 1/k)) + 3)):
1229 (n, k, math.factorial(n + k - 1)//math.factorial(n - 1),
1231 print(
'ok'); sys.stdout.flush()
1234 print(
'fibonacci()...', end=
''); sys.stdout.flush()
1241 for k
in range(5000):
1243 Fk_1, Fk = Fk, Fk + Fk_1
1244 print(
'ok'); sys.stdout.flush()
1247 print(
'fibonacci2()...', end=
''); sys.stdout.flush()
1254 for k
in range(5000):
1256 Fk_1, Fk = Fk, Fk + Fk_1
1257 print(
'ok'); sys.stdout.flush()
1260 print(
'fibonacci_is()...', end=
''); sys.stdout.flush()
1266 for k
in range(0, 1000):
1268 for n
in range(32768):
1273 if debug.assertspeed >= debug.ASSERT_NORMAL:
1274 for n
in range(1, 2**45, 1000000000):
1279 for n
in range(2**32 - 32768, 2**32 + 32768):
1285 print(debug.assertspeed_str(), end=
'')
1286 print(
'ok'); sys.stdout.flush()
1289 print(
'fibonacci_to_index()...', end=
''); sys.stdout.flush()
1295 for k
in range(3, 1000):
1298 for n
in range(32768):
1302 if debug.assertspeed >= debug.ASSERT_NORMAL:
1303 for n
in range(1, 2**45, 1000000000):
1307 for n
in range(2**32 - 32768, 2**32 + 32768):
1312 print(debug.assertspeed_str(), end=
'')
1313 print(
'ok'); sys.stdout.flush()
1316 print(
'gcd()...', end=
''); sys.stdout.flush()
1317 for m
in range(100):
1318 assert gcd(m, 0) == m, (m,
gcd(m, 0))
1319 assert gcd(m, 1) == 1, (m,
gcd(m, 1))
1320 assert gcd(0, m) == m, (m,
gcd(0, m))
1321 assert gcd(1, m) == 1, (m,
gcd(1, m))
1322 assert gcd(m, m) == m, (m,
gcd(m, m))
1323 for p
in nat32.PRIME_16_ARRAY[:100]:
1325 assert gcd(m, p) == p, (m, p,
gcd(m, p))
1327 assert gcd(m, p) == 1, (m, p,
gcd(m, p))
1328 for m
in range(100):
1330 assert gcd(m, n) == fractions.gcd(m, n), (m, n,
gcd(m, n), fractions.gcd(m, n))
1331 assert gcd(m, n) ==
gcd(n, m), (m, n,
gcd(m, n),
gcd(n, m))
1332 assert gcd(2**34, 3) == 1,
gcd(2**34, 3)
1333 assert gcd(2**34 + 1, 3) == 1,
gcd(2**34 + 1, 3)
1334 assert gcd(2**34 + 2, 3) == 3,
gcd(2**34 + 2, 3)
1335 print(
'ok'); sys.stdout.flush()
1338 print(
'lcm()...', end=
''); sys.stdout.flush()
1339 for m
in range(100):
1340 assert lcm(m, 0) == 0, (m,
lcm(m, 0))
1341 assert lcm(m, 1) == m, (m,
lcm(m, 1))
1342 assert lcm(0, m) == 0, (m,
lcm(0, m))
1343 assert lcm(1, m) == m, (m,
lcm(1, m))
1344 assert lcm(m, m) == m, (m,
lcm(m, m))
1345 for p
in nat32.PRIME_16_ARRAY[:100]:
1347 assert lcm(m, p) == m, (m, p,
lcm(m, p))
1349 assert lcm(m, p) == m * p, (m, p,
lcm(m, p))
1350 for m
in range(100):
1352 assert lcm(m, n) ==
lcm(n, m), (m, n,
lcm(m, n),
lcm(n, m))
1353 assert lcm(m, n)*fractions.gcd(m, n) == m*n, (m, n,
lcm(m, n),
1354 fractions.gcd(n, m),
1355 lcm(m, n)*fractions.gcd(n, m),
1357 assert lcm(2**34, 3) == 2**34 * 3, (
lcm(2**34, 3), 2**34 * 3)
1358 assert lcm(2**34 + 1, 3) == (2**34 + 1) * 3, (
lcm(2**34 + 1, 3), (2**34 + 1) * 3)
1359 assert lcm(2**34 + 2, 3) == 2**34 + 2, (
lcm(2**34 + 2, 3), 2**34 + 2)
1360 print(
'ok'); sys.stdout.flush()
1363 print(
'lg()...', end=
''); sys.stdout.flush()
1364 for n
in range(1, 4096):
1365 assert lg(n) == len(
bin(n)) - 1, (n,
lg(n), len(
bin(n)))
1366 for n
in range(1, 2**32, 10000000):
1367 assert lg(n) == len(
bin(n)) - 1, (n,
lg(n), len(
bin(n)))
1368 for n
in range(2**32 - 4096, 2**32 + 10000):
1369 assert lg(n) == len(
bin(n)) - 1, (n,
lg(n), len(
bin(n)))
1370 print(
'ok'); sys.stdout.flush()
1373 print(
'lucas()...', end=
''); sys.stdout.flush()
1376 for k
in range(5000):
1378 Lk_1, Lk = Lk, Lk + Lk_1
1379 print(
'ok'); sys.stdout.flush()
1382 print(
'lucas2()...', end=
''); sys.stdout.flush()
1389 for k
in range(5000):
1391 Lk_1, Lk = Lk, Lk + Lk_1
1392 print(
'ok'); sys.stdout.flush()
1395 print(
'lucas_is()...', end=
''); sys.stdout.flush()
1402 for k
in range(0, 1000):
1404 for n
in range(32768):
1409 if debug.assertspeed >= debug.ASSERT_NORMAL:
1410 for n
in range(1, 2**45, 1000000000):
1415 for n
in range(2**32 - 32768, 2**32 + 32768):
1421 print(debug.assertspeed_str(), end=
'')
1422 print(
'ok'); sys.stdout.flush()
1425 print(
'lucas_to_index()...', end=
''); sys.stdout.flush()
1432 for k
in range(3, 1000):
1434 for n
in range(32768):
1438 if debug.assertspeed >= debug.ASSERT_NORMAL:
1439 for n
in range(1, 2**45, 1000000000):
1443 for n
in range(2**32 - 32768, 2**32 + 32768):
1448 print(debug.assertspeed_str(), end=
'')
1449 print(
'ok'); sys.stdout.flush()
1452 print(
'mersenne()...', end=
''); sys.stdout.flush()
1455 print(
'ok'); sys.stdout.flush()
1458 print(
'mersenne_to_index()...', end=
''); sys.stdout.flush()
1464 for k
in range(3, 1000):
1467 for n
in range(32768):
1471 if debug.assertspeed >= debug.ASSERT_NORMAL:
1472 for n
in range(1, 2**45, 1000000000):
1476 for n
in range(2**32 - 32768, 2**32 + 32768):
1481 print(debug.assertspeed_str(), end=
'')
1482 print(
'ok'); sys.stdout.flush()
1485 print(
'mertens()...', end=
''); sys.stdout.flush()
1492 for n
in range(100):
1493 assert mertens(n + 1) ==
mertens(n) + factors.mobius(factors.primaries(n + 1)), \
1494 (n,
mertens(n + 1),
mertens(n), factors.mobius(factors.primaries(n + 1)))
1495 print(
'ok'); sys.stdout.flush()
1498 print(
'nbbits0()...', end=
''); sys.stdout.flush()
1500 for n
in range(1, 4096):
1502 for i
in range(
rscan1(n)):
1506 if debug.assertspeed >= debug.ASSERT_NORMAL:
1507 for n
in range(1, 2**34, 10000000):
1509 for i
in range(
rscan1(n)):
1513 for n
in range(2**32 - 4096, 2**32 + 4096):
1515 for i
in range(
rscan1(n)):
1520 print(debug.assertspeed_str(), end=
'')
1521 print(
'ok'); sys.stdout.flush()
1524 print(
'nbbits1()...', end=
''); sys.stdout.flush()
1526 for n
in range(4096):
1532 if debug.assertspeed >= debug.ASSERT_NORMAL:
1533 for n
in range(1, 2**34, 10000000):
1539 for n
in range(2**32 - 4096, 2**32 + 4096):
1546 print(debug.assertspeed_str(), end=
'')
1547 print(
'ok'); sys.stdout.flush()
1550 print(
'nbbits()...', end=
''); sys.stdout.flush()
1556 for n
in range(1, 16384):
1558 assert (1<<(l - 1)) <= n, (n, l)
1559 assert n < (1<<l), (n, l)
1561 if debug.assertspeed >= debug.ASSERT_NORMAL:
1562 for n
in range(1, 2**40, 1000000000):
1564 assert (1<<(l - 1)) <= n, (n, l)
1565 assert n < (1<<l), (n, l)
1567 for n
in range(2**32 - 16384, 2**32 + 32768):
1569 assert (1<<(l - 1)) <= n, (n, l)
1570 assert n < (1<<l), (n, l)
1573 print(debug.assertspeed_str(), end=
'')
1574 print(
'ok'); sys.stdout.flush()
1577 print(
'nfp()...', end=
''); sys.stdout.flush()
1578 for m
in range(100):
1579 assert nfp(m, 0) == 0, (m,
nfp(m, 0))
1580 assert nfp(m, 1) == m, (m,
nfp(m, 1))
1581 assert nfp(0, m) == 0, (m,
nfp(0, m))
1582 assert nfp(1, m) == m, (m,
nfp(1, m))
1584 assert nfp(m, m) == 1, (m,
nfp(m, m))
1585 for p
in nat32.PRIME_16_ARRAY[:100]:
1587 assert nfp(m, p) == m // p, (m, p,
nfp(m, p))
1589 assert nfp(m, p) == m * p, (m, p,
nfp(m, p))
1590 for m
in range(100):
1592 assert nfp(m, n) ==
nfp(n, m), (m, n,
nfp(m, n),
nfp(n, m))
1593 assert nfp(m, n)*(fractions.gcd(m, n)**2) == m*n, (m, n,
nfp(m, n),
1594 fractions.gcd(n, m)**2,
1595 nfp(m, n)*(fractions.gcd(n,
1598 assert nfp(2**34, 3) == 2**34 * 3, (
nfp(2**34, 3), 2**34 * 3)
1599 assert nfp(2**34 + 1, 3) == (2**34 + 1) * 3, (
nfp(2**34 + 1, 3), (2**34 + 1) * 3)
1600 assert nfp(2**34 + 2, 3) == (2**34 + 2) // 3, (
nfp(2**34 + 2, 3), (2**34 + 2) // 3)
1601 print(
'ok'); sys.stdout.flush()
1604 print(
'nontotatives()...', end=
''); sys.stdout.flush()
1612 assert nontotatives(24) == [2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24], \
1614 for n
in range(500
if debug.assertspeed >= debug.ASSERT_NORMAL
1617 assert d <= n, (n, d)
1619 for p
in nat32.PRIME_16_ARRAY[:100]:
1621 if debug.assertspeed < debug.ASSERT_NORMAL:
1622 print(debug.assertspeed_str(), end=
'')
1623 print(
'ok'); sys.stdout.flush()
1626 print(
'nontotient()...', end=
''); sys.stdout.flush()
1635 for n
in range(500
if debug.assertspeed >= debug.ASSERT_NORMAL
1639 for p
in nat32.PRIME_16_ARRAY[:100]:
1641 if debug.assertspeed < debug.ASSERT_NORMAL:
1642 print(debug.assertspeed_str(), end=
'')
1643 print(
'ok'); sys.stdout.flush()
1646 print(
'polygonal()...', end=
''); sys.stdout.flush()
1647 for n
in range(1000):
1652 for k
in range(2, 1000):
1657 print(
'ok'); sys.stdout.flush()
1660 print(
'pow2()...', end=
''); sys.stdout.flush()
1661 for k
in range(1000):
1662 assert pow2(k) == 2**k, (k,
pow2(k))
1663 print(
'ok'); sys.stdout.flush()
1666 print(
'pow3()...', end=
''); sys.stdout.flush()
1667 for k
in range(1000):
1668 assert pow3(k) == 3**k, (k,
pow3(k))
1669 print(
'ok'); sys.stdout.flush()
1672 print(
'prime_is()...', end=
''); sys.stdout.flush()
1680 for n
in range(2**32 - 4, 2**32 + 15):
1682 print(
'ok'); sys.stdout.flush()
1685 print(
'pyramidal()...', end=
''); sys.stdout.flush()
1686 for n
in range(1000):
1688 assert pyramidal(3, n) == n*(n + 1)*(n + 2)//6, \
1689 (n,
pyramidal(3, n), n*(n + 1)*(n + 2)//6)
1690 assert pyramidal(4, n) == n*(n + 1)*(2*n + 1)//6, \
1691 (n,
pyramidal(4, n), n*(n + 1)*(2*n + 1)//6)
1693 for k
in range(2, 1000):
1698 print(
'ok'); sys.stdout.flush()
1701 print(
'rising_factorial_pow()...', end=
''); sys.stdout.flush()
1703 for n
in range(2000):
1714 for k
in range(1, 20):
1716 for n
in range(1, min(100, int(pow(bit32.MERSENNE32, 1/k) - k + 4))):
1718 == math.factorial(n + k - 1)//math.factorial(n - 1), \
1719 (n, math.factorial(n + k - 1)//math.factorial(n - 1),
1721 print(
'ok'); sys.stdout.flush()
1724 print(
'rscan1()...', end=
''); sys.stdout.flush()
1731 assert rscan1(bit32.MERSENNE32) == 31
1732 for n
in range(1, 4096):
1734 if debug.assertspeed >= debug.ASSERT_NORMAL:
1735 for n
in range(1, 2**34, 10000000):
1737 for n
in range(2**32 - 4096, 2**32 + 4096):
1740 print(debug.assertspeed_str(), end=
'')
1741 print(
'ok'); sys.stdout.flush()
1744 print(
'scan0()...', end=
''); sys.stdout.flush()
1746 assert scan0(2**i - 1) == i, (i,
scan0(2**i - 1))
1747 assert scan0(2**32 - 1) == 32,
scan0(2**32 - 1)
1748 for n
in range(32768):
1753 if debug.assertspeed >= debug.ASSERT_NORMAL:
1754 for n
in range(1, 2**45, 1000000000):
1759 for n
in range(2**32 - 32768, 2**32 + 32768):
1765 print(debug.assertspeed_str(), end=
'')
1766 print(
'ok'); sys.stdout.flush()
1769 print(
'scan1()...', end=
''); sys.stdout.flush()
1773 for n
in range(1, 32768):
1778 if debug.assertspeed >= debug.ASSERT_NORMAL:
1779 for n
in range(1, 2**45, 1000000000):
1784 for n
in range(2**32 - 32768, 2**32 + 32768):
1790 print(debug.assertspeed_str(), end=
'')
1791 print(
'ok'); sys.stdout.flush()
1794 print(
'totatives()...', end=
''); sys.stdout.flush()
1803 for n
in range(500
if debug.assertspeed >= debug.ASSERT_NORMAL
1807 assert d <= n, (n, d)
1811 assert tnt == [x
for x
in range(1, n + 1)], \
1812 (n, t, tnt, [x
for x
in range(1, n + 1)])
1813 for p
in nat32.PRIME_16_ARRAY[:100]:
1815 if debug.assertspeed < debug.ASSERT_NORMAL:
1816 print(debug.assertspeed_str(), end=
'')
1817 print(
'ok'); sys.stdout.flush()
1820 print(
'totient()...', end=
''); sys.stdout.flush()
1829 for n
in range(1, 500
if debug.assertspeed >= debug.ASSERT_NORMAL
1833 for p
in nat32.PRIME_16_ARRAY[:100]:
1834 for k
in range(1, 10):
1835 assert totient(p**k) == (p - 1) * p**(k - 1), (p, k,
totient(p**k))
1836 if debug.assertspeed < debug.ASSERT_NORMAL:
1837 print(debug.assertspeed_str(), end=
'')
1838 print(
'ok'); sys.stdout.flush()
1841 print(
'unitarydivisors()...', end=
''); sys.stdout.flush()
1849 for n
in range(1, 1000):
1852 assert d > 0, (n, d)
1853 assert n%d == 0, (n, d, n%d)
1854 assert nat32.coprime_is(n//d, d), (n, d, n//d)
1859 print(
'ok'); sys.stdout.flush()