예제 #1
0
def try_weightedavg(N, weightedavg2sq):
    # gmpy.set_minprec(1000)
    # print weightedavg2 - 2*gmpy.ceil(gmpy.fsqrt(6*N))
    import pdb
    # pdb.set_trace()
    # radical = gmpy.fsqrt(weightedavg2sq - 24*N)
    # numerators = [gmpy.fsqrt(weightedavg2sq) + x for x in [radical, -radical]]
    # for n in numerators:
    #     p = gmpy.cdivmod(mpz(gmpy.fround(n)), 6)[0] # +/- 1?
    #     # q = gmpy.cdivmod(weightedavg2 - 3*p, 2)[0]
    #     if(gmpy.cdivmod(N,p)[1] == 0):
    #         print("found divisor")
    #         print p
    #     if p*q == N:
    #         return min(p,q)
    #     print "diff"
    #     print (3*p + 2*q)/2 - gmpy.ceil(gmpy.sqrt(6*N))

    # pdb.set_trace()
    # weightedavg2 = mpz(gmpy.floor(gmpy.fsqrt(weightedavg2sq)))
    # num = weightedavg2 - mpz(gmpy.fsqrt(N/2)) - 1
    # lim = weightedavg2 + 1
    # done = False
    # while not done and num < lim:
    #     # print num
    #     p = gmpy.cdivmod(num, 6)[0] # +/- 1?
    #     # q = gmpy.cdivmod(weightedavg2 - 3*p, 2)[0]
    #     if(gmpy.cdivmod(N,p)[1] == 0):
    #         done = True
    #         print("found divisor")
    #         print min(p, gmpy.cdivmod(N,p)[0])
    #         return True
    #     num += 1

    weightedavg2 = gmpy.ceil(gmpy.fsqrt(weightedavg2sq))
    num = weightedavg2 - gmpy.fsqrt(weightedavg2 * weightedavg2 - 24 * N)
    p = gmpy.cdivmod(mpz(num), 6)[0]
    for i in xrange(0, 100000):
        if (gmpy.cdivmod(N, p + i)[1] == 0 or gmpy.cdivmod(N, p - i)[1] == 0):
            print("found divisor")
            q = gmpy.cdivmod(N, p)[0]
            print min(p, q)
            print N == p * q
            print i
            return True

    return False
예제 #2
0
파일: hw6.py 프로젝트: robmike/cryptography
def try_weightedavg(N, weightedavg2sq):
    # gmpy.set_minprec(1000) 
    # print weightedavg2 - 2*gmpy.ceil(gmpy.fsqrt(6*N))
    import pdb
    # pdb.set_trace()
    # radical = gmpy.fsqrt(weightedavg2sq - 24*N)
    # numerators = [gmpy.fsqrt(weightedavg2sq) + x for x in [radical, -radical]]
    # for n in numerators:
    #     p = gmpy.cdivmod(mpz(gmpy.fround(n)), 6)[0] # +/- 1?
    #     # q = gmpy.cdivmod(weightedavg2 - 3*p, 2)[0]
    #     if(gmpy.cdivmod(N,p)[1] == 0):
    #         print("found divisor")
    #         print p
    #     if p*q == N:
    #         return min(p,q)
    #     print "diff"
    #     print (3*p + 2*q)/2 - gmpy.ceil(gmpy.sqrt(6*N))

    # pdb.set_trace()
    # weightedavg2 = mpz(gmpy.floor(gmpy.fsqrt(weightedavg2sq)))
    # num = weightedavg2 - mpz(gmpy.fsqrt(N/2)) - 1
    # lim = weightedavg2 + 1
    # done = False
    # while not done and num < lim:
    #     # print num
    #     p = gmpy.cdivmod(num, 6)[0] # +/- 1?
    #     # q = gmpy.cdivmod(weightedavg2 - 3*p, 2)[0]
    #     if(gmpy.cdivmod(N,p)[1] == 0):
    #         done = True
    #         print("found divisor")
    #         print min(p, gmpy.cdivmod(N,p)[0])
    #         return True
    #     num += 1

    weightedavg2 = gmpy.ceil(gmpy.fsqrt(weightedavg2sq))
    num = weightedavg2 - gmpy.fsqrt(weightedavg2*weightedavg2 - 24*N)
    p = gmpy.cdivmod(mpz(num), 6)[0]
    for i in xrange(0, 100000):
        if(gmpy.cdivmod(N,p+i)[1] == 0 or gmpy.cdivmod(N,p-i)[1] == 0) :
            print("found divisor")
            q = gmpy.cdivmod(N,p)[0]
            print min(p, q)
            print N == p*q
            print i
            return True

    return False
예제 #3
0
def fibonacci_term_number(length):
    '''
    Find Fibonacci's term number for number that contains over (length) digits
    '''
    index = long(1)
    #Initial step divisor value
    step_div = 1
    num = gmpy.mpz(0)
    while gmpy.numdigits(num) != length:

        golden_ratio = gmpy.mpf(((1 + gmpy.fsqrt(5)) / 2)**gmpy.mpf(index) / gmpy.fsqrt(5))
        num = gmpy.mpz(gmpy.fround(golden_ratio, 0))

        if gmpy.numdigits(num) < length:
            index += length / step_div
        #If the previous step was too large, then we go back and reduce it to 2 times
        if gmpy.numdigits(num) > length:
            index -= length / step_div
            step_div *= 2
    return index
예제 #4
0
 def sqrt(n):
    return gmpy.fsqrt(n)
예제 #5
0
def func1(N):
    """Computes p and q based on N and the fact that  |p−q| < 2*N**(1/4)"""
    A = mpz(ceil(fsqrt(N)))
    return _func1(N, A)
예제 #6
0
 def sqrt(n):
    return gmpy.fsqrt(n)
예제 #7
0
#!/usr/bin/env python
import gmpy
import sys

gmpy.set_minprec(20000)

f = sys.stdin
t = int(f.next())

for case in range(1, t + 1):
    n = int(f.next())
    s = 3 + gmpy.fsqrt(5)
    res = long(s**n)
    print "Case #%s: %s" % (case, str(res)[-3:].rjust(3, '0'))
예제 #8
0
def func1(N):
    """Computes p and q based on N and the fact that |p−q| < 2*N**(1/4)"""
    A = mpz(ceil(fsqrt(N)))
    return _func1(N, A)