Example #1
0
    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))
Example #2
0
def primes(n):
    """memoized list of n first primes
    :warning: do not call with large n, use prime_gen instead
    """
    m=n-len(_primes)
    if m>0:
        more=list(itertools2.take(m,primes_gen(_primes[-1]+1)))
        _primes.extend(more)
        _primes_set.union(set(more))

    return _primes[:n]
Example #3
0
def primes(n):
    """memoized list of n first primes

    :warning: do not call with large n, use prime_gen instead
    """
    m = n - len(_primes)
    if m > 0:
        more = list(itertools2.take(m, primes_gen(_primes[-1] + 1)))
        _primes.extend(more)
        _primes_set.union(set(more))

    return _primes[:n]
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
 def test_repeat(self):
     #check that we can iterate twice in the same Sequence
     l1 = list(itertools2.take(20, A000040))
     l2 = list(itertools2.take(20, A000040))
     assert_equal(l1, l2)
Example #7
0
 def test_repeat(self):
     # check that we can iterate twice in the same Sequence
     l1 = list(itertools2.take(20, A000040))
     l2 = list(itertools2.take(20, A000040))
     assert_equal(l1, l2)
Example #8
0
 def test_triples(self):
     key=lambda x:(x[2],x[1])
     for t in take(10000,itertools2.ensure_sorted(triples(),key)):
         assert_true(is_pythagorean_triple(*t))
Example #9
0
 def test_triples(self):
     key=lambda x:(x[2],x[1])
     for t in take(10000,itertools2.ensure_sorted(triples(),key)):
         assert_true(is_pythagorean_triple(*t))
Example #10
0
 def test_repunit_gen(self):
     assert_equal(itertools2.take(5, repunit_gen(digit=1)),
                  [0, 1, 11, 111, 1111])
     assert_equal(itertools2.take(5, repunit_gen(digit=9)),
                  [0, 9, 99, 999, 9999])
Example #11
0
desc="Smallest member 'a' of the primitive Pythagorean triples (a,b,c) ordered by increasing c, then b"
A046086=Sequence(math2.primitive_triples,desc=desc).apply(lambda x:x[0])
    # .sort(key=lambda x:x[2]) \ #not needed anymore
    # .apply(lambda x:x[0])
    
""" found a bug in OEIS ! 20th term of the serie is 145, not 142 !

desc="Hypotenuse of primitive Pythagorean triangles sorted on area (A024406), then on hypotenuse"
A121727=Sequence(math2.primitive_triples,desc=desc) \
    .sort(lambda x:(x[0]*x[1],x[2])) \
    .apply(lambda x:x[2])
"""

# Build oeis dict by module introspection : Simple and WOW !
seqs=globals().copy()
oeis={}
for id in seqs:
    if id[0]=='A' and len(id)==7:
        seqs[id].name=id
        oeis[id]=seqs[id]
        
        
if __name__ == "__main__": 
    """local tests"""
    it=itertools2.sorted_iterable(math2.primitive_triples(),(lambda x:(x[0]*x[1],x[2])))
    for p in itertools2.take(30,it):
        print(p,p[0]*p[1]/2)


Example #12
0
 def test_repunit_gen(self):
     assert_equal(itertools2.take(5,repunit_gen(digit=1)),[0,1,11,111,1111])
     assert_equal(itertools2.take(5,repunit_gen(digit=9)),[0,9,99,999,9999])