/
test_Goulib_math2.py
1329 lines (961 loc) · 37.5 KB
/
test_Goulib_math2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf8
from nose.tools import assert_equal, assert_greater
from nose import SkipTest
# lines above are inserted automatically by pythoscope. Line below overrides them
from Goulib.tests import *
from Goulib.math2 import *
import Goulib.itertools2
class TestLongint:
def test_longint(self):
assert_equal(longint(1,3),1000)
assert_equal(longint(10,2),1000)
assert_equal(longint(0.1,4),1000)
assert_equal(longint(0.123,4),1230)
class TestSign:
def test_sign(self):
assert_equal(sign(0.0001), 1)
assert_equal(sign(-0.0001), -1)
assert_equal(sign(-0.0000), 0)
class TestCmp:
def test_cmp(self):
assert_equal(cmp(0.0002, 0.0001), 1)
assert_equal(cmp(0.0001, 0.0002), -1)
assert_equal(cmp(0.0000, 0.0000), 0)
class TestMul(unittest.TestCase):
def test_mul(self):
assert_equal(mul(range(1, 10)), 362880)
class TestRint(unittest.TestCase):
def test_rint(self):
# https://docs.python.org/3.4/library/functions.html#round
assert_equal(rint(0.5), 0, places=None)
assert_equal(rint(-0.5), -0, places=None)
assert_equal(rint(0.50001), 1, places=None)
assert_equal(rint(-0.50001), -1, places=None)
class TestQuad:
def test_quad(self):
assert_equal(quad(1, 3, 2), (-1, -2))
assert_raises(ValueError, quad, 1, 2, 3) # complex results
# complex results
assert_equal(sum(quad(1, 2, 3, allow_complex=True)), -2)
class TestEqual:
def test_isclose(self):
a = 1E6
d = 0.99e-3
assert_true(isclose(a, a+d))
assert_false(isclose(a, a+2*d))
def test_allclose(self):
a = 1E6
d = 0.99e-3
assert_true(allclose([a, a-d], [a+d, a]))
assert_false(allclose([a, a+2*d], [a, a]))
assert_false(allclose([a, a+2*d], [a, nan]))
assert_false(allclose([a], [a, nan]))
def test_equal(self):
# assert_equal(expected, equal(a, b, epsilon))
raise SkipTest
class TestLcm:
def test_lcm(self):
assert_equal(lcm(101, -3), -303)
assert_equal(lcm(4, 6), 12)
assert_equal(lcm(3, 4, 6), 12)
class TestGcd:
def test_gcd(self):
assert_equal(gcd(54, 24), 6)
assert_equal(gcd(24, 54), 6)
assert_equal(gcd(68, 14, 9, 36, 126), 1)
assert_equal(gcd(7, 14, 35, 7000), 7)
assert_equal(gcd(1548), 1548)
class TestCoprime:
def test_coprime(self):
assert_true(coprime(68, 14, 9, 36, 126))
assert_false(coprime(7, 14, 35, 7000))
class TestAccsum:
def test_accsum(self):
s = list(accsum(range(10)))
assert_equal(s[-1], 45)
class TestTranspose:
def test_transpose(self):
v1 = list(range(3))
v2 = list(accsum(v1))
m1 = [v1, v2, vecsub(v2, v1)]
assert_equal(transpose(m1), [(0, 0, 0), (1, 1, 0), (2, 3, 1)])
class TestMaximum:
def test_maximum(self):
m = [(1, 2, 3), (1, -2, 0), (4, 0, 0)]
assert_equal(maximum(m), [4, 2, 3])
class TestMinimum:
def test_minimum(self):
m = [(1, 2, 3), (1, -2, 0), (4, 0, 0)]
assert_equal(minimum(m), [1, -2, 0])
class TestDotVv:
def test_dot_vv(self):
v1 = list(range(3))
v2 = list(accsum(v1))
assert_equal(dot_vv(v1, v2), 7)
class TestDotMv:
def test_dot_mv(self):
v1 = list(range(3))
v2 = list(accsum(v1))
m1 = [v1, v2, vecsub(v2, v1)]
assert_equal(dot_mv(m1, v1), [5, 7, 2])
class TestDotMm:
def test_dot_mm(self):
v1 = list(range(3))
v2 = list(accsum(v1))
m1 = [v1, v2, vecsub(v2, v1)]
m2 = transpose(m1)
assert_equal(dot_mm(m1, m2), [[5, 7, 2], [7, 10, 3], [2, 3, 1]])
class TestDot:
def test_dot(self):
v1 = list(range(3))
v2 = list(accsum(v1))
assert_equal(dot(v1, v2), 7)
m1 = [v1, v2, vecsub(v2, v1)]
assert_equal(dot(m1, v1), [5, 7, 2])
m2 = transpose(m1)
assert_equal(dot(m1, m2), [[5, 7, 2], [7, 10, 3], [2, 3, 1]])
v1 = [715827883, 715827882]
# fails with numpy.dot !
assert_equal(dot(v1, v1), 1024819114728867613)
class TestVecadd:
def test_vecadd(self):
v1 = list(range(4))
v2 = list(accsum(v1))
assert_equal(vecadd(v1, v2), [0, 2, 5, 9])
v1 = v1[1:]
assert_equal(vecadd(v1, v2), [1, 3, 6, 6])
assert_equal(vecadd(v1, v2, -1), [1, 3, 6, 5])
class TestVecsub:
def test_vecsub(self):
v1 = list(range(4))
v2 = tuple(accsum(v1))
assert_equal(vecsub(v1, v2), [0, 0, -1, -3])
v1 = v1[1:]
assert_equal(vecsub(v1, v2), [1, 1, 0, -6])
assert_equal(vecsub(v1, v2, -1), [1, 1, 0, -7])
class TestVecmul:
def test_vecmul(self):
v1 = list(range(4))
v2 = list(accsum(v1))
assert_equal(vecmul(v1, v2), [0, 1, 6, 18])
assert_equal(vecmul(v1, 2), [0, 2, 4, 6])
assert_equal(vecmul(2, v1), [0, 2, 4, 6])
class TestVecdiv:
def test_vecdiv(self):
v1 = list(range(5))[1:]
v2 = list(accsum(v1))
assert_equal(vecdiv(v1, v2), [1, 2./3, 1./2, 2./5])
assert_equal(vecdiv(v1, 2), [1./2, 2./2, 3./2, 4./2])
class TestVeccompare:
def test_veccompare(self):
v1 = list(range(5))
v2 = list(accsum(v1))
v2[-1] = 2 # force to test ai>bi
assert_equal(veccompare(v1, v2), [2, 2, 1])
class TestFibonacciGen:
def test_fibonacci_gen(self):
# also tested in test_oeis
# https://projecteuler.net/problem=2
from itertools import takewhile
def problem2(n):
"""Find the sum of all the even-valued terms in the Fibonacci < 4 million."""
even_fibonacci = (x for x in fibonacci_gen() if x % 2 == 0)
l = list(takewhile(lambda x: x < n, even_fibonacci))
return sum(l)
assert_equal(problem2(10), 10)
assert_equal(problem2(100), 44)
assert_equal(problem2(4E6), 4613732)
class TestFibonacci:
def test_fibonacci(self):
# checks that fibonacci and fibonacci_gen give the same results
f = [fibonacci(i) for i in range(10)]
assert_equal(f, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
assert_equal(f, itertools2.take(10, fibonacci_gen()))
# http://controlfd.com/2016/07/05/using-floats-in-python.html
assert_equal(fibonacci(78), 8944394323791464)
f50 = fibonacci(50)
f51 = fibonacci(51)
phi = (1+sqrt(5))/2
assert_equal(f51/f50, phi)
# mod 1000000007 has the effect of using int32 only
assert_equal(fibonacci(int(1E19), 1000000007), 647754067)
assert_equal(fibonacci(int(1E19), 10), 5)
class TestIsFibonacci:
def test_is_fibonacci(self):
assert_true(is_fibonacci(0))
assert_true(is_fibonacci(1))
assert_true(is_fibonacci(2))
assert_true(is_fibonacci(3))
assert_false(is_fibonacci(4))
assert_true(is_fibonacci(8944394323791464))
assert_false(is_fibonacci(8944394323791464+1))
class TestPisanoPeriod:
def test_pisano_period(self):
assert_equal(pisano_period(3), 8)
assert_equal(pisano_period(10), 60)
class TestPisanoCycle:
def test_pisano_cycle(self):
assert_equal(pisano_cycle(3), [0, 1, 1, 2, 0, 2, 2, 1]) # A082115
class TestIsInteger:
def test_is_integer(self):
assert_true(is_integer(1+1e-6, 1e-6))
assert_false(is_integer(1+2e-6, 1e-6))
class TestIntOrFloat:
def test_int_or_float(self):
# comparing values would always pass, so we must compare types
assert_equal(type(int_or_float(1.0)), int)
assert_equal(type(int_or_float(1.0+eps)), float)
assert_equal(type(int_or_float(1+1e-6, 1e-6)), int)
assert_equal(type(int_or_float(1+2e-6, 1e-6)), float)
class TestSieve:
def test_sieve(self):
last = sieve(10000)[-1] # more than _sieve for coverage
assert_equal(last, 9973)
class TestPrimes:
def test_primes(self):
last = primes(1001)[999] # more than _primes for coverage
assert_equal(last, 7919)
class TestNextprime:
def test_nextprime(self):
assert_equal(nextprime(0), 2)
assert_equal(nextprime(1), 2)
assert_equal(nextprime(2), 3)
assert_equal(nextprime(1548), 1549)
class TestPrevprime:
def test_prevprime(self):
assert_equal(prevprime(1), None)
assert_equal(prevprime(2), None)
assert_equal(prevprime(3), 2)
assert_equal(prevprime(1548), 1543)
class TestPrimesGen:
def test_primes_gen(self):
from itertools import islice
a = list(islice(primes_gen(), 10))
assert_equal(a, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29])
a = list(islice(primes_gen(29), 10))
assert_equal(a, [29, 31, 37, 41, 43, 47, 53, 59, 61, 67])
a = list(islice(primes_gen(67, 29), 10))
assert_equal(a, reversed([29, 31, 37, 41, 43, 47, 53, 59, 61, 67]))
a = list(primes_gen(901, 1000))
assert_equal(a, [907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997])
class TestStrBase:
def test_str_base(self):
assert_equal(str_base(2014), "2014")
assert_equal(str_base(-2014), "-2014")
assert_equal(str_base(-0), "0")
assert_equal(str_base(2014, 2), "11111011110")
assert_equal(str_base(65535, 16), "ffff")
assert_raises(ValueError, str_base, 0, 1)
# http://www.drgoulu.com/2011/09/25/comment-comptent-les-extraterrestres
shadok = ['GA', 'BU', 'ZO', 'MEU']
assert_raises(ValueError, str_base, 0, 10, shadok)
assert_equal(str_base(41, 4, shadok), "ZOZOBU")
assert_equal(str_base(1681, 4, shadok), "BUZOZOBUGABU")
class TestDigitsGen:
def test_digits_gen(self):
pass # used below
class TestDigits:
def test_digits(self):
assert_equal(digits(1234), [1, 2, 3, 4])
assert_equal(digits(1234, rev=True), [4, 3, 2, 1])
assert_equal(digits(2014, 2), [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0])
class TestDigsum:
def test_digsum(self):
assert_equal(digsum(1234567890), 45)
assert_equal(digsum(255, base=2), 8) # sum of ones in binary rep
assert_equal(digsum(255, base=16), 30) # $FF in hex
assert_equal(digsum(1234567890, 2), sum_of_squares(9))
assert_equal(digsum(548834, 6), 548834) # narcissic number
assert_equal(digsum(3435, lambda x: x**x), 3435) # Munchausen number
class TestIntegerExponent:
def test_integer_exponent(self):
assert_equal(integer_exponent(1000), 3)
assert_equal(integer_exponent(1024, 2), 10)
# http://thales.math.uqam.ca/~rowland/packages/BinomialCoefficients/HTMLLinks/index_3.html
assert_equal(integer_exponent(binomial(1000, 373), 2), 6)
class TestPowerTower:
def test_power_tower(self):
assert_equal(power_tower([3, 2, 2, 2]), 43046721)
class TestCarries:
def test_carries(self):
assert_equal(carries(127, 123), 1)
assert_equal(carries(127, 173), 2)
assert_equal(carries(1, 999), 3)
assert_equal(carries(999, 1), 3)
assert_equal(carries(127, 127, 2), 7)
class TestNumFromDigits:
def test_num_from_digits(self):
assert_equal(num_from_digits('1234'), 1234)
assert_equal(num_from_digits('11111011110', 2), 2014)
assert_equal(num_from_digits([1, 2, 3, 4]), 1234)
assert_equal(num_from_digits(
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0], 2), 2014)
class TestNumberOfDigits:
def test_number_of_digits(self):
assert_equal(number_of_digits(0), 1)
assert_equal(number_of_digits(-1), 1)
assert_equal(number_of_digits(999), 3)
assert_equal(number_of_digits(1000), 4)
assert_equal(number_of_digits(1234), 4)
assert_equal(number_of_digits(9999), 4)
assert_equal(number_of_digits(2014, 2), 11)
assert_equal(number_of_digits(65535, 16), 4)
class TestIsPalindromic:
def test_is_palindromic(self):
assert_true(is_palindromic(4352534))
assert_true(is_palindromic(17, 2))
assert_equal(
sum(filter(is_palindromic, range(34*303, 100000, 303))), 394203)
class TestIsLychrel:
def test_is_lychrel(self):
assert_true(is_lychrel(196))
assert_true(is_lychrel(4994))
class TestIsPrime:
def test_is_prime(self):
assert_false(is_prime(0))
assert_false(is_prime(1))
assert_true(is_prime(2))
# https://oeis.org/A014233
pseudoprimes = [2047, 1373653, 25326001, 3215031751, 2152302898747, 3474749660383,
341550071728321, 3825123056546413051, 318665857834031151167461, 3317044064679887385961981]
for pp in pseudoprimes:
assert_false(is_prime(pp))
assert_true(is_prime(201420142013))
assert_true(
is_prime(4547337172376300111955330758342147474062293202868155909489))
assert_false(
is_prime(4547337172376300111955330758342147474062293202868155909393))
assert_equal(
[x for x in range(901, 1000) if is_prime(x)],
[907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
)
assert_true(is_prime(643808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153))
assert_false(is_prime(743808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153))
class TestFactorEcm:
def test_factor_ecm(self):
for _ in range(10):
size = 32
a = random_prime(size)
b = random_prime(size)
c = factor_ecm(a*b)
assert_true(c in (a, b))
class TestPrimeFactors:
def test_prime_factors(self):
assert_equal(prime_factors(2014), [2, 19, 53])
assert_equal(prime_factors(2048), [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
class TestFactorize:
def test_factorize(self):
d = list(factorize(1))
assert_equal(factorize(1), [(1, 1)])
d = list(factorize(2014))
assert_equal(d, [(2, 1), (19, 1), (53, 1)])
d = list(factorize(2048))
assert_equal(factorize(2048), [(2, 11)])
class TestDivisors:
def test_divisors(self):
d = list(divisors(1))
assert_equal(d, [1])
d = list(divisors(2014))
assert_equal(d, [1, 53, 19, 1007, 2, 106, 38, 2014])
d = list(divisors(2**3*3**2))
assert_equal(sorted(d), sorted(
[1, 2, 4, 8, 3, 9, 6, 12, 24, 18, 36, 72]))
class TestProperDivisors:
def test_proper_divisors(self):
d = list(proper_divisors(2014))
assert_equal(d, [1, 53, 19, 1007, 2, 106, 38])
class TestTriangle:
def test_triangle(self):
assert_equal(triangle(10), 55)
class TestIsTriangle:
def test_is_triangle(self):
assert_true(is_triangle(55))
assert_false(is_triangle(54))
class TestPentagonal:
def test_pentagonal(self):
assert_equal(pentagonal(10), 145)
class TestIsPentagonal:
def test_is_pentagonal(self):
assert_true(is_pentagonal(145))
assert_false(is_pentagonal(146))
class TestHexagonal:
def test_hexagonal(self):
assert_equal(hexagonal(10), 190)
class TestGetCardinalName:
def test_get_cardinal_name(self):
assert_equal(get_cardinal_name(123456),
'one hundred and twenty-three thousand four hundred and fifty-six'
)
assert_equal(get_cardinal_name(1234567890),
'one billion two hundred and thirty-four million five hundred and sixty-seven thousand eight hundred and ninety'
)
class TestIsPerfect:
def test_is_perfect(self):
assert_equal(is_perfect(496), 0) # perfect
assert_equal(is_perfect(54), 1) # abundant
assert_equal(is_perfect(2), -1) # deficient
# Millenium 4, page 326
assert_equal(is_perfect(2305843008139952128), 0)
assert_equal(is_perfect(2658455991569831744654692615953842176), 0)
class TestIsPandigital:
def test_is_pandigital(self):
# https://en.wikipedia.org/wiki/Pandigital_number
assert_true(is_pandigital(9786530421))
assert_true(is_pandigital(1223334444555567890))
assert_true(is_pandigital(10, 2))
assert_true(is_pandigital(0x1023456789ABCDEF, 16))
class TestSetsDist:
def test_sets_dist(self):
a = set(list('hello'))
b = set(list('world'))
assert_equal(sets_dist(a, b), 3.1622776601683795)
class TestHamming:
def test_hamming(self):
a = "10011100"
b = "00011010"
assert_equal(hamming(a, b), 3)
class TestSetsLevenshtein:
def test_sets_levenshtein(self):
a = set(list('hello'))
b = set(list('world'))
assert_equal(sets_levenshtein(a, b), 5)
class TestLevenshtein:
def test_levenshtein(self):
assert_equal(levenshtein('hello', 'world'), 4)
class TestBinomial:
def test_binomial(self):
# https://www.hackerrank.com/challenges/ncr
assert_equal(binomial(1, 2), 0)
assert_equal(binomial(2, 1), 2)
assert_equal(binomial(4, 0), 1)
assert_equal(binomial(5, 2), 10)
assert_equal(binomial(10, 3), 120)
assert_equal(binomial(87, 28) % 142857, 141525)
assert_equal(
binomial(100000, 4000),
binomial(100000, 96000) # same because 100000-96000=4000
)
@raises(OverflowError)
def test_binomial_overflow(self):
assert_equal(binomial(961173600, 386223045) % 142857, 0)
class TestFaulhaber:
def test_faulhaber(self):
def sumpow(n, p):
return sum((x**p for x in range(n+1)))
assert_equal(faulhaber(100, 0), 100)
assert_equal(faulhaber(100, 1), triangular(100))
assert_equal(faulhaber(100, 2), sum_of_squares(100))
assert_equal(faulhaber(100, 3), sum_of_cubes(100))
assert_equal(faulhaber(100, 4), sumpow(100, 4))
class TestBinomialExponent:
def test_binomial_exponent(self):
# https://www.math.upenn.edu/~wilf/website/dm36.pdf
assert_equal(binomial_exponent(88, 50, 3), 3)
# http://thales.math.uqam.ca/~rowland/packages/BinomialCoefficients/HTMLLinks/index_3.html
assert_equal(binomial_exponent(1000, 373, 2), 6)
for b in range(2, 11):
for n in range(1, 20):
for k in range(1, n):
assert_equal(binomial_exponent(n, k, b),
integer_exponent(binomial(n, k), b))
class TestProportional:
def test_proportional(self):
assert_equal(proportional(12, [0, 0, 1, 0]), [0, 0, 12, 0])
votes = [10, 20, 30, 40]
assert_equal(proportional(100, votes), votes)
assert_equal(proportional(10, votes), [1, 2, 3, 4])
assert_equal(sum(proportional(37, votes)), 37)
assert_equal(proportional(37, votes), [4, 7, 11, 15])
class TestTriangularRepartition:
def test_triangular_repartition(self):
ref = [0.01, 0.03, 0.05, 0.07, 0.09, 0.11, 0.13, 0.15, 0.17, 0.19]
res = triangular_repartition(1, 10)
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
ref.reverse()
res = triangular_repartition(0, 10)
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
ref = [0.02, 0.06, 0.1, 0.14, 0.18, 0.18, 0.14, 0.1, 0.06, 0.02]
res = triangular_repartition(.5, 10)
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
ref = [0.08, 0.24, 0.36, 0.24, 0.08]
res = triangular_repartition(.5, 5) # center value is top of triangle
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
class TestRectangularRepartition:
def test_rectangular_repartition(self):
ref = [.5, .125, .125, .125, .125]
res = rectangular_repartition(0, 5, .5)
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
ref = [0.3125, 0.3125, .125, .125, .125]
res = rectangular_repartition(.2, 5, .5)
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
ref.reverse()
res = rectangular_repartition(.8, 5, .5)
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
ref = [0.1, 0.1675, 0.3325, .1, .1, .1, .1]
res = rectangular_repartition(.325, 7, .4)
assert_equal(sum(res), 1)
assert_true(dist(res, ref) < 1E-6)
class TestNorm2:
def test_norm_2(self):
assert_equal(norm_2([-3, 4]), 5)
class TestNorm1:
def test_norm_1(self):
assert_equal(norm_1([-3, 4]), 7)
class TestNormInf:
def test_norm_inf(self):
assert_equal(norm_inf([-3, 4]), 4)
class TestNorm:
def test_norm(self):
assert_equal(norm([-3, 4], 2), 5)
assert_equal(norm([-3, 4], 1), 7)
class TestDist:
def test_dist(self):
pass # tested somewhere else
class TestSat:
def test_sat(self):
assert_equal(sat(3), 3)
assert_equal(sat(-2), 0)
assert_equal(sat(-3, -3), -3)
assert_equal(sat(3, 1, 2), 2)
assert_equal(sat([-2, -1, 0, 1, 2, 3], -1, 2), [-1, -1, 0, 1, 2, 2])
class TestVecneg:
def test_vecneg(self):
assert_equal(vecneg([-2, -1, 0, 1, 2, 3]), [2, 1, 0, -1, -2, -3])
class TestAngle:
def test_angle(self):
assert_equal(angle((1, 0), (0, 1)), math.pi/2)
assert_equal(angle((1, 0), (-1, 0)), math.pi)
assert_equal(angle((1, 1), (0, 1), unit=False), math.pi/4)
assert_equal(angle(vecunit((2, 1)), vecunit((1, -2))), math.pi/2)
class TestVecunit:
def test_vecunit(self):
v = vecunit((-3, 4, 5))
assert_equal(norm(v), 1)
class TestSinOverX:
def test_sin_over_x(self):
assert_equal(sin_over_x(1), math.sin(1))
assert_equal(sin_over_x(0), 1)
assert_equal(sin_over_x(1e-9), 1)
class TestSlerp:
def test_slerp(self):
u = vecunit((1, 1, 1))
v = vecunit((1, 1, -1))
assert_equal(slerp(u, v, 0), u)
assert_equal(slerp(u, v, 1), v)
s = slerp(u, v, 0.5)
assert_equal(s, vecunit((1, 1, 0)))
class TestLogFactorial:
def test_log_factorial(self):
assert_equal(log_factorial(100), 363.73937555556349014408)
class TestLogBinomialCoefficient:
def test_log_binomial(self):
assert_equal(log_binomial(87, 28), math.log(49848969000742658237160))
class Moebius:
def test_moebius(self):
assert_equal(moebius(3), -1)
class Omega:
def test_omega(self):
assert_equal(omega(3), 0)
assert_equal(omega(4), 1)
assert_equal(omega(6), 2)
class TestEulerPhi:
def test_euler_phi(self):
assert_equal(euler_phi(8849513), 8843520)
class TestKempner:
def test_kempner(self):
# from https://projecteuler.net/problem=549
assert_equal(kempner(1), 1)
assert_equal(kempner(8), 4)
assert_equal(kempner(10), 5)
assert_equal(kempner(25), 10)
# assert_equal(kempner(128),8) #TODO: find why it fails ...
assert_equal(sum(kempner(x) for x in range(2, 100+1)), 2012)
class TestRecurrence:
def test_recurrence(self):
# assert_equal(expected, recurrence(factors, values, max))
raise SkipTest
class TestLucasLehmer:
def test_lucas_lehmer(self):
assert_false(lucas_lehmer(1548)) # trivial case
assert_true(lucas_lehmer(11213)) # found on Illiac 2, 1963)
assert_false(lucas_lehmer(239))
class TestReverse:
def test_reverse(self):
# assert_equal(expected, reverse(i))
raise SkipTest
class TestLychrelSeq:
def test_lychrel_seq(self):
# assert_equal(expected, lychrel_seq(n))
raise SkipTest
class TestLychrelCount:
def test_lychrel_count(self):
# assert_equal(expected, lychrel_count(n, limit))
raise SkipTest
class TestPow:
def test_pow(self):
from Goulib.math2 import pow # make sure we don't use builtins
assert_equal(pow(10, 100), 1E100)
assert_not_equal(pow(10, -100), 0)
assert_equal(pow(2, 10, 100), 24)
# https://fr.wikipedia.org/wiki/Exponentiation_modulaire
assert_equal(pow(4, 13, 497), 445)
# http://www.math.utah.edu/~carlson/hsp2004/PythonShortCourse.pdf
assert_equal(pow(2, 13739062, 13739063), 2933187)
class TestIsqrt:
def test_isqrt(self):
assert_equal(isqrt(256), 16)
assert_equal(isqrt(257), 16)
assert_equal(isqrt(255), 15)
class TestAbundance:
def test_abundance(self):
# assert_equal(expected, abundance(n))
raise SkipTest
class TestFactorial:
def test_factorial(self):
assert_equal(factorial(0), 1)
assert_equal(factorial2(0), 1)
assert_equal([factorial2(x) for x in [7, 8, 9]], [105, 384, 945])
assert_equal(factorialk(5, 1), 120)
assert_equal(factorialk(5, 3), 10)
class TestCeildiv:
def test_ceildiv(self):
assert_equal(ceildiv(1, 3), 1)
assert_equal(ceildiv(5, 3), 2)
assert_equal(ceildiv(-1, 3), 0)
assert_equal(ceildiv(1, -3), 0)
class TestCatalanGen:
def test_catalan_gen(self):
assert_equal(itertools2.nth(20, catalan_gen()),
6564120420) # https://oeis.org/A000108
class TestCatalan:
def test_catalan(self):
assert_equal(catalan(20), 6564120420) # https://oeis.org/A000108
class TestPrimitiveTriples:
def test_primitive_triples(self):
def key(x): return (x[2], x[1])
for t in itertools2.take(10000, itertools2.ensure_sorted(primitive_triples(), key)):
assert_true(is_pythagorean_triple(*t))
class TestTriples:
def test_triples(self):
def key(x): return (x[2], x[1])
for t in itertools2.take(10000, itertools2.ensure_sorted(triples(), key)):
assert_true(is_pythagorean_triple(*t))
class TestPolygonal:
def test_polygonal(self):
pass # tested in test_oeis
class TestSquare:
def test_square(self):
pass # tested in test_oeis
class TestIsSquare:
def test_is_square(self):
pass # tested in test_oeis
class TestIsHexagonal:
def test_is_hexagonal(self):
pass # tested in test_oeis
class TestHeptagonal:
def test_heptagonal(self):
pass # tested in test_oeis
class TestIsHeptagonal:
def test_is_heptagonal(self):
pass # tested in test_oeis
class TestOctagonal:
def test_octagonal(self):
pass # tested in test_oeis
class TestIsOctagonal:
def test_is_octagonal(self):
pass # tested in test_oeis
class TestPartition:
def test_partition(self):
pass # tested in test_oeis
class TestChakravala:
def test_chakravala(self):
x, y = chakravala(61)
assert_true(x == 1766319049 and y == 226153980)
# https://en.wikipedia.org/wiki/Chakravala_method
class TestBouncy:
def test_bouncy(self):
# assert_equal(expected, bouncy(n))
raise SkipTest
class TestIsHappy:
def test_is_happy(self):
# assert_equal(expected, is_happy(n))
raise SkipTest
class TestNumberOfDivisors:
def test_number_of_divisors(self):
# assert_equal(expected, number_of_divisors(n))
raise SkipTest
class TestFactorialGen:
def test_factorial_gen(self):
# assert_equal(expected, factorial_gen())
raise SkipTest
class TestEuclidGen:
def test_euclid_gen(self):
# assert_equal(expected, euclid_gen())
raise SkipTest
class TestEgcd:
def test_egcd(self):
pass # tested below
class TestModInv:
def test_mod_inv(self):
assert_equal(mod_inv(3, 11), 4)
class TestModDiv:
def test_mod_div(self):
assert_equal(mod_div(3, 16, 53), 30)
assert_equal(mod_div(3, 16, 53), 30)
assert_equal(mod_div(5, 5, 12), 25)
class TestModFact:
def test_mod_fact(self):
assert_equal(mod_fact(10, 71), 61)
assert_equal(mod_fact(11, 71), 32)
class TestChineseRemainder:
def test_chinese_remainder(self):
assert_equal(chinese_remainder([3, 5, 7], [2, 3, 2]), 23)
# http://en.wikipedia.org/wiki/Chinese_remainder_theorem
assert_equal(chinese_remainder([3, 4, 5], [2, 3, 1]), 11)
class TestModBinomial:
def test_mod_binomial(self):
# http://math.stackexchange.com/questions/95491/n-choose-k-bmod-m-using-chinese-remainder-theorem
assert_equal(mod_binomial(456, 51, 30), 28)
# http://thales.math.uqam.ca/~rowland/packages/BinomialCoefficients/HTMLLinks/index_4.html
assert_equal(mod_binomial(1000, 729, 19), 13)