Exemple #1
0
def pi_gmpy(prec):
    """gmpy.mpf"""
    set_minprec(prec)
    lasts, t, s, n, na, d, da = mpf(0), mpf(3), mpf(3), mpf(1), mpf(0), mpf(
        0), mpf(24)
    while s != lasts:
        lasts = s
        n, na = n + na, na + 8
        d, da = d + da, da + 32
        t = (t * n) / d
        s += t
    return s
def fermat_factor(N, minutes=10, verbose=False):
   """
   Code based on Sage code from FactHacks, a joint work by
   Daniel J. Bernstein, Nadia Heninger, and Tanja Lange.

   http://facthacks.cr.yp.to/
   
   N - integer to attempt to factor using Fermat's Last Theorem
   minutes - number of minutes to run the algorithm before giving up
   verbose - (bool) Periodically show how many iterations have been
      attempted
   """
   from time import time
   current_time = int(time())
   end_time = current_time + int(minutes * 60)

   def sqrt(n):
      return gmpy.fsqrt(n)
  
   def is_square(n):
      sqrt_n = sqrt(n)
      return sqrt_n.floor() == sqrt_n

   if verbose:
      print "Starting factorization..."
   
   gmpy.set_minprec(4096)

   N = gmpy.mpf(N)
   if N <= 0:        return [1,N]
   if N % 2 == 0:    return [2,N/2]

   a = gmpy.mpf(gmpy.ceil(sqrt(N)))
   count = 0

   while not is_square(gmpy.mpz(a ** 2 - N)):
      a += 1
      count += 1
      if verbose:
         if (count % 1000000 == 0):
            sys.stdout.write("\rCurrent iterations: %d" % count)
            sys.stdout.flush()
      if time() > end_time:
         if verbose: print "\nTime expired, returning [1,N]"
         return [1,N]

   b = sqrt(gmpy.mpz(a ** 2 - N))
   print "\nModulus factored!"
   return [long(a - b), long(a + b)]
Exemple #3
0
def fermat_factor(N, minutes=10, verbose=False):
   """
   Code based on Sage code from FactHacks, a joint work by
   Daniel J. Bernstein, Nadia Heninger, and Tanja Lange.

   http://facthacks.cr.yp.to/
   
   N - integer to attempt to factor using Fermat's Last Theorem
   minutes - number of minutes to run the algorithm before giving up
   verbose - (bool) Periodically show how many iterations have been
      attempted
   """
   from time import time
   current_time = int(time())
   end_time = current_time + int(minutes * 60)

   def sqrt(n):
      return gmpy.fsqrt(n)
  
   def is_square(n):
      sqrt_n = sqrt(n)
      return sqrt_n.floor() == sqrt_n

   if verbose:
      print "Starting factorization..."
   
   gmpy.set_minprec(4096)

   N = gmpy.mpf(N)
   if N <= 0:        return [1,N]
   if N % 2 == 0:    return [2,N/2]

   a = gmpy.mpf(gmpy.ceil(sqrt(N)))
   count = 0

   while not is_square(gmpy.mpz(a ** 2 - N)):
      a += 1
      count += 1
      if verbose:
         if (count % 1000000 == 0):
            sys.stdout.write("\rCurrent iterations: %d" % count)
            sys.stdout.flush()
      if time() > end_time:
         if verbose: print "\nTime expired, returning [1,N]"
         return [1,N]

   b = sqrt(gmpy.mpz(a ** 2 - N))
   print "\nModulus factored!"
   return [long(a - b), long(a + b)]
Exemple #4
0
import gmpy
from gmpy import mpz

gmpy.set_minprec(700)

N1 = mpz(
    '179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581'
)
N2 = mpz(
    '648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877'
)

N = N1


def try_midpoint(N, midpoint):
    delta = gmpy.sqrt(midpoint * midpoint - N)
    p = midpoint - delta
    q = midpoint + delta
    if p * q == N:
        return min(p, q)
    else:
        return False


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)
#!/usr/bin/python

# Calculate digits of e
# Marcus Kazmierczak, [email protected]
# July 29th, 2004

# the formula
#  e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ... 1/N!

# You should chop off the last five digits returned
# they aren't necessarily accurate



# precision library 
import gmpy

# how many digits (roughly)
N = 1000


gmpy.set_minprec(int(N*3.5+16))
e = gmpy.mpf('1.0')
    
for n in range(1,N+3):
    e = gmpy.fdigits(gmpy.mpf(e) + (gmpy.mpf('1.0') / gmpy.fac(n)))

print e
Exemple #6
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'))
Exemple #7
0
def num_longest_period(length):
    #Minimum accuracy to get the correct answer - 10000.
    gmpy.set_minprec(10000)
    #We search only prime numbers, because they have the longest periods
    fact_lst = Factors(length).make_sieve()
    return max(((period(1 / gmpy.mpf(i)), i) for i in fact_lst), key=itemgetter(0))
Exemple #8
0
import gmpy
from gmpy import mpz

gmpy.set_minprec(700)

N1 = mpz('179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581')
N2 = mpz('648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877')

N = N1
def try_midpoint(N, midpoint):
    delta = gmpy.sqrt(midpoint*midpoint - N)
    p = midpoint - delta
    q = midpoint + delta
    if p*q == N:
        return min(p,q)
    else:
        return False

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
Exemple #9
0
import sys

from math import sqrt
from gmpy import mpf, mpz, set_minprec

def fib(n):
    """
    Returns n-th fibonacci number using Binet's formula
    """
    gr = (1 + mpf(5).sqrt()) / 2
    return mpz((gr**n - (1-gr)**n)/mpf(5).sqrt())

def find_term(length):
    '''
    Find Fibonacci's term number for number that contains over lenght digits
    '''
    index = 0
    num = mpz(0)
    while True:
        index += 1
        num = fib(index)
        if num.numdigits() >= length:
            # help(numdigits) says it's can be off by one some times  
            return (index, index + 1)


set_minprec(1024)
print find_term(int(sys.argv[1]))
Exemple #10
0
from flask import Flask, request, redirect
import subprocess
import gmpy

gmpy.set_minprec(200)

app = Flask(__name__)


@app.route('/')
def hello_world():
    args = dict(x=0, y=0, wid=2, iters=10)
    for k, v in request.args.iteritems():
        args[k] = v
    args["i2"] = int(int(args["iters"]) * 1.2)
    return """
    <html>
    <body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){
        $("#i").click(function(e){
            var ox = e.offsetX;
            var oy = e.offsetY;
            console.log(ox, oy);
            window.location = "/s/" + ox + "/" + oy + "" + window.location.search;
            })
        })
    </script>
    <dl>
        <dt>X</dt><dd>%(x)s</dd>