Exemplo n.º 1
0
def main():
    limit = 1500000
    sumCount = {}
    # We have a+b+c = 2m^2 + 2mn > 2m^2, so we need only test check m up to floor(sqrt(limit/2))
    for m in range(1,math.floor(math.sqrt(limit/2))):
        # m,n have different parity
        n = 1+(m%2)
        while n<m:
            #Establishing coprimality condition
            if gcd.main(m,n) == 1:
                #Taking care of nonprimitive triples
                sidelength = 2*m*m+2*m*n
                mult = 1
                while sidelength*mult <= limit:
                    sumCount[mult*(2*m*m+2*m*n)]= 1+sumCount.get(mult*(2*m*m+2*m*n),0)
                    mult += 1
            n += 2
    total = 0
    for i in sumCount:
        if sumCount.get(i) == 1:
            total += 1
    return total
    
    
    
Exemplo n.º 2
0
def main(frac_floor1, frac_floor2, frac_ceil1, frac_ceil2, d):
    #solves the defined problem
    #as given, frac_floor1 = 1, frac_floor2 = 3, frac_ceil1 = 1, frac_ceil2 = 2, d = 12000

    frac_num = 0
    #will count number of irreducible fractions

    for b in range(2, (d + 1)):
        #want to check all denominators 2 to d

        for a in range((((b * frac_floor1) // frac_floor2) + 1),
                       ((((b * frac_ceil1) - 1) // frac_ceil2) + 1)):
            #see above calculations for cap and floor on a

            #TEST
            print('a=', a, 'b=', b)
            #TEST
            #BOUNDS ARE INCORRECT

            if gcd.main(a, b) == 1:
                #i.e. if a/b is an irreducible fraction, then add one to the fraction count

                frac_num += 1

    return frac_num
Exemplo n.º 3
0
def main( x ):

	count = 1
	#since 1 will always be relatively prime to an integer

	for i in range( 2, x ):
	#checks all integers >= 2 and less than x. if gcd(x,i) is one (i.e. if i and x are coprime), then increments count

		if gcd.main( x, i ) == 1:

			count += 1

	return count
Exemplo n.º 4
0
def main(x):
    #finds all primitive pythagorean triples of sum < x
    #based on Euclid's formula as explained on https://en.wikipedia.org/wiki/Pythagorean_triple

    trip_list = []
    m = 1
    n = 1

    while (2 * (m**2) + 2 * m * n) < x:
        #sum of a,b,c to be generated

        m += 1
        #increments m

        n_max = min(m, ((x - (2 * (m**2))) // (2 * m) + 1))
        #finds cap for search of n to keep total length below x

        #REPLACED
        #		n_max = m
        #REPLACED

        for n in range(1, n_max):
            #search through n < n_max

            if ((m % 2 == 1) or (n % 2 == 1)) and ((m % 2 == 0) or
                                                   (n % 2 == 0)):
                #must have one even and one odd to get primitive triple

                if gcd.main(m, n) == 1:
                    #need m,n to be coprime to get primitive triple

                    a = ((m**2) - (n**2))

                    b = (2 * m * n)

                    c = ((m**2) + (n**2))
                    #calculates the triple

                    trip_list.append([a, b, c])
                    #adds triple to the list

        n = 1
        #needed so while loop doesn't end early

    return trip_list
Exemplo n.º 5
0
def main(x, y, cap):
    #solves the defined problem for fraction to the left of x/y for d<= cap
    #as defined, x = 3, y = 7, cap = 10**6

    best = [1, 0]
    #will store answer

    desired = x / y
    #the number that we want to get close to

    for b in range(2, cap + 1):
        #checks all divisors 2 -> d

        a = (x * b - 1) // y
        #finds the highest integer a for which a/b is less than x/y

        if a != 0:
            #necessary since gcd function will break if a = 0

            if gcd.main(a, b) == 1:
                #need a,b to be coprime for fraction to be irreducible

                #TEST
                #				print('a=',a,'b=',b)
                #TEST

                diff = desired - (a / b)
                #calculates the difference between x/y and a/b. will always be positive since it is assured that a/b < x/y

                if diff < best[0]:
                    #i.e. if best a/b found so far

                    best[0] = diff
                    best[1] = [a, b]
                    #stores the best values found so far

    return best
Exemplo n.º 6
0
from gcd import main
from pyannotate_runtime import collect_types

if __name__ == '__main__':
    collect_types.init_types_collection()
    with collect_types.collect():
        main()
    collect_types.dump_stats('type_info.json')