[PYTHON] Project Euler 33

problem

49/98 is an interesting fraction. An inexperienced mathematician mistakenly thinks, "If you remove 9 from each of the numerator and denominator, you get 49/98 = 4/8, which is a simple form." Maybe. (The method is incorrect, but in the case of 49/98 it happens to be the correct fraction.)

We assume that types like 30/50 = 3/5 are trivial examples.

Of these fractions, there are four that are less than one and are not trivial, with both the numerator and denominator being two-digit numbers.

Answer the value of the denominator when the product of the four fractions is given in the reduced form. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2033

Answer

Two fractions a1 / b1 and a2 / b2 have the same value → a1//gcd(a1, b1) == a2//gcd(a2,b2) and b1//gcd(a1, b1) == b2//gcd(a2,b2) A fraction of a fraction a / b → (a//gcd(a,b)) / (b//gcd(a,b))

I coded it appropriately based on the above facts. I want to fix it soon (= I won't do it)

def gcd(m,n):
  if m < n:
    (m,n) = (n,m)
  while n != 0:
    (m,n) = (n,m%n)
  return m
  
def main():
  seq = range(11,100)
  ans_x = 1
  ans_y = 1
  for i in seq:
    for j in seq[i+1:]:
      for (x, y) in delete_same_number(i,j):
        x, y = int(x), int(y)
        if x == 0 or y == 0:
          break

        gcd1 = gcd(i,j)
        gcd2 = gcd(x,y)
        if (i//gcd1 == x//gcd2) and (j//gcd1 == y//gcd2):
          if (i%10) == 0 or (j%10) == 0:
            pass
          else:
            ans_x *= x//gcd2
            ans_y *= y//gcd2
  print ans_y//gcd(ans_x,ans_y)

def delete_same_number(i,j):
  s1 = str(i)
  s2 = str(j)
  if s1[0] == s2[0]:
    yield (s1[1], s2[1])
  if s1[0] == s2[1]:
    yield (s1[1], s2[0])
  if s1[1] == s2[0]:
    yield (s1[0], s2[1])
  if s1[1] == s2[1]:
    yield (s1[0], s2[0])
  yield ('0','0')
main()

Recommended Posts

Project Euler 7
Project Euler 47
Project Euler 31
Project Euler 38
Project Euler 17
Project Euler 8
Project Euler 23
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 42
Project Euler 33
Project Euler 32
Project Euler 43
Project Euler 35
Project Euler 36
Project Euler 24
Project Euler 46
Project Euler 48
Project Euler 45
Project Euler 6
Project Euler 44
Project Euler 39
Project Euler 40
Project Euler 49
Project Euler 29
Project Euler 27
Project Euler 41
Project Euler 18
Project Euler 13
Project Euler 30
Project Euler 16
Project Euler 14
Project Euler 34
Project Euler 25
[Project Euler] problem1
Project Euler15 "Lattice Path"
Project Euler Original Method Group 1
What is Project Euler 3 Acceleration?
[Note] Project Euler in Python (Problem 1-22)
Functional programming in Python Project Euler 3
Project Euler # 5 "Minimum Multiples" in Python
Project Euler 4 Attempt to speed up
Functional programming in Python Project Euler 2
Project Euler 11 "Maximum product in grid"
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler 9 Retention of calculation results
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
I wrote Project Euler 1 in one liner.
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Project Euler # 8 "Maximum Product in Number String" in Python
Project Euler # 10 "sum of prime numbers" in Python
Project Euler 28 Inefficient Answer Proposal Create "Spiral Numbers"