def pe(M): s = 0 for m in range(2, isqrt(M/2)): mm = (M-1) // (2*m) m2 = m * m start = 1 if m % 2 == 0 else 2 for n in range(start, min(mm-m+1, m), 2): if gcd(m, n) != 1: continue n2 = n * n if (m2+n2) % (m2-n2-2*m*n) != 0: continue s += mm // (m+n) return s
def pe(M): s = 0 for m in range(2, isqrt(M / 2)): mm = (M - 1) // (2 * m) m2 = m * m start = 1 if m % 2 == 0 else 2 for n in range(start, min(mm - m + 1, m), 2): if gcd(m, n) != 1: continue n2 = n * n if (m2 + n2) % (m2 - n2 - 2 * m * n) != 0: continue s += mm // (m + n) return s
def pe(M): rad = [0] * M rad[1] = 1 for p in range(2, M): if rad[p] > 0: continue isqrtp = isqrt(p) flag = True for q in range(2, isqrtp + 1): if p % q != 0: continue pp = p while pp % q == 0: pp //= q qq = q * rad[pp] pp = p while pp < M: rad[pp] = qq pp *= q flag = (q > isqrtp) break if flag: pp = p while pp < M: rad[pp] = p pp *= p s = 0 for c in range(3, M): cc = (c - 1) // rad[c] if rad[c - 1] <= cc: s += c if cc < 6: continue if c % 2 == 0 and cc < 15: continue if c % 3 == 0 and cc < 10: continue for a in range(2, c // 2): b = c - a if rad[a] > cc or rad[b] > cc: continue if rad[a] * rad[b] <= cc and gcd(a, b) == 1: s += c return s
def pe(M): rad = [0] * M rad[1] = 1 for p in range(2, M): if rad[p] > 0: continue isqrtp = isqrt(p) flag = True for q in range(2, isqrtp + 1): if p % q != 0: continue pp = p while pp % q == 0: pp //= q qq = q * rad[pp] pp = p while pp < M: rad[pp] = qq pp *= q flag = (q > isqrtp) break if flag: pp = p while pp < M: rad[pp] = p pp *= p s = 0 for c in range(3, M): cc = (c-1)//rad[c] if rad[c-1] <= cc: s += c if cc < 6: continue if c % 2 == 0 and cc < 15: continue if c % 3 == 0 and cc < 10: continue for a in range(2, c//2): b = c - a if rad[a] > cc or rad[b] > cc: continue if rad[a] * rad[b] <= cc and gcd(a, b) == 1: s += c return s
#Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed? #Note: This problem has been changed recently, please check that you are using the right parameters. #Answer: #161667 from time import time; t=time() from mathplus import gcd, isqrt M = 1500000 q = [0]*(M+1) for m in range(2, isqrt(M/2)+1): for n in range(m % 2 + 1, min(m, M//(2*m)-m+1), 2): #length = 2*m*(m+n) #if length > M: break #x = m*m-n*n #y = 2*m*n #if gcd(x,y)==1: if gcd(m, n)==1: length = 2*m*(m+n) q[length] += 1 p = [0]*(M+1) for m, c in enumerate(q): if c: for n in range(m, M+1, m): p[n] += c print(sum(1 for i in p if i == 1))#, time()-t
#!/usr/bin/python # -*- coding: utf-8 -*- #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? #Note: The upper limit has been changed recently. #Answer: #7295372 from time import time; t=time() from mathplus import gcd D=12000 cnt = 0 for n in range(5, D+1): for m in range(n//3+1, (n+1)//2): if gcd(n, m) == 1: cnt += 1 print(cnt)#, time()-t
t = time() from mathplus import isqrt, gcd M = 1000 p = [0] * (M // 2 + 1) q = [0] * (M // 2 + 1) for n in range(1, M // 2 + 1): for k in range(1, n): if n % k != 0: continue m = n // k if q[m] == 0: for i in range(isqrt(m // 2) + 1, isqrt(m) + 1): if m % i != 0: continue u, v = i, m // i - i if (u + v) % 2 != 0 and gcd(u, v) == 1: q[m] += 1 #if n == 60: #print k, u, v, k*(u*u-v*v), 2*k*u*v, k*(u*u+v*v) p[n] += q[m] print(max((x, i) for i, x in enumerate(p))[1] * 2) #, time()-t ''' from time import time; t=time() from mathplus import isqrt, gcd M = 1000 ss = 0 MM = M/2+1 p = [0] * MM
from time import time; t=time() from mathplus import isqrt, gcd M = 1000 p = [0] * (M//2+1) q = [0] * (M//2+1) for n in range(1, M//2+1): for k in range(1, n): if n % k != 0: continue m = n//k if q[m] == 0: for i in range(isqrt(m//2)+1, isqrt(m)+1): if m % i != 0: continue u, v = i, m//i - i if (u+v) % 2 != 0 and gcd(u, v) == 1: q[m] += 1 #if n == 60: #print k, u, v, k*(u*u-v*v), 2*k*u*v, k*(u*u+v*v) p[n] += q[m] print(max((x, i) for i, x in enumerate(p))[1]*2)#, time()-t ''' from time import time; t=time() from mathplus import isqrt, gcd M = 1000 ss = 0 MM = M/2+1 p = [0] * MM