Beispiel #1
0
def f(fraction, i): #fraction = (num,den)
    frac_num = fraction[0]
    frac_den = fraction[1]
    new_frac_num = frac_den
    new_frac_den = numbers[i]*frac_den + frac_num
    g = gcd(new_frac_den, new_frac_num)
    new_frac_den /= g
    new_frac_num /= g
    if i ==0: return new_frac_den
    return f((new_frac_num,new_frac_den),i-1)
Beispiel #2
0
def E69():
    res = 0
    max_res = 0
    max_i = 0
    for i in range (2, MAXNUM):
        if i % 2 == 1:
            continue;
        if is_prime(i) == True:
            continue
        cnt = 1
        for j in range (1, i):
            if gcd(i, j) == 1:
                cnt+=1
        if cnt != 0:
            res = float(i)/float(cnt)
            if max_res < res:
                max_res = res
                max_i = i

    print max_res, max_i
    return 0
Beispiel #3
0
def E69():
    res = 0
    max_res = 0
    max_i = 0
    for i in range(2, MAXNUM):
        if i % 2 == 1:
            continue
        if is_prime(i) == True:
            continue
        cnt = 1
        for j in range(1, i):
            if gcd(i, j) == 1:
                cnt += 1
        if cnt != 0:
            res = float(i) / float(cnt)
            if max_res < res:
                max_res = res
                max_i = i

    print max_res, max_i
    return 0
Beispiel #4
0
    triangles2(i)
"""


asa=[]
for i in range(1500001):
	asa.append(0)

t=[]
#620x620    
#var result = myList.OrderBy(k => k.Item2);
limit=sqrt(1500000/float(2))
print int(limit)+1
for n in range(1,int(limit)+1):
    for m in range(1,int(limit)+1):
        if m>n and gcd(m,n)==1 and (m-n)%2!=0:
            #print m*m-n*n,2*m*n,m*m+n*n,2*m*m+2*m*n
            a=m*m-n*n
            b=2*m*n
            c=m*m+n*n
            L=2*m*m+2*m*n
            t.append((a,b,c,L))
            for s in range(L,1500001,L):
                asa[s]+=1
                
print t

"""
for i in range(0,len(t)):
    for k in range(2,(t[len(t)-1][3]/t[i][3])+1):
        t.append((t[i][0]*k,t[i][1]*k,t[i][2]*k,t[i][3]*k))
Beispiel #5
0
'''
This is just an extension of Problem 9 of project euler.
I just initiated the value, then took a squareroot( since the values req. are 3)
Then did a gcd b/w the 2 values to find the later value. 
Then did a sum to get the Length. Then added them and printed them out.
'''


from Euler import gcd, sqrt
 
L = 1500000
sqrt_L = int(sqrt(L))
lengths = [0]*L #initating the arrays.
for i in range(1, sqrt_L, 2):
  for j in range(2, sqrt_L- i , 2):
      if gcd(i, j) == 1: 
           sum = abs(j*j - i*i) + 2*i*j + i*i + j*j
           for s in range(sum, L, sum):
	           lengths[s]+=1
		    
print lengths.count(1)
from math import floor, ceil
from Euler import gcd

def mainly_slow ():
  
  frac_set = set()
  
  #for d in range(2, 100+1):
  for d in range(2, 12*10**3+1):
    if not d%100:
      print d
    
    # for this d, get the ints that bound 1/3 & 2/3
    n_min = int(ceil(d/3.))
    n_max = int(floor(d/2.))

    for n in range(n_min, n_max+1):
      f = Fraction(n,d)
      frac_set.add(f)


  print '\nAnswer:', len(frac_set)-2

  pass

total = 0
for d in range(3, 12*10**3+1):
  for n in range(int(ceil(d/3.)), int(floor(d/2.))):
    if gcd(n,d) == 1:
      total += 1
print total
Beispiel #7
0
"""
This is just an extension of Problem 9 of project euler.
I just initiated the value, then took a squareroot( since the values req. are 3)
Then did a gcd b/w the 2 values to find the later value. 
Then did a sum to get the Length. Then added them and printed them out.
"""


from Euler import gcd, sqrt

L = 1500000
sqrt_L = int(sqrt(L))
lengths = [0] * L  # initating the arrays.
for i in range(1, sqrt_L, 2):
    for j in range(2, sqrt_L - i, 2):
        if gcd(i, j) == 1:
            sum = abs(j * j - i * i) + 2 * i * j + i * i + j * j
            for s in range(sum, L, sum):
                lengths[s] += 1

print lengths.count(1)
Beispiel #8
0
from Euler import gcd
 
L = 12000+1
c = 0
for n in range(5, L):
  for k in range(n/3 + 1, (n-1)/2 + 1):
    if gcd(n,k) == 1: c+=1
 
print "Answer to PE73 =", c
Beispiel #9
0
#91. Count the number of lattice point right triangles which use the origin
# in the 50x50 grid.


from Euler import gcd

count = 3*50**2 #base has slope 0

for x in xrange(1,51):
    for y in xrange(1,51): #this will run over all the points (x,y) which
        g = gcd(x,y)        #form the right base point 
        x_new =x/g
        y_new =y/g
        k=1
        while k <= x//y_new:
            if y + k*x_new <=50:
                count += 2
            k += 1
        
print count
Beispiel #10
0
<div id="content">
<div style="text-align:center;" class="print"><img src="images/print_page_logo.png" alt="projecteuler.net" style="border:none;" /></div>
<h2>Counting fractions in a range</h2><div id="problem_info" class="info"><h3>Problem 73</h3><span>Published on Friday, 2nd July 2004, 06:00 pm; Solved by 15547; Difficulty rating: 15%</span></div>
<div class="problem_content" role="problem">
<p>Consider the fraction, <i>n/d</i>, where <i>n</i> and <i>d</i> are positive integers. If <i>n</i>&lt;<i>d</i> and HCF(<i>n,d</i>)=1, it is called a reduced proper fraction.</p>
<p>If we list the set of reduced proper fractions for <i>d</i> &le; 8 in ascending order of size, we get:</p>
<p style="text-align:center;font-size:90%;">1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, <b>3/8, 2/5, 3/7</b>, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8</p>
<p>It can be seen that there are 3 fractions between 1/3 and 1/2.</p>
<p>How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for <i>d</i> &le; 12,000?</p>
</div><br />
<br /></div>
"""

from Euler import gcd

MAXNUM=51
MAXNUM=12001
a2=1
a3=1

cnt = 0
for i in range(4, MAXNUM):
    a2 = i*3
    a3 = i*2
    for j in range(a3+1, a2):
        if j%6 == 0:
            if gcd(i, j/6) == 1:
                cnt+=1

print cnt