DSPython  00.03.03 — 25 juin 2012
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
natural.py
Aller à la documentation de ce fichier.
1 #!/usr/bin/env python
2 # -*- coding: latin-1 -*-
3 ##\package DSPython.natural Naturels : fonctions arithmétiques
4 
5 ##\file
6 # Naturels : fonctions arithmétiques
7 
8 # (c) Olivier Pirson --- DragonSoft
9 # http://www.opimedia.be/DS/
10 # Débuté le 25 septembre 2006
11 ####################################
12 from __future__ import division
13 from __future__ import print_function
14 
15 ## Date du dernier changement pour ce module
16 VERSION = 'natural --- 2010 April 12'
17 
18 import fractions, math, types
19 
20 import DSPython
21 import DSPython.bit32 as bit32
22 import DSPython.nat32 as nat32
23 
24 
25 
26 # ###########
27 # Fonctions #
28 #############
29 ## n en binaire dans un string
30 def bin(n):
31  """Renvoie un string représentant n en binaire
32  (Si n == 0 alors renvoie '0')
33 
34  Pre: n: naturel
35 
36  Result: string
37 
38  O(n) = lg(n)"""
39  assert DSPython.natural_is(n), n
40 
41  if n <= bit32.MERSENNE32:
42  return bit32.bin(n)
43  else:
44  l = [bit32.bin(n & bit32.MERSENNE32)]
45  while n > bit32.MERSENNE32:
46  if len(l[0]) < 32:
47  l.insert(0, '0' * (32 - len(l[0])))
48  n >>= 32
49  l.insert(0, bit32.bin(n & bit32.MERSENNE32))
50  return ''.join(l)
51 
52 
53 ## Coefficient binomial de n et k
54 def binom(n, k):
55  """Renvoie le coefficient binomial (n)
56  (k)
57 
58  Pre: n: naturel
59  k: naturel
60 
61  Result: naturel
62 
63  O(n, k) = ..."""
64  assert DSPython.natural_is(n), n
65  assert DSPython.natural_is(k), k
66 
67  if k <= n:
68  if n - k < k:
69  k = n - k
70  return falling_factorial_pow(n, k) // math.factorial(k)
71  else:
72  return 0
73 
74 
75 ## Le bit d'indice i de n vaut 1 ?
76 def bit(n, i):
77  """Renvoie True si le bit d'indice i de n vaut 1,
78  False sinon
79 
80  Pre: n: naturel
81  i: naturel
82 
83  Result: bool
84 
85  O(n) = 1"""
86  assert DSPython.natural_is(n), n
87  assert DSPython.natural_is(i), i
88 
89  return bool(n & (1 << i))
90 
91 
92 ## n avec son bit d'indice i à b
93 def bitset(n, i, b):
94  """Si b alors renvoie n avec son bit d'indice i à 1,
95  sinon renvoie n avec son bit d'indice i à 0
96 
97  Pre: n: naturel
98  i: naturel
99  b: bool
100 
101  Result: naturel
102 
103  O(n) = 1"""
104  assert DSPython.natural_is(n), n
105  assert DSPython.natural_is(i), i
106 
107  return (n | (1 << i) if b
108  else n & ~(1 << i))
109 
110 
111 ## n avec son bit d'indice i à 0
112 def bitset0(n, i):
113  """Renvoie n avec son bit d'indice i à 0
114 
115  Pre: n: naturel
116  i: naturel
117 
118  Result: naturel
119 
120  O(n) = 1"""
121  assert DSPython.natural_is(n), n
122  assert DSPython.natural_is(i), i
123 
124  return n & ~(1 << i)
125 
126 
127 ## n avec son bit d'indice i à 1
128 def bitset1(n, i):
129  """Renvoie n avec son bit d'indice i à 1
130 
131  Pre: n: naturel
132  i: naturel
133 
134  Result: naturel
135 
136  O(n) = 1"""
137  assert DSPython.natural_is(n), n
138  assert DSPython.natural_is(i), i
139 
140  return n | (1 << i)
141 
142 
143 ## m et n sont premiers entre eux ?
144 def coprime_is(m, n):
145  """Renvoie True si m et n sont premiers entre eux,
146  False sinon
147  (Si m == n == 0 alors renvoie False)
148 
149  Pre: m: naturel
150  n: naturel
151 
152  O(m, n) = ..."""
153  assert DSPython.natural_is(m), m
154  assert DSPython.natural_is(n), n
155 
156  return (not ((m&1 == 0) and (n&1 == 0))) and (fractions.gcd(m, n) == 1)
157 
158 
159 ##\brief Distance de Dominici
160 #
161 # (cf. <i>An Arithmetic Metric</i>,
162 # Diego <span style="font-variant:small-caps">Dominici</span>,
163 # <a href="http://arxiv.org/abs/0906.0632/"
164 # target="_blank"><tt>http://arxiv.org/abs/0906.0632/</tt></a>)
166  """Renvoie la distance de Dominici entre a et b
167 
168  Pre: a: naturel >= 1
169  b: naturel >= 1
170 
171  Result: naturel
172 
173  O(s) = ..."""
174  assert DSPython.natural_is(a), a
175  assert a > 0, a
176  assert DSPython.natural_is(b), b
177  assert b > 0, b
178 
179  import DSPython.factors as factors
180 
181  return factors.distance_dominici(factors.primaries(a), factors.primaries(b))
182 
183 
184 ## Liste des diviseurs de n
185 def divisors(n):
186  """Renvoie la liste des diviseurs de n
187  (dans l'ordre strictement croissant)
188  (Si n == 1 alors renvoie [1])
189 
190  Pre: n: naturel >= 1
191 
192  Result: séquence strictement ordonnée non vide de naturels >= 1
193 
194  O(n) = ..."""
195  assert DSPython.natural_is(n), n
196  assert n > 0, n
197 
198  l = []
199  for d in range(1, n):
200  if n%d == 0:
201  l.append(d)
202  l.append(n)
203  return l
204 
205 
206 ## Liste des diviseurs de n restreint à la condition cond
207 def divisors_cond(n, cond):
208  """Renvoie la liste des diviseurs de n vérifiant la condition cond
209  (dans l'ordre strictement croissant)
210  Pre: n: naturel >= 1
211  cond: fonction à une variable naturelle et renvoyant un bool
212  Result: séquence strictement ordonnée non vide de naturels >= 1
213  O(n) = ..."""
214  assert DSPython.natural_is(n), n
215  assert n > 0, n
216  assert isinstance(cond, types.FunctionType), type(cond)
217 
218  l = []
219  for d in range(1, n + 1):
220  if (n%d == 0) and cond(d):
221  l.append(d)
222  return l
223 
224 
225 ## n!
226 def factorial(n):
227  """Renvoie n!, la factorielle de n
228  == 1 * 2 * 3 * ... * n
229  (Si n == 0 alors renvoie 1)
230 
231  Pre: n: naturel
232 
233  Result: naturel >= 1
234 
235  O(n) = ..."""
236  assert DSPython.natural_is(n), n
237 
238  return math.factorial(n)
239 
240 
241 ## k<sup>e</sup> puissance factorielle descendante de n
243  """Renvoie la kème puissance factorielle descendante de n
244  == n * (n - 1) * (n - 2) * ... * (n - k + 1)
245 
246  Pre: n: naturel
247  k: naturel
248 
249  Result: naturel
250 
251  O(n, k) = ..."""
252  assert DSPython.natural_is(n), n
253  assert DSPython.natural_is(k), k
254 
255  if n - 1 > k > 0: # n - 1 > k > 0
256  if n <= 12:
257  return nat32.FACTORIAL_TUPLE[n] // nat32.FACTORIAL_TUPLE[n - k]
258  else:
259  if k&1 == 0:
260  f = n * (n - 1)
261  n -= 2
262  k -= 2
263  else:
264  f = n
265  n -= 1
266  k >>= 1
267  while k > 0: # multiplie les 2k facteurs restants
268  f *= n * (n - 1)
269  n -= 2
270  k -= 1
271  return f
272  elif (n - 1 == k) or (n == k): # n = k + (0 ou 1)
273  return math.factorial(n)
274  elif k == 0: # n > 1, k = 0
275  return 1
276  else: # n < k > 0
277  return 0
278 
279 
280 #
281 # Nombres de Fibonacci :
282 # F_0 = 0
283 # F_1 = 1
284 # pour tout k entier : F_k = F_(k-2) + F_(k-1)
285 #
286 # k : -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21...
287 # F_k : 1 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
288 #
289 # ==> Pour tout k naturel : F_(2k-1) == F_(k )**2 + F_(k-1)**2
290 # F_(2k ) == F_(k+1)**2 - F_(k-1)**2
291 # F_(2k+1) == F_(k+1)**2 + F_(k )**2
292 #
293 
294 ## F<sub>k</sub>
295 def fibonacci(k):
296  """Renvoie F_k, le kième nombre de Fibonacci
297 
298  Pre: k: naturel
299 
300  Result: naturel
301 
302  O(k) = ..."""
303  assert DSPython.natural_is(k), k
304 
305  return fibonacci2(k)[1]
306 
307 
308 ## (F<sub>k-1</sub>, F<sub>k</sub>)
309 def fibonacci2(k):
310  """Renvoie (F_(k-1), F_k), les (k-1)ième et kième nombres de Fibonacci
311 
312  Pre: k: naturel
313 
314  Result: couple de naturels
315 
316  O(k) = ..."""
317  assert DSPython.natural_is(k), k
318 
319  if k > 47:
320  Fq_1, Fq = fibonacci2(k//2) # F_(q-1), F_q avec q == k/2
321  Fq1S = Fq + Fq_1 # F_(q+1)
322  Fq1S *= Fq1S # F_(q+1)**2
323  if k&1 == 0: # k pair
324  Fq_1S = Fq_1 * Fq_1 # F_(q-1)**2
325  return (Fq*Fq + Fq_1S, Fq1S - Fq_1S) # ( F_(2q-1), F_(2q ) )
326  else: # k impair
327  return (Fq1S - Fq_1*Fq_1, Fq1S + Fq*Fq) # ( F_(2q ), F_(2q+1) )
328  else:
329  return nat32.fibonacci2(k)
330 
331 
332 ## Le naturel n est un nombre de Fibonacci ?
334  """Renvoie True si n est un nombre de Fibonacci,
335  False sinon
336 
337  Pre: n: naturel
338 
339  Result: bool
340 
341  O(n) = ..."""
342  assert DSPython.natural_is(n), n
343 
344  if n <= bit32.MERSENNE32:
345  return nat32.fibonacci_is(n)
346  else:
347  Fk_1, Fk = fibonacci2(48)
348  while Fk < n:
349  Fk_1, Fk = Fk, Fk+Fk_1
350  return Fk == n
351 
352 
353 ## Indice du nombre de Fibonacci correspondant au naturel n, ou None
355  """Si n == F_k alors renvoie k
356  (si n == 1 alors renvoie toujours 1, jamais 2),
357  sinon renvoie None
358 
359  Pre: n naturel ou None
360 
361  O(n) = ..."""
362  assert DSPython.natural_is(n), n
363 
364  if n <= bit32.MERSENNE32:
365  return nat32.fibonacci_to_index(n)
366  else:
367  k = 48
368  Fk_1, Fk = fibonacci2(k)
369  while Fk < n:
370  Fk_1, Fk = Fk, Fk+Fk_1
371  k += 1
372  return (k if Fk == n
373  else None)
374 
375 
376 ## PGCD de m et n (Plus Grand Commun Diviseur/ Greatest Common Divisor)
377 def gcd(m, n):
378  """Renvoie le PGCD de m et n
379  (Si m == n == 0 alors renvoie 0)
380  (Plus Grand Commun Diviseur/ Greatest Common Divisor)
381 
382  Pre: m: naturel
383  n: naturel
384 
385  Result: naturel
386 
387  O(m, n) = ..."""
388  assert DSPython.natural_is(m), m
389  assert DSPython.natural_is(n), n
390 
391  return fractions.gcd(m, n)
392 
393 
394 ## PPCM de m et n (Plus Petit Commun Multiple/ Least Common Multiple)
395 def lcm(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)
399 
400  Pre: m: naturel
401  n: naturel
402 
403  Result: naturel
404 
405  O(m, n) = ..."""
406  assert DSPython.natural_is(m), m
407  assert DSPython.natural_is(n), n
408 
409  return (m*n//fractions.gcd(m, n) if (m != 0) and (n != 0)
410  else 0)
411 
412 
413 ## lg(n) == log<sub>2</sub>(n) == nbbits(n) - 1 == l'indice du dernier bit à 1 de n
414 ## &nbsp; &nbsp; (n != 0)
415 def lg(n):
416  """Renvoie lg(n) == log_2(n) == nbbits(n) - 1
417  == l'indice du dernier bit à 1 de n
418 
419  Pre: n: 1 <= naturel
420 
421  Result: naturel
422 
423  O(n) = 1"""
424  assert DSPython.natural_is(n), n
425 
426  return nbbits(n) - 1
427 
428 
429 #
430 # Nombres de Lucas :
431 # L_0 = 2
432 # L_1 = 1
433 # pour tout k entier : L_k = L_(k-2) + L_(k-1)
434 #
435 # k : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20...
436 # L_k : 2 1 3 4 7 11 18 29 47 76
437 #
438 # ==> Pour tout k naturel : L_k == F_{k-1} + F_{k+1} == 2F_{k-1} + F_k
439 #
440 
441 ## L<sub>k</sub>
442 def lucas(k):
443  """Renvoie L_k, le kième nombre de Lucas
444 
445  Pre: k: naturel
446 
447  Result: naturel
448 
449  O(k) = ..."""
450  assert DSPython.natural_is(k), k
451 
452  Fk_1, Fk= fibonacci2(k)
453  return (Fk_1 << 1) + Fk
454 
455 
456 ## (L<sub>k-1</sub>, L<sub>k</sub>)
457 def lucas2(k):
458  """Renvoie (L_(k-1), L_k), les (k-1)ième et kième nombres de Lucas
459 
460  Pre: k: naturel
461 
462  Result: (-1, 2) si k == 0, sinon couple de naturels
463 
464  O(k) = ..."""
465  assert DSPython.natural_is(k), k
466 
467  if k > 0:
468  Fk_2, Fk_1= fibonacci2(k - 1)
469  Fk = Fk_1 + Fk_2
470  return (Fk + Fk_2, (Fk_1 << 1) + Fk)
471  else:
472  return (-1, 2)
473 
474 
475 ## Le naturel n est un nombre de Lucas ?
476 def lucas_is(n):
477  """Renvoie True si n est un nombre de Lucas,
478  False sinon
479 
480  Pre: n: naturel
481 
482  Result: bool
483 
484  O(n) = ..."""
485  assert DSPython.natural_is(n), n
486 
487  if n != 2:
488  Lk_1, Lk = 2, 1
489  while Lk < n:
490  Lk_1, Lk = Lk, Lk + Lk_1
491  return Lk == n
492  else:
493  return True
494 
495 
496 ## Indice du nombre de Lucas correspondant au naturel n, ou None
498  """Si n == L_k alors renvoie k
499  sinon renvoie None
500 
501  Pre: n: naturel
502 
503  Result: naturel ou None
504 
505  O(n) = ..."""
506  assert DSPython.natural_is(n), n
507 
508  if n != 2:
509  k = 1
510  Lk_1, Lk = 2, 1
511  while Lk < n:
512  Lk_1, Lk = Lk, Lk + Lk_1
513  k += 1
514  return (k if Lk == n
515  else None)
516  else:
517  return 0
518 
519 
520 ## M<sub>k</sub>
521 def mersenne(k):
522  """Renvoie le kième nombre de Mersenne Mk == 2**k - 1
523 
524  Pre: k: naturel
525 
526  Result: naturel
527 
528  O(k) = 1"""
529  assert DSPython.natural_is(k), k
530 
531  return (bit32.MERSENNE_TUPLE[k] if k <= 32
532  else (2 << (k - 1)) - 1)
533 #??? else (2L << (k - 1)) - 1L)
534 
535 
536 ## Indice du nombre de Mersenne correspondant au naturel n, ou None
538  """Si n == M_k alors renvoie k
539  sinon renvoie None
540 
541  Pre: n: naturel ou None
542 
543  O(n) = ..."""
544  assert DSPython.natural_is(n), n
545 
546  k = scan0(n)
547  return (k if mersenne(k) == n
548  else None)
549 
550 
551 ## Fonction de Mertens
552 def mertens(n):
553  """Renvoie la fonction de Mertens en n
554  (Si n == 0 alors renvoie 0)
555 
556  Pre: n: naturel
557 
558  Result: entier
559 
560  O(n) = ..."""
561  assert DSPython.natural_is(n), n
562 
563  import DSPython.factors as factors
564 
565  r = 0
566  for i in range(1, n + 1):
567  r += factors.mobius(factors.primaries(i))
568  return r
569 
570 
571 ## Nombre de bits à 0 de n
572 def nbbits0(n):
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)
576 
577  Pre: n: naturel
578 
579  Result: naturel
580 
581  O(n) = ..."""
582  assert DSPython.natural_is(n), n
583 
584  nb = 0
585  while n > 0:
586  n32 = n & bit32.MERSENNE32
587  n >>= 32
588  if n32 > 0:
589  nb += bit32.nbbits0(n32)
590  if n > 0:
591  nb += 31 - bit32.rscan1(n32)
592  elif n > 0:
593  nb += 32
594  return nb
595 
596 
597 ## Nombre de bits à 1 de n
598 def nbbits1(n):
599  """Renvoie le nombre de bits à 1 de n
600  (Si n == 0 alors renvoie 0)
601 
602  Pre: n: naturel
603 
604  Result: naturel
605 
606  O(n) = ..."""
607  assert DSPython.natural_is(n), n
608 
609  nb = 0
610  while n > 0:
611  nb += bit32.nbbits1(n & bit32.MERSENNE32)
612  n >>= 32
613  return nb
614 
615 
616 ## Nombre de bits de n
617 def nbbits(n):
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)
622 
623  Pre: n: naturel
624 
625  Result: naturel
626 
627  O(n) = ..."""
628  assert DSPython.natural_is(n), n
629 
630  k = 0
631  while n > bit32.MERSENNE32:
632  n >>= 32
633  k += 1
634  return (k<<5) + bit32.nbbits(n)
635 
636 
637 ## Produit des facteurs non communs de m et n (Not common Factors Product)
638 def nfp(m, 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)
642 
643  Pre: m: naturel
644  n: naturel
645 
646  Result: naturel
647 
648  O(m, n) = ..."""
649  assert DSPython.natural_is(m), m
650  assert DSPython.natural_is(n), n
651 
652  return (m*n//(fractions.gcd(m, n)**2) if (m != 0) and (n != 0)
653  else 0)
654 
655 
656 ## Liste des nontotatives de n
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 [])
662 
663  Pre: n: naturel
664 
665  Result: séquence strictement ordonnée de naturels
666 
667  O(n) = ..."""
668  assert DSPython.natural_is(n), n
669 
670  if n > 1:
671  l = []
672  for d in range(2, n):
673  if not coprime_is(n, d):
674  l.append(d)
675  l.append(n)
676  return l
677  else:
678  return []
679 
680 
681 ## Nontotient de n
682 def nontotient(n):
683  """Renvoie le nombre de nontotatives de n
684  (Si n == 0 ou 1 alors renvoie 0)
685 
686  Pre: n: naturel
687 
688  Result: naturel
689 
690  O(n) = ..."""
691  assert DSPython.natural_is(n), n
692 
693  import DSPython.factors as factors
694 
695  return (n - factors.totient(factors.primaries(n)) if n > 1
696  else 0)
697 
698 
699 ## Nombre polygonal à k côtés de n
700 def polygonal(k, n):
701  """Nombre polygonal à k côtés de n
702 
703  Pre: k: naturel >= 2
704  n: naturel
705 
706  Result: naturel
707 
708  O(n) = ..."""
709  assert DSPython.natural_is(k), k
710  assert k >= 2, k
711  assert DSPython.natural_is(n), n
712 
713  return ((n*(n - 1)) >> 1) * (k - 2) + n
714 
715 
716 ## 2<sup>k</sup>
717 def pow2(k):
718  """Renvoie la kième puissance de 2 : 2**k
719 
720  Pre: k: naturel
721 
722  Result: naturel >= 1
723 
724  O(k) = 1"""
725  assert DSPython.natural_is(k), k
726 
727  return (bit32.POW2_TUPLE[k] if k < 32
728  else 1<<k)
729 
730 
731 ## 3<sup>k</sup>
732 def pow3(k):
733  """Renvoie la kième puissance de 3 : 3**k
734 
735  Pre: k: naturel
736 
737  Result: naturel >= 1
738 
739  O(k) = ..."""
740  assert DSPython.natural_is(k), k
741 
742  k, rem = divmod(k, 20)
743  return (nat32.POW3_TUPLE[20]**k) * nat32.POW3_TUPLE[rem]
744 
745 
746 ## n est premier ?
747 def prime_is(n):
748  """Renvoie True si n est premier,
749  False sinon
750 
751  Pre: n: naturel
752 
753  Result: bool
754 
755  O(n) = ..."""
756  assert DSPython.natural_is(n), n
757 
758  if n < 2**32:
759  return nat32.prime_is(n)
760  elif n <= 2**32 + 14:
761  return False
762  else:
763  raise NotImplementedError
764 
765 
766 ## Nombre pyramidal à c côtés de n
767 def pyramidal(k, n):
768  """Nombre pyramidal à k côtés de n
769 
770  Pre: k: naturel >= 2
771  n: naturel
772 
773  Result: naturel
774 
775  O(n) = ..."""
776  assert DSPython.natural_is(k), k
777  assert k >= 2, k
778  assert DSPython.natural_is(n), n
779 
780  s = n**2
781  return (s*n*(k - 2) + s*3 - n*(k - 5)) // 6
782 
783 
784 ## k<sup>e</sup> puissance factorielle montante de n
786  """Renvoie la kème puissance factorielle montante de n
787  == n * (n + 1) * (n + 2) * ... * (n + k - 1)
788 
789  Pre: n: naturel
790  k: naturel
791 
792  Result: naturel
793 
794  O(n, k) = ..."""
795  assert DSPython.natural_is(n), n
796  assert DSPython.natural_is(k), k
797 
798  if (n > 0) and (k > 0): # n, k > 0
799  if n + k < 14: # n + k - 1 <= 12
800  return nat32.FACTORIAL_TUPLE[n + k - 1] // nat32.FACTORIAL_TUPLE[n - 1]
801  else:
802  if k&1 == 0:
803  f = n * (n + 1)
804  n += 2
805  k -= 2
806  else:
807  f = n
808  n += 1
809  k >>= 1
810  while k > 0: # multiplie les 2k facteurs restants
811  f *= n * (n + 1)
812  n += 2
813  k -= 1
814  return f
815  elif k == 0: # n >= k = 0
816  return 1
817  else: # n = 0 < k
818  return 0
819 
820 
821 ## Indice du dernier bit à 1 de, n == nbbits(n) - 1 ou None
822 def rscan1(n):
823  """Renvoie l'indice du dernier bit à 1 de n
824  ou None si n == 0
825 
826  Pre: n: naturel
827 
828  Result: naturel ou None
829 
830  O(n) = 1"""
831  assert DSPython.natural_is(n), n
832 
833  return (nbbits(n) - 1 if n > 0
834  else None)
835 
836 
837 ## Indice du premier bit à 0 de n
838 def scan0(n):
839  """Renvoie l'indice du premier bit à 0 de n
840  (si n == 0 alors renvoie 0)
841 
842  Pre: n: naturel
843 
844  Result: naturel
845 
846  O(n) = ..."""
847  assert DSPython.natural_is(n), n
848 
849  k = 0
850  while n & bit32.MERSENNE32 == bit32.MERSENNE32:
851  n >>= 32
852  k += 1
853  return (k << 5) + bit32.scan0(n & bit32.MERSENNE32)
854 
855 
856 ## Indice du premier bit à 1 de n, ou None
857 def scan1(n):
858  """Renvoie l'indice du premier bit à 1 de n
859  ou None si n == 0
860 
861  Pre: n: naturel
862 
863  Result: naturel ou None
864 
865  O(n) = ..."""
866  assert DSPython.natural_is(n), n
867 
868  if n > 0:
869  k = 0
870  while n&bit32.MERSENNE32 == 0:
871  n >>= 32
872  k += 1
873  return (k << 5) + bit32.scan1(n & bit32.MERSENNE32)
874  else:
875  return None
876 
877 
878 ## Liste des totatives de n
879 def totatives(n):
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])
885 
886  Pre: n: naturel
887 
888  Result: séquence strictement ordonnée de naturels
889 
890  O(n) = ..."""
891  assert DSPython.natural_is(n), n
892 
893  if n > 0:
894  l = [1]
895  for d in range(2, n):
896  if coprime_is(n, d):
897  l.append(d)
898  return l
899  else:
900  return []
901 
902 
903 ## Totient de n (fonction phi, indicatrice d'Euler)
904 def totient(n):
905  """Si n > 0 alors renvoie le nombre de totatives de n
906  sinon renvoie 1
907  (Si n == 0 ou 1 alors renvoie 1)
908 
909  Pre: n: naturel
910 
911  Result: naturel >= 1
912 
913  O(n) = ..."""
914  assert DSPython.natural_is(n), n
915 
916  import DSPython.factors as factors
917 
918  return (factors.totient(factors.primaries(n)) if n > 1
919  else 1)
920 
921 
922 ## Liste des diviseurs unitaires de n
924  """Renvoie la liste des diviseurs unitaires de n
925  (dans l'ordre strictement croissant)
926  (Si n == 1 alors renvoie [1])
927 
928  Pre: n: naturel >= 1
929 
930  Result: séquence strictement ordonnée non vide de naturels >= 1
931 
932  O(n) = ..."""
933  assert DSPython.natural_is(n), n
934  assert n > 0, n
935 
936  l = [1]
937  for d in range(2, n + 1):
938  (q, r) = divmod(n, d)
939  if (r == 0) and coprime_is(q, d):
940  l.append(d)
941  return l
942 
943 
944 
945 # ######\cond MAINTEST
946 # Main #
947 ########
948 if __name__ == '__main__':
949  def main_test():
950  """Test du module"""
951  import sys
952 
953  try:
954  if not 'profile' in dir():
955  import psyco
956  psyco.full()
957  except ImportError:
958  pass
959 
960  import DSPython.debug as debug
961 
962  import DSPython.factors as factors
963 
964  debug.test_begin(VERSION, __debug__)
965 
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)) # 1...11
971  assert bin(2**i) == '1' + '0'*i, (i, bin(2**i)) # 10...00
972  assert bin(2**i + 1) == '1' + '0'*(i - 1) + '1', (i, bin(2**i + 1)) # 10...01
973  for n in range(1, 16384):
974  assert int(bin(n), 2) == n, (n, bin(n))
975  assert len(bin(n)) == nbbits(n), (n, bin(n), nbbits(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))
979  assert len(bin(n)) == nbbits(n), (n, bin(n), nbbits(n))
980  for n in range(2**32 - 16384, 2**32 + 16384):
981  assert int(bin(n), 2) == n, (n, bin(n))
982  assert len(bin(n)) == nbbits(n), (n, bin(n), nbbits(n))
983  else:
984  print(debug.assertspeed_str(), end='')
985  print('ok'); sys.stdout.flush()
986 
987 
988  print('binom()...', end=''); sys.stdout.flush()
989  for n in range(100):
990  assert binom(n, 0) == 1, (n, binom(n, 0))
991  assert binom(n, 1) == n, (n, binom(n, 1))
992  for k in range(1, 10):
993  assert binom(n, n+k) == 0, (n, k, binom(n, n+k))
994  s = 0
995  for k in range(n + 1):
996  s += binom(n, k)
997  assert s == 2**n, (n, s, 2**n)
998  for n in range(1, 50):
999  for k in range(1, n + 1):
1000  assert binom(n, k) == binom(n, n - k), (n, k, binom(n, k), binom(n, n - k))
1001  assert binom(n, k) == binom(n - 1, k - 1) * n // k, \
1002  (n, k, binom(n, k), binom(n - 1, k - 1))
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()
1008 
1009 
1010  print('bit()...', end=''); sys.stdout.flush()
1011  for i in range(40):
1012  assert bit(2**i, i) == True, (i, 2**i, bit(2**i, i))
1013  for j in range(40):
1014  assert bit(0, i) == False, (i, bit(0, i))
1015  assert bit(mersenne(40), i) == True, (i, bit(mersenne(40), i))
1016  if i != j:
1017  assert bit(2**i, j) == False, (i, j, 2**i, bit(2**i, j))
1018  print('ok'); sys.stdout.flush()
1019 
1020 
1021  print('bitset()...', end=''); sys.stdout.flush()
1022  for n in range(2048):
1023  for i in range(32):
1024  if bit(n, i):
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))
1027  else:
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):
1032  for i in range(32):
1033  if bit(n, i):
1034  assert bitset(n, i, False) == n - (1<<i), (n, n - (1<<i),
1035  bitset(n, i, False))
1036  assert bitset(n, i, True) == n, (n, bitset(n, i, True))
1037  else:
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):
1041  for i in range(32):
1042  if bit(n, i):
1043  assert bitset(n, i, False) == n - (1<<i), (n, n - (1<<i),
1044  bitset(n, i, False))
1045  assert bitset(n, i, True) == n, (n, bitset(n, i, True))
1046  else:
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))
1049  else:
1050  print(debug.assertspeed_str(), end='')
1051  print('ok'); sys.stdout.flush()
1052 
1053 
1054  print('bitset0()...', end=''); sys.stdout.flush()
1055  for n in range(2048):
1056  for i in range(32):
1057  if bit(n, i):
1058  assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i), bitset0(n, i))
1059  else:
1060  assert bitset0(n, i) == n, (n, bitset0(n, i))
1061  assert bitset0(n, i) == bitset(n, i, False), (n, bitset(n, i, False), bitset0(n, i))
1062  if debug.assertspeed >= debug.ASSERT_NORMAL:
1063  for n in range(1, 2**32, 10000000):
1064  for i in range(32):
1065  if bit(n, i):
1066  assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i), bitset0(n, i))
1067  else:
1068  assert bitset0(n, i) == n, (n, bitset0(n, i))
1069  assert bitset0(n, i) == bitset(n, i, False), (n, bitset(n, i, False), bitset0(n, i))
1070  for n in range(2**32 - 2048, 2**32 + 5000):
1071  for i in range(32):
1072  if bit(n, i):
1073  assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i), bitset0(n, i))
1074  else:
1075  assert bitset0(n, i) == n, (n, bitset0(n, i))
1076  assert bitset0(n, i) == bitset(n, i, False), (n, bitset(n, i, False), bitset0(n, i))
1077  else:
1078  print(debug.assertspeed_str(), end='')
1079  print('ok'); sys.stdout.flush()
1080 
1081 
1082  print('bitset1()...', end=''); sys.stdout.flush()
1083  for n in range(2048):
1084  for i in range(32):
1085  if bit(n, i):
1086  assert bitset1(n, i) == n, (n, bitset1(n, i))
1087  else:
1088  assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i), bitset1(n, i))
1089  assert bitset1(n, i) == bitset(n, i, True), (n, bitset(n, i, True), bitset1(n, i))
1090  if debug.assertspeed >= debug.ASSERT_NORMAL:
1091  for n in range(1, 2**32, 10000000):
1092  for i in range(32):
1093  if bit(n, i):
1094  assert bitset1(n, i) == n, (n, bitset1(n, i))
1095  else:
1096  assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i), bitset1(n, i))
1097  assert bitset1(n, i) == bitset(n, i, True), (n, bitset(n, i, True), bitset1(n, i))
1098  for n in range(2**32 - 2048, 2**32 + 5000):
1099  for i in range(32):
1100  if bit(n, i):
1101  assert bitset1(n, i) == n, (n, bitset1(n, i))
1102  else:
1103  assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i), bitset1(n, i))
1104  assert bitset1(n, i) == bitset(n, i, True), (n, bitset(n, i, True), bitset1(n, i))
1105  else:
1106  print(debug.assertspeed_str(), end='')
1107  print('ok'); sys.stdout.flush()
1108 
1109 
1110  print('coprime_is()...', end=''); sys.stdout.flush()
1111  assert not coprime_is(0, 0), coprime_is(0, 0)
1112  assert coprime_is(1, 0), coprime_is(1, 0)
1113  assert coprime_is(0, 1), coprime_is(0, 1)
1114  assert coprime_is(1, 1), coprime_is(1, 1)
1115  for m in range(50):
1116  if m != 1:
1117  assert not coprime_is(m, 0), (m, coprime_is(m, 0))
1118  assert coprime_is(m, 1), (m, coprime_is(m, 1))
1119  for p in nat32.PRIME_16_ARRAY[:100]:
1120  if m%p == 0:
1121  assert not coprime_is(m, p), (m, p, coprime_is(m, p))
1122  else:
1123  assert coprime_is(m, p), (m, p, coprime_is(m, p))
1124  assert coprime_is(m, m + 1), m
1125  for m in range(50):
1126  for n in range(10):
1127  assert coprime_is(m, n) == coprime_is(n, m), (m, n, coprime_is(m, n),
1128  fractions.gcd(n, m))
1129  for m in range(2**32 - 4096, 2**32 + 4096):
1130  assert coprime_is(m, m + 1), m
1131  print('ok'); sys.stdout.flush()
1132 
1133 
1134  print('distance_dominici()...', end='') ; sys.stdout.flush()
1135  assert distance_dominici(11, 12) == 4, distance_dominici(11, 12)
1136  for a in range(1, 50):
1137  assert distance_dominici(a, a) == 0, distance_dominici(a, a)
1138  for b in range(1, 50):
1139  assert distance_dominici(a, b) == distance_dominici(b, a), \
1140  (distance_dominici(a, b), distance_dominici(b, a))
1141  print('???', end='')
1142  print('ok') ; sys.stdout.flush()
1143 
1144 
1145  print('divisors()...', end=''); sys.stdout.flush()
1146  assert divisors(1) == [1], divisors(1)
1147  assert divisors(2) == [1, 2], divisors(2)
1148  assert divisors(3) == [1, 3], divisors(3)
1149  assert divisors(4) == [1, 2, 4], divisors(4)
1150  assert divisors(5) == [1, 5], divisors(5)
1151  assert divisors(6) == [1, 2, 3, 6], divisors(6)
1152  for n in range(1, 1000):
1153  for d in divisors(n):
1154  assert d > 0, (n, d)
1155  assert n%d == 0, (n, d, n%d)
1156  print('ok'); sys.stdout.flush()
1157 
1158 
1159  print('divisors_cond()...', end=''); sys.stdout.flush()
1160  f = lambda d: True
1161  assert divisors_cond(1, f) == [1], divisors_cond(1, f)
1162  assert divisors_cond(2, f) == [1, 2], divisors_cond(2, f)
1163  assert divisors_cond(3, f) == [1, 3], divisors_cond(3, f)
1164  assert divisors_cond(4, f) == [1, 2, 4], divisors_cond(4, f)
1165  assert divisors_cond(5, f) == [1, 5], divisors_cond(5, f)
1166  assert divisors_cond(6, f) == [1, 2, 3, 6], divisors_cond(6, f)
1167  f = lambda d: d%2 == 0
1168  assert divisors_cond(1, f) == [], divisors_cond(1, f)
1169  assert divisors_cond(2, f) == [2], divisors_cond(2, f)
1170  assert divisors_cond(3, f) == [], divisors_cond(3, f)
1171  assert divisors_cond(4, f) == [2, 4], divisors_cond(4, f)
1172  assert divisors_cond(5, f) == [], divisors_cond(5, f)
1173  assert divisors_cond(6, f) == [2, 6], divisors_cond(6, f)
1174  for n in range(1, 1000):
1175  assert divisors_cond(n, lambda d: False) == [], \
1176  (n, divisors_cond(n, lambda d: False))
1177  for d in divisors_cond(n, lambda d: True):
1178  assert d > 0, (n, d)
1179  assert n%d == 0, (n, d, n%d)
1180  for d in divisors_cond(n, lambda d: d%3 == 0):
1181  assert d > 0, (n, d)
1182  assert n%d == 0 and d%3 == 0, (n, d, n%d)
1183  for d in divisors_cond(n, lambda d: d%3 == 1):
1184  assert d > 0, (n, d)
1185  assert n%d == 0 and d%3 == 1, (n, d, n%d)
1186  for d in divisors_cond(n, lambda d: d%3 == 2):
1187  assert d > 0, (n, d)
1188  assert n%d == 0 and d%3 == 2, (n, d, n%d)
1189  assert len(divisors_cond(n, lambda d: d%3 == 0)
1190  + divisors_cond(n, lambda d: d%3 == 1)
1191  + divisors_cond(n, lambda d: d%3 == 2)) == len(divisors(n)), \
1192  (n, divisors_cond(n, lambda d: d%3 == 0),
1193  divisors_cond(n, lambda d: d%3 == 1),
1194  divisors_cond(n, lambda d: d%3 == 2),
1195  divisors(n))
1196  print('ok'); sys.stdout.flush()
1197 
1198 
1199  print('factorial()...', end=''); sys.stdout.flush()
1200  assert factorial(0) == 1, factorial(0)
1201  f = 1
1202  for n in range(1, 1000):
1203  f *= n
1204  assert factorial(n) == f, (n, factorial(n), f)
1205  assert factorial(n) == math.factorial(n), (n, factorial(n), math.factorial(n))
1206  print('ok'); sys.stdout.flush()
1207 
1208 
1209  print('falling_factorial_pow()...', end=''); sys.stdout.flush()
1210  assert falling_factorial_pow(0, 0) == 1, falling_factorial_pow(0, 0)
1211  for n in range(2000):
1212  assert falling_factorial_pow(n, 0) == 1, (n, falling_factorial_pow(n, 0))
1213  assert falling_factorial_pow(n, 1) == n, (n, falling_factorial_pow(n, 1))
1214  assert falling_factorial_pow(n, 2) == n*(n - 1), (n, falling_factorial_pow(n, 2))
1215  assert falling_factorial_pow(n, 3) == n*(n - 1)*(n - 2), \
1216  (n, falling_factorial_pow(n, 3))
1217  assert falling_factorial_pow(n, n + 1) == 0, (n, falling_factorial_pow(n, n+1))
1218  for k in range(20):
1219  assert falling_factorial_pow(k, k) == math.factorial(k), \
1220  (k, math.factorial(k), falling_factorial_pow(k, k))
1221  for k in range(20):
1222  assert falling_factorial_pow(k + 1, k) == math.factorial(k + 1), \
1223  (k, math.factorial(k + 1), falling_factorial_pow(2, k))
1224  for k in range(1, 100):
1225  assert falling_factorial_pow(0, k) == 0, (k, falling_factorial_pow(0, k))
1226  for k in range(1, 20):
1227  for n in range(k, min(100, int(pow(bit32.MERSENNE32, 1/k)) + 3)):
1228  assert falling_factorial_pow(n, k) == math.factorial(n)//math.factorial(n - k), \
1229  (n, k, math.factorial(n + k - 1)//math.factorial(n - 1),
1230  falling_factorial_pow(n, k))
1231  print('ok'); sys.stdout.flush()
1232 
1233 
1234  print('fibonacci()...', end=''); sys.stdout.flush()
1235  assert fibonacci(0) == 0, fibonacci(0)
1236  assert fibonacci(1) == 1, fibonacci(1)
1237  assert fibonacci(2) == 1, fibonacci(2)
1238  assert fibonacci(3) == 2, fibonacci(3)
1239  Fk_1 = 1
1240  Fk = 0
1241  for k in range(5000):
1242  assert fibonacci(k) == Fk, (k, fibonacci(k))
1243  Fk_1, Fk = Fk, Fk + Fk_1
1244  print('ok'); sys.stdout.flush()
1245 
1246 
1247  print('fibonacci2()...', end=''); sys.stdout.flush()
1248  assert fibonacci2(0) == (1, 0), fibonacci2(0)
1249  assert fibonacci2(1) == (0, 1), fibonacci2(1)
1250  assert fibonacci2(2) == (1, 1), fibonacci2(2)
1251  assert fibonacci2(3) == (1, 2), fibonacci2(3)
1252  Fk_1 = 1
1253  Fk = 0
1254  for k in range(5000):
1255  assert fibonacci2(k) == (Fk_1, Fk), (k, fibonacci2(k))
1256  Fk_1, Fk = Fk, Fk + Fk_1
1257  print('ok'); sys.stdout.flush()
1258 
1259 
1260  print('fibonacci_is()...', end=''); sys.stdout.flush()
1261  assert fibonacci_is(0)
1262  assert fibonacci_is(1)
1263  assert fibonacci_is(2)
1264  assert fibonacci_is(3)
1265  assert not fibonacci_is(4)
1266  for k in range(0, 1000):
1267  assert fibonacci_is(fibonacci(k)), (k, fibonacci(k))
1268  for n in range(32768):
1269  if fibonacci_is(n):
1270  assert 0 <= fibonacci_to_index(n) <= 47, (n, fibonacci_to_index(n))
1271  else:
1272  assert fibonacci_to_index(n) == None, (n, fibonacci_to_index(n))
1273  if debug.assertspeed >= debug.ASSERT_NORMAL:
1274  for n in range(1, 2**45, 1000000000):
1275  if fibonacci_is(n):
1276  assert 0 <= fibonacci_to_index(n) <= 47, (n, fibonacci_to_index(n))
1277  else:
1278  assert fibonacci_to_index(n) == None, (n, fibonacci_to_index(n))
1279  for n in range(2**32 - 32768, 2**32 + 32768):
1280  if fibonacci_is(n):
1281  assert 0 <= fibonacci_to_index(n) <= 47, (n, fibonacci_to_index(n))
1282  else:
1283  assert fibonacci_to_index(n) == None, (n, fibonacci_to_index(n))
1284  else:
1285  print(debug.assertspeed_str(), end='')
1286  print('ok'); sys.stdout.flush()
1287 
1288 
1289  print('fibonacci_to_index()...', end=''); sys.stdout.flush()
1290  assert fibonacci_to_index(0) == 0, fibonacci_to_index(0)
1291  assert fibonacci_to_index(1) == 1, fibonacci_to_index(1)
1292  assert fibonacci_to_index(2) == 3, fibonacci_to_index(2)
1293  assert fibonacci_to_index(3) == 4, fibonacci_to_index(3)
1294  assert fibonacci_to_index(4) == None, fibonacci_to_index(4)
1295  for k in range(3, 1000):
1296  assert fibonacci_to_index(fibonacci(k)) == k, \
1297  (k, fibonacci_to_index(fibonacci(k)))
1298  for n in range(32768):
1299  k = fibonacci_to_index(n)
1300  if k != None:
1301  assert fibonacci(k) == n, (n, k, fibonacci(k))
1302  if debug.assertspeed >= debug.ASSERT_NORMAL:
1303  for n in range(1, 2**45, 1000000000):
1304  k = fibonacci_to_index(n)
1305  if k != None:
1306  assert fibonacci(k) == n, (n, k, fibonacci(k))
1307  for n in range(2**32 - 32768, 2**32 + 32768):
1308  k = fibonacci_to_index(n)
1309  if k != None:
1310  assert fibonacci(k) == n, (n, k, fibonacci(k))
1311  else:
1312  print(debug.assertspeed_str(), end='')
1313  print('ok'); sys.stdout.flush()
1314 
1315 
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]:
1324  if m%p == 0:
1325  assert gcd(m, p) == p, (m, p, gcd(m, p))
1326  else:
1327  assert gcd(m, p) == 1, (m, p, gcd(m, p))
1328  for m in range(100):
1329  for n in range(50):
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()
1336 
1337 
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]:
1346  if m%p == 0:
1347  assert lcm(m, p) == m, (m, p, lcm(m, p))
1348  else:
1349  assert lcm(m, p) == m * p, (m, p, lcm(m, p))
1350  for m in range(100):
1351  for n in range(50):
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),
1356  m*n)
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()
1361 
1362 
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()
1371 
1372 
1373  print('lucas()...', end=''); sys.stdout.flush()
1374  Lk_1 = -1
1375  Lk = 2
1376  for k in range(5000):
1377  assert lucas(k) == Lk, (k, lucas(k))
1378  Lk_1, Lk = Lk, Lk + Lk_1
1379  print('ok'); sys.stdout.flush()
1380 
1381 
1382  print('lucas2()...', end=''); sys.stdout.flush()
1383  assert lucas2(0) == (-1, 2), lucas2(0)
1384  assert lucas2(1) == (2, 1), lucas2(1)
1385  assert lucas2(2) == (1, 3), lucas2(2)
1386  assert lucas2(3) == (3, 4), lucas2(3)
1387  Lk_1 = -1
1388  Lk = 2
1389  for k in range(5000):
1390  assert lucas2(k) == (Lk_1, Lk), (k, lucas2(k))
1391  Lk_1, Lk = Lk, Lk + Lk_1
1392  print('ok'); sys.stdout.flush()
1393 
1394 
1395  print('lucas_is()...', end=''); sys.stdout.flush()
1396  assert not lucas_is(0)
1397  assert lucas_is(1)
1398  assert lucas_is(2)
1399  assert lucas_is(3)
1400  assert lucas_is(4)
1401  assert not lucas_is(5)
1402  for k in range(0, 1000):
1403  assert lucas_is(lucas(k)), (k, lucas(k))
1404  for n in range(32768):
1405  if lucas_is(n):
1406  assert 0 <= lucas_to_index(n) <= 46, (n, lucas_to_index(n))
1407  else:
1408  assert lucas_to_index(n) == None, (n, lucas_to_index(n))
1409  if debug.assertspeed >= debug.ASSERT_NORMAL:
1410  for n in range(1, 2**45, 1000000000):
1411  if lucas_is(n):
1412  assert 0 <= lucas_to_index(n) <= 46, (n, lucas_to_index(n))
1413  else:
1414  assert lucas_to_index(n) == None, (n, lucas_to_index(n))
1415  for n in range(2**32 - 32768, 2**32 + 32768):
1416  if lucas_is(n):
1417  assert 0 <= lucas_to_index(n) <= 46, (n, lucas_to_index(n))
1418  else:
1419  assert lucas_to_index(n) == None, (n, lucas_to_index(n))
1420  else:
1421  print(debug.assertspeed_str(), end='')
1422  print('ok'); sys.stdout.flush()
1423 
1424 
1425  print('lucas_to_index()...', end=''); sys.stdout.flush()
1426  assert lucas_to_index(0) == None, lucas_to_index(0)
1427  assert lucas_to_index(1) == 1, lucas_to_index(1)
1428  assert lucas_to_index(2) == 0, lucas_to_index(2)
1429  assert lucas_to_index(3) == 2, lucas_to_index(3)
1430  assert lucas_to_index(4) == 3, lucas_to_index(4)
1431  assert lucas_to_index(5) == None, lucas_to_index(5)
1432  for k in range(3, 1000):
1433  assert lucas_to_index(lucas(k)) == k, (k, lucas_to_index(lucas(k)))
1434  for n in range(32768):
1435  k = lucas_to_index(n)
1436  if k != None:
1437  assert lucas(k) == n, (n, k, lucas(k))
1438  if debug.assertspeed >= debug.ASSERT_NORMAL:
1439  for n in range(1, 2**45, 1000000000):
1440  k = lucas_to_index(n)
1441  if k != None:
1442  assert lucas(k) == n, (n, k, lucas(k))
1443  for n in range(2**32 - 32768, 2**32 + 32768):
1444  k = lucas_to_index(n)
1445  if k != None:
1446  assert lucas(k) == n, (n, k, lucas(k))
1447  else:
1448  print(debug.assertspeed_str(), end='')
1449  print('ok'); sys.stdout.flush()
1450 
1451 
1452  print('mersenne()...', end=''); sys.stdout.flush()
1453  for k in range(50):
1454  assert mersenne(k) + 1 == 2**k, (k, mersenne(k))
1455  print('ok'); sys.stdout.flush()
1456 
1457 
1458  print('mersenne_to_index()...', end=''); sys.stdout.flush()
1459  assert mersenne_to_index(0) == 0, mersenne_to_index(0)
1460  assert mersenne_to_index(1) == 1, mersenne_to_index(1)
1461  assert mersenne_to_index(2) == None, mersenne_to_index(2)
1462  assert mersenne_to_index(3) == 2, mersenne_to_index(3)
1463  assert mersenne_to_index(4) == None, mersenne_to_index(4)
1464  for k in range(3, 1000):
1465  assert mersenne_to_index(mersenne(k)) == k, \
1466  (k, mersenne_to_index(mersenne(k)))
1467  for n in range(32768):
1468  k = mersenne_to_index(n)
1469  if k != None:
1470  assert mersenne(k) == n, (n, k, mersenne(k))
1471  if debug.assertspeed >= debug.ASSERT_NORMAL:
1472  for n in range(1, 2**45, 1000000000):
1473  k = mersenne_to_index(n)
1474  if k != None:
1475  assert mersenne(k) == n, (n, k, mersenne(k))
1476  for n in range(2**32 - 32768, 2**32 + 32768):
1477  k = mersenne_to_index(n)
1478  if k != None:
1479  assert mersenne(k) == n, (n, k, mersenne(k))
1480  else:
1481  print(debug.assertspeed_str(), end='')
1482  print('ok'); sys.stdout.flush()
1483 
1484 
1485  print('mertens()...', end=''); sys.stdout.flush()
1486  assert mertens(0) == 0, mertens(0)
1487  assert mertens(1) == 1, mertens(1)
1488  assert mertens(2) == 0, mertens(2)
1489  assert mertens(3) == -1, mertens(3)
1490  assert mertens(4) == -1, mertens(4)
1491  assert mertens(5) == -2, mertens(5)
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()
1496 
1497 
1498  print('nbbits0()...', end=''); sys.stdout.flush()
1499  assert nbbits0(0) == 0
1500  for n in range(1, 4096):
1501  nb = 0
1502  for i in range(rscan1(n)):
1503  if not bit(n, i):
1504  nb += 1
1505  assert nbbits0(n) == nb, (n, nb, nbbits0(n), bin(n))
1506  if debug.assertspeed >= debug.ASSERT_NORMAL:
1507  for n in range(1, 2**34, 10000000):
1508  nb = 0
1509  for i in range(rscan1(n)):
1510  if not bit(n, i):
1511  nb += 1
1512  assert nbbits0(n) == nb, (n, nb, nbbits0(n), bin(n))
1513  for n in range(2**32 - 4096, 2**32 + 4096):
1514  nb = 0
1515  for i in range(rscan1(n)):
1516  if not bit(n, i):
1517  nb += 1
1518  assert nbbits0(n) == nb, (n, nb, nbbits0(n), bin(n))
1519  else:
1520  print(debug.assertspeed_str(), end='')
1521  print('ok'); sys.stdout.flush()
1522 
1523 
1524  print('nbbits1()...', end=''); sys.stdout.flush()
1525  assert nbbits1(0) == 0
1526  for n in range(4096):
1527  nb = 0
1528  for i in range(40):
1529  if bit(n, i):
1530  nb += 1
1531  assert nbbits1(n) == nb, (n, nb, nbbits1(n), bin(n))
1532  if debug.assertspeed >= debug.ASSERT_NORMAL:
1533  for n in range(1, 2**34, 10000000):
1534  nb = 0
1535  for i in range(40):
1536  if bit(n, i):
1537  nb += 1
1538  assert nbbits1(n) == nb, (n, nb, nbbits1(n), bin(n))
1539  for n in range(2**32 - 4096, 2**32 + 4096):
1540  nb = 0
1541  for i in range(40):
1542  if bit(n, i):
1543  nb += 1
1544  assert nbbits1(n) == nb, (n, nb, nbbits1(n), bin(n))
1545  else:
1546  print(debug.assertspeed_str(), end='')
1547  print('ok'); sys.stdout.flush()
1548 
1549 
1550  print('nbbits()...', end=''); sys.stdout.flush()
1551  assert nbbits(0) == 0
1552  assert nbbits(1) == 1
1553  assert nbbits(2) == 2
1554  assert nbbits(3) == 2
1555  assert nbbits(4) == 3
1556  for n in range(1, 16384):
1557  l = nbbits(n)
1558  assert (1<<(l - 1)) <= n, (n, l)
1559  assert n < (1<<l), (n, l)
1560  assert nbbits(n) == len(bin(n)), (n, nbbits(n), bin(n))
1561  if debug.assertspeed >= debug.ASSERT_NORMAL:
1562  for n in range(1, 2**40, 1000000000):
1563  l = nbbits(n)
1564  assert (1<<(l - 1)) <= n, (n, l)
1565  assert n < (1<<l), (n, l)
1566  assert nbbits(n) == len(bin(n)), (n, nbbits(n), bin(n))
1567  for n in range(2**32 - 16384, 2**32 + 32768):
1568  l = nbbits(n)
1569  assert (1<<(l - 1)) <= n, (n, l)
1570  assert n < (1<<l), (n, l)
1571  assert nbbits(n) == len(bin(n)), (n, nbbits(n), bin(n))
1572  else:
1573  print(debug.assertspeed_str(), end='')
1574  print('ok'); sys.stdout.flush()
1575 
1576 
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))
1583  if m > 0:
1584  assert nfp(m, m) == 1, (m, nfp(m, m))
1585  for p in nat32.PRIME_16_ARRAY[:100]:
1586  if m%p == 0:
1587  assert nfp(m, p) == m // p, (m, p, nfp(m, p))
1588  else:
1589  assert nfp(m, p) == m * p, (m, p, nfp(m, p))
1590  for m in range(100):
1591  for n in range(50):
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,
1596  m)**2),
1597  m*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()
1602 
1603 
1604  print('nontotatives()...', end=''); sys.stdout.flush()
1605  assert nontotatives(0) == [], nontotatives(0)
1606  assert nontotatives(1) == [], nontotatives(1)
1607  assert nontotatives(2) == [2], nontotatives(2)
1608  assert nontotatives(3) == [3], nontotatives(3)
1609  assert nontotatives(4) == [2, 4], nontotatives(4)
1610  assert nontotatives(5) == [5], nontotatives(5)
1611  assert nontotatives(6) == [2, 3, 4, 6], nontotatives(6)
1612  assert nontotatives(24) == [2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24], \
1613  nontotatives(24)
1614  for n in range(500 if debug.assertspeed >= debug.ASSERT_NORMAL
1615  else 50):
1616  for d in nontotatives(n):
1617  assert d <= n, (n, d)
1618  assert not coprime_is(n, d), (n, d)
1619  for p in nat32.PRIME_16_ARRAY[:100]:
1620  assert nontotatives(p) == [p], (p, nontotatives(p))
1621  if debug.assertspeed < debug.ASSERT_NORMAL:
1622  print(debug.assertspeed_str(), end='')
1623  print('ok'); sys.stdout.flush()
1624 
1625 
1626  print('nontotient()...', end=''); sys.stdout.flush()
1627  assert nontotient(0) == 0, nontotient(0)
1628  assert nontotient(1) == 0, nontotient(1)
1629  assert nontotient(2) == 1, nontotient(2)
1630  assert nontotient(3) == 1, nontotient(3)
1631  assert nontotient(4) == 2, nontotient(4)
1632  assert nontotient(5) == 1, nontotient(5)
1633  assert nontotient(6) == 4, nontotient(6)
1634  assert nontotient(24) == 16, nontotient(24)
1635  for n in range(500 if debug.assertspeed >= debug.ASSERT_NORMAL
1636  else 50):
1637  assert nontotient(n) == len(nontotatives(n)), \
1638  (n, nontotient(n), len(nontotatives(n)), nontotatives(n))
1639  for p in nat32.PRIME_16_ARRAY[:100]:
1640  assert nontotient(p) == 1, (p, nontotient(p))
1641  if debug.assertspeed < debug.ASSERT_NORMAL:
1642  print(debug.assertspeed_str(), end='')
1643  print('ok'); sys.stdout.flush()
1644 
1645 
1646  print('polygonal()...', end=''); sys.stdout.flush()
1647  for n in range(1000):
1648  assert polygonal(2, n) == n, (n, polygonal(2, n))
1649  assert polygonal(3, n) == n*(n + 1)//2, (n, polygonal(3, n), n*(n + 1)//2)
1650  assert polygonal(4, n) == n**2, (n, polygonal(4, n), n**2)
1651  assert polygonal(5, n) == n*(3*n - 1)//2, (n, polygonal(5, n), n*(3*n - 1)//2)
1652  for k in range(2, 1000):
1653  assert polygonal(k, 0) == 0, (k, polygonal(k, 0))
1654  assert polygonal(k, 1) == 1, (k, polygonal(k, 1))
1655  assert polygonal(k, 2) == k, (k, polygonal(k, 2))
1656  assert polygonal(k, 3) == 3*(k - 1), (k, polygonal(k, 3), 3*(k - 1))
1657  print('ok'); sys.stdout.flush()
1658 
1659 
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()
1664 
1665 
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()
1670 
1671 
1672  print('prime_is()...', end=''); sys.stdout.flush()
1673  assert not prime_is(0)
1674  assert not prime_is(1)
1675  assert prime_is(2)
1676  assert prime_is(3)
1677  assert not prime_is(4)
1678  assert prime_is(5)
1679  assert prime_is(2**32 - 5)
1680  for n in range(2**32 - 4, 2**32 + 15):
1681  assert not prime_is(n), n
1682  print('ok'); sys.stdout.flush()
1683 
1684 
1685  print('pyramidal()...', end=''); sys.stdout.flush()
1686  for n in range(1000):
1687  assert pyramidal(2, n) == n*(n + 1)//2, (n, pyramidal(2, n), n*(n + 1)//2)
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)
1692  assert pyramidal(5, n) == n**2 * (n + 1)//2, (n, pyramidal(5, n), n**2 * (n + 1)//2)
1693  for k in range(2, 1000):
1694  assert pyramidal(k, 0) == 0, (k, pyramidal(k, 0))
1695  assert pyramidal(k, 1) == 1, (k, pyramidal(k, 1))
1696  assert pyramidal(k, 2) == k + 1, (k, pyramidal(k, 2), k + 1)
1697  assert pyramidal(k, 3) == 4*k - 2, (k, pyramidal(k, 3), 4*k - 2)
1698  print('ok'); sys.stdout.flush()
1699 
1700 
1701  print('rising_factorial_pow()...', end=''); sys.stdout.flush()
1702  assert rising_factorial_pow(0, 0) == 1, rising_factorial_pow(0, 0)
1703  for n in range(2000):
1704  assert rising_factorial_pow(n, 0) == 1, (n, rising_factorial_pow(n, 0))
1705  assert rising_factorial_pow(n, 1) == n, (n, rising_factorial_pow(n, 1))
1706  assert rising_factorial_pow(n, 2) == n*(n + 1), (n, rising_factorial_pow(n, 2))
1707  assert rising_factorial_pow(n, 3) == n*(n + 1)*(n + 2), (n, rising_factorial_pow(n, 3))
1708  for k in range(20):
1709  assert rising_factorial_pow(1, k) == math.factorial(k), \
1710  (k, math.factorial(k), rising_factorial_pow(1, k))
1711  for k in range(20):
1712  assert rising_factorial_pow(2, k) == math.factorial(k + 1), \
1713  (k, math.factorial(k+1), rising_factorial_pow(2, k))
1714  for k in range(1, 20):
1715  assert rising_factorial_pow(0, k) == 0, (k, rising_factorial_pow(0, k))
1716  for n in range(1, min(100, int(pow(bit32.MERSENNE32, 1/k) - k + 4))):
1717  assert rising_factorial_pow(n, k) \
1718  == math.factorial(n + k - 1)//math.factorial(n - 1), \
1719  (n, math.factorial(n + k - 1)//math.factorial(n - 1),
1720  rising_factorial_pow(n, k))
1721  print('ok'); sys.stdout.flush()
1722 
1723 
1724  print('rscan1()...', end=''); sys.stdout.flush()
1725  assert rscan1(0) == None
1726  assert rscan1(1) == 0
1727  assert rscan1(2) == 1
1728  assert rscan1(3) == 1
1729  assert rscan1(4) == 2
1730  assert rscan1(5) == 2
1731  assert rscan1(bit32.MERSENNE32) == 31
1732  for n in range(1, 4096):
1733  assert rscan1(n) == len(bin(n)) - 1, (n, rscan1(n), len(bin(n)))
1734  if debug.assertspeed >= debug.ASSERT_NORMAL:
1735  for n in range(1, 2**34, 10000000):
1736  assert rscan1(n) == len(bin(n)) - 1, (n, rscan1(n), len(bin(n)))
1737  for n in range(2**32 - 4096, 2**32 + 4096):
1738  assert rscan1(n) == len(bin(n)) - 1, (n, rscan1(n), len(bin(n)))
1739  else:
1740  print(debug.assertspeed_str(), end='')
1741  print('ok'); sys.stdout.flush()
1742 
1743 
1744  print('scan0()...', end=''); sys.stdout.flush()
1745  for i in range(33):
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):
1749  if n&1 == 0:
1750  assert scan0(n) == 0, (n, scan0(n))
1751  else:
1752  assert scan0(n) == scan0(n>>1) + 1, (n, scan0(n), scan0(n>>1))
1753  if debug.assertspeed >= debug.ASSERT_NORMAL:
1754  for n in range(1, 2**45, 1000000000):
1755  if n&1 == 0:
1756  assert scan0(n) == 0, (n, scan0(n))
1757  else:
1758  assert scan0(n) == scan0(n>>1) + 1, (n, scan0(n), scan0(n>>1))
1759  for n in range(2**32 - 32768, 2**32 + 32768):
1760  if n&1 == 0:
1761  assert scan0(n) == 0, (n, scan0(n))
1762  else:
1763  assert scan0(n) == scan0(n>>1) + 1, (n, scan0(n), scan0(n>>1))
1764  else:
1765  print(debug.assertspeed_str(), end='')
1766  print('ok'); sys.stdout.flush()
1767 
1768 
1769  print('scan1()...', end=''); sys.stdout.flush()
1770  assert scan1(0) == None, scan1(0)
1771  for i in range(32):
1772  assert scan1(2**i) == i, (i, scan1(2**i))
1773  for n in range(1, 32768):
1774  if n&1 == 1:
1775  assert scan1(n) == 0, (n, scan1(n))
1776  else:
1777  assert scan1(n) == scan1(n>>1) + 1, (n, scan1(n), scan1(n>>1))
1778  if debug.assertspeed >= debug.ASSERT_NORMAL:
1779  for n in range(1, 2**45, 1000000000):
1780  if n&1 == 1:
1781  assert scan1(n) == 0, (n, scan1(n))
1782  else:
1783  assert scan1(n) == scan1(n>>1) + 1, (n, scan1(n), scan1(n>>1))
1784  for n in range(2**32 - 32768, 2**32 + 32768):
1785  if n&1 == 1:
1786  assert scan1(n) == 0, (n, scan1(n))
1787  else:
1788  assert scan1(n) == scan1(n>>1) + 1, (n, scan1(n), scan1(n>>1))
1789  else:
1790  print(debug.assertspeed_str(), end='')
1791  print('ok'); sys.stdout.flush()
1792 
1793 
1794  print('totatives()...', end=''); sys.stdout.flush()
1795  assert totatives(0) == [], totatives(0)
1796  assert totatives(1) == [1], totatives(1)
1797  assert totatives(2) == [1], totatives(2)
1798  assert totatives(3) == [1, 2], totatives(3)
1799  assert totatives(4) == [1, 3], totatives(4)
1800  assert totatives(5) == [1, 2, 3, 4], totatives(5)
1801  assert totatives(6) == [1, 5], totatives(6)
1802  assert totatives(24) == [1, 5, 7, 11, 13, 17, 19, 23], totatives(24)
1803  for n in range(500 if debug.assertspeed >= debug.ASSERT_NORMAL
1804  else 50):
1805  t = totatives(n)
1806  for d in t:
1807  assert d <= n, (n, d)
1808  assert coprime_is(n, d), (n, d)
1809  tnt = t + nontotatives(n)
1810  tnt.sort()
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]:
1814  assert totatives(p) == [x for x in range(1, p)], (p, totatives(p))
1815  if debug.assertspeed < debug.ASSERT_NORMAL:
1816  print(debug.assertspeed_str(), end='')
1817  print('ok'); sys.stdout.flush()
1818 
1819 
1820  print('totient()...', end=''); sys.stdout.flush()
1821  assert totient(0) == 1, totient(0)
1822  assert totient(1) == 1, totient(1)
1823  assert totient(2) == 1, totient(2)
1824  assert totient(3) == 2, totient(3)
1825  assert totient(4) == 2, totient(4)
1826  assert totient(5) == 4, totient(5)
1827  assert totient(6) == 2, totient(6)
1828  assert totient(24) == 8, totient(24)
1829  for n in range(1, 500 if debug.assertspeed >= debug.ASSERT_NORMAL
1830  else 50):
1831  assert totient(n) == len(totatives(n)), (n, totient(n), len(totatives(n)), totatives(n))
1832  assert totient(n) + nontotient(n) == n, (n, totient(n), nontotient(n))
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()
1839 
1840 
1841  print('unitarydivisors()...', end=''); sys.stdout.flush()
1842  assert unitarydivisors(1) == [1], unitarydivisors(1)
1843  assert unitarydivisors(2) == [1, 2], unitarydivisors(2)
1844  assert unitarydivisors(3) == [1, 3], unitarydivisors(3)
1845  assert unitarydivisors(4) == [1, 4], unitarydivisors(4)
1846  assert unitarydivisors(5) == [1, 5], unitarydivisors(5)
1847  assert unitarydivisors(6) == [1, 2, 3, 6], unitarydivisors(6)
1848  assert unitarydivisors(12) == [1, 3, 4, 12], unitarydivisors(12)
1849  for n in range(1, 1000):
1850  divs = unitarydivisors(n)
1851  for d in divs:
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)
1855  assert divs == \
1856  divisors_cond(n, lambda d: nat32.coprime_is(n//d, d)), \
1857  (n, divs,
1858  divisors_cond(n, lambda d: nat32.coprime_is(n//d, d)))
1859  print('ok'); sys.stdout.flush()
1860  debug.test_end()
1861 
1862  main_test()
1863 ##\endcond MAINTEST