Ejemplo n.º 1
0
def main():
    limit = 1500000
    perimeters = [0]*(limit + 1)

    for m in range(2, int(sqrt(limit>>1))):
        for n in range(2 if m&1 else 1, m, 2):
            if gcd(m, n) == 1:

                p = m*(m+n)<<1 # a + b + c

                if p <= limit:
                    for i in range(p, limit, p):
                        perimeters[i] += 1

    return perimeters.count(1)
Ejemplo n.º 2
0
# for d in range(8, 1000001):
# 	# lower< n/d < 3/7
# 	fractions = [target]
# 	upper_n = int(d * upper) + 1
# 	lower_n = int(d * lower)
# 	for n in range(lower_n, upper_n):
# 		if gcd(d, n) == 1:
# 			fractions.append((float(n)/d, str(n)+'/'+str(d)))
# 	if len(fractions) > 1:
# 		fractions.sort()
# 		idx = fractions.index(target)
# 		if fractions[idx-1][0] > lower: 
# 			lower = fractions[idx-1][0]
# 			result = fractions[idx-1]

# print result

# only need to check n = (d*3/7)
from Helper import gcd
upper = 0
result = 0

for d in range(8, 1000001):
	n = int(d * 3.0/7)
	if gcd(d, n) == 1:
		temp = float(n) / d
		if temp > upper:
			upper = temp
			result = n
print result
Ejemplo n.º 3
0
"""

Consider the fraction, n/d, where n and d are positive integers. 
If n<d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d <= 8 in 
ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 
3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that there are 3 fractions between 1/3 and 1/2.

How many fractions lie between 1/3 and 1/2 in the sorted set 
of reduced proper fractions for d <= 12,000?
"""
from Helper import gcd
 
limit = 12001
count = 0

for d in range(5, limit):
	lower = d / 3 + 1
	upper = d / 2 + 1
	for n in range(lower, upper):
		if gcd(n, d) == 1:
			count += 1
print count
Ejemplo n.º 4
0
# Generate pythagorean triple using
# http://en.wikipedia.org/wiki/Pythagorean_triple
# a = m^2 - n^2, b = 2mn, c = m^2 + n^2
#  m > n, and m+n is odd and gcd(n,m) = 1

from math import sqrt
from Helper import gcd

Limit = 1500000
mlimit = int(sqrt(Limit / 2)) + 1  # a+b+c<Limit
# list to point how many times the same value
# a + b + c has been caculated
wires = [0] * Limit
count = 0

for m in range(2, mlimit):
    for n in range(1, m):
        if (m + n) % 2 == 1 and gcd(m, n) == 1:
            a = m ** 2 - n ** 2
            b = 2 * m * n
            c = m ** 2 + n ** 2
            length = a + b + c
            while length < Limit:
                wires[length] += 1
                if wires[length] == 1:
                    count += 1
                elif wires[length] == 2:
                    count -= 1
                length += a + b + c
print count
Ejemplo n.º 5
0
#https://raw.githubusercontent.com/AaronJiang/ProjectEuler/master/py/problem073.py
"""

Consider the fraction, n/d, where n and d are positive integers. 
If n<d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d <= 8 in 
ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 
3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that there are 3 fractions between 1/3 and 1/2.

How many fractions lie between 1/3 and 1/2 in the sorted set 
of reduced proper fractions for d <= 12,000?
"""
from Helper import gcd
 
limit = 12001
count = 0

for d in range(5, limit):
	lower = d / 3 + 1
	upper = d / 2 + 1
	for n in range(lower, upper):
		if gcd(n, d) == 1:
			count += 1
print count
Ejemplo n.º 6
0
"""

Consider the fraction, n/d, where n and d are positive integers. 
If n<d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d <= 8 in 
ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 
5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that 2/5 is the fraction immediately to the left of 3/7.

By listing the set of reduced proper fractions for d <= 1,000,000 in 
ascending order of size, find the numerator of the fraction immediately 
to the left of 3/7.
"""

from Helper import gcd
upper = 0
result = 0

for d in range(8, 1000001):
    n = int(d * 3.0 / 7)
    if gcd(d, n) == 1:
        temp = float(n) / d
        if temp > upper:
            upper = temp
            result = n
print result